kernel pack, update and some fix

This commit is contained in:
Билай Станислав 2024-10-14 15:52:52 +03:00
parent 7cf3708e4d
commit d3d5e3a8e5
9 changed files with 147 additions and 59 deletions

2
composer.lock generated
View File

@ -2562,5 +2562,5 @@
"ext-zip": "*"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"
"plugin-api-version": "2.6.0"
}

View File

@ -14,7 +14,8 @@
<link href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700,800,900" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="<?=$resources?>/css/style.css">
<link rel="stylesheet" href="<?= $resources ?>/css/bootstrap.min.css">
<link rel="stylesheet" href="<?= $resources ?>/css/style.css">
</head>
<body>

View File

@ -0,0 +1,37 @@
<?php
namespace kernel\console\controllers;
use kernel\console\ConsoleController;
use kernel\helpers\Files;
use kernel\services\KernelService;
class KernelController extends ConsoleController
{
/**
* @throws \Exception
*/
public function actionPackKernel(): void
{
if (!isset($this->argv['path'])) {
throw new \Exception('Missing kernel path "--path" specified');
}
if (file_exists(ROOT_DIR . $this->argv['path'])) {
$tmpKernelDirFull = RESOURCES_DIR . '/tmp/ad/kernel/';
$fileHelper = new Files();
$fileHelper->copy_folder(KERNEL_DIR, $tmpKernelDirFull);
$fileHelper->pack($tmpKernelDirFull, RESOURCES_DIR . '/tmp/kernel/kernel.itguild');
$fileHelper->recursiveRemoveDir($tmpKernelDirFull);
$this->out->r("Ядро заархивировано", 'green');
} else {
$this->out->r("Ядро не найдено", 'red');
}
}
}

View File

@ -28,3 +28,10 @@ App::$collector->group(["prefix" => "module"], callback: function (RouteCollecto
App::$collector->console('update', [\kernel\console\controllers\ModuleController::class, 'actionUpdateModule']);
});
App::$collector->group(["prefix" => "kernel"], callback: function (RouteCollector $router){
// App::$collector->console('install', [\kernel\console\controllers\ModuleController::class, 'actionInstallModule']);
// App::$collector->console('uninstall', [\kernel\console\controllers\ModuleController::class, 'actionUninstallModule']);
App::$collector->console('pack', [\kernel\console\controllers\KernelController::class, 'actionPackKernel']);
App::$collector->console('update', [\kernel\console\controllers\KernelController::class, 'actionUpdateKernel']);
});

View File

@ -74,7 +74,12 @@ class ModuleController extends AdminController
public function actionDeactivate(): void
{
$request = new Request();
$this->moduleService->deactivateModule($request->get("slug"));
$active_res = $this->moduleService->deactivateModule($request->get("slug"));
if (!$active_res){
Session::start();
Session::set("error", implode(";", $this->moduleService->getErrors()));
$this->redirect("/admin", 302);
}
$mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));
$this->cgView->render("view.php", ['data' => $mod_info]);
@ -88,4 +93,18 @@ class ModuleController extends AdminController
$this->cgView->render("view.php", ['data' => $mod_info]);
}
public function actionUpdate(): void
{
$request = new Request();
$active_res = $this->moduleService->updateModule($request->get("slug"));
if (!$active_res){
Session::start();
Session::set("error", implode(";", $this->moduleService->getErrors()));
$this->redirect("/admin", 302);
}
$mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));
$this->cgView->render("view.php", ['data' => $mod_info]);
}
}

View File

@ -19,7 +19,7 @@ class Files
$d = dir($d1);
while (false !== ($entry = $d->read())) {
if ($entry != '.' && $entry != '..') {
if ($entry != '.' && $entry != '..' && $entry != 'app_modules') {
$this->copy_folder("$d1/$entry", "$d2/$entry");
}
}
@ -47,9 +47,6 @@ class Files
{
$zip = new ZipArchive();
$zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// if ($include_source) {
// $zip->addEmptyDir(basename($source));
// }
if (is_file($source)) {
$zip->addFile(basename($source));
@ -68,8 +65,7 @@ class Files
foreach ($includes as $include) {
if(is_dir($include)) {
// $folder .= '/' . $include->getFilename() . '/';
// $this->recursiveAddFile($zip, $include, $folder);
if ($include->getFilename() === 'app_modules') continue;
$tmpFolder = $folder . '/' . $include->getFilename() . '/';
$this->recursiveAddFile($zip, $include, $tmpFolder);

View File

@ -12,6 +12,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->get('/activate', [\kernel\controllers\ModuleController::class, 'actionActivate']);
App::$collector->get('/deactivate', [\kernel\controllers\ModuleController::class, 'actionDeactivate']);
App::$collector->get('/view', [\kernel\controllers\ModuleController::class, 'actionView']);
App::$collector->get('/update', [\kernel\controllers\ModuleController::class, 'actionUpdate']);
});
});
});

View File

@ -13,6 +13,10 @@ class ModuleService
{
protected array $errors = [];
/**
* @param string $module
* @return false|array|string
*/
public function getModuleInfo(string $module): false|array|string
{
$info = [];
@ -27,21 +31,36 @@ class ModuleService
return $info;
}
/**
* @return array
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* @param $msg
* @return void
*/
public function addError($msg): void
{
$this->errors[] = $msg;
}
/**
* @param string $slug
* @return false|array|string
*/
public function getModuleInfoBySlug(string $slug): false|array|string
{
return $this->getModuleInfo($this->getModuleDir($slug));
}
/**
* @param string $slug
* @return bool
*/
public function isActive(string $slug): bool
{
$active_modules = Option::where("key", "active_modules")->first();
@ -58,32 +77,19 @@ class ModuleService
}
/**
* @throws \Exception
* @param string $module
* @return void
*/
public function toggleModule(string $module): void
{
$active_modules_info = Option::where("key", "active_modules")->first();
$active_modules = json_decode($active_modules_info->value);
if (in_array($module, $active_modules->modules)) {
unset($active_modules->modules[array_search($module, $active_modules->modules)]);
$active_modules->modules = array_values($active_modules->modules);
$this->runDeactivateScript($this->getModuleInfoBySlug($module));
$this->deactivateModule($module);
} else {
$module_info = $this->getModuleInfoBySlug($module);
if (isset($module_info['dependence'])) {
$dependence_array = explode(';', $module_info['dependence']);
foreach ($dependence_array as $depend) {
if (!in_array($depend, $active_modules->modules)) {
throw new \Exception("first activate the $depend module");
$this->setActiveModule($module);
}
}
}
$active_modules->modules[] = $module;
$this->runInitScript($this->getModuleInfoBySlug($module));
}
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
}
/**
* @param string $module
@ -135,7 +141,7 @@ class ModuleService
if ($str_for_exception !== '') {
$str_for_exception .= ', ';
}
$str_for_exception .= "$mod";
$str_for_exception .= $mod;
}
}
@ -154,7 +160,11 @@ class ModuleService
return true;
}
public function getModuleDir(string $slug)
/**
* @param string $slug
* @return false
*/
public function getModuleDir(string $slug): false
{
$module_paths = Option::where("key", "module_paths")->first();
$dirs = [];
@ -175,6 +185,10 @@ class ModuleService
return false;
}
/**
* @param $mod_info
* @return void
*/
public function runInitScript($mod_info): void
{
if (isset($mod_info['module_class'])){
@ -186,6 +200,10 @@ class ModuleService
}
}
/**
* @param $mod_info
* @return void
*/
public function runDeactivateScript($mod_info): void
{
if (isset($mod_info['module_class'])){
@ -197,6 +215,9 @@ class ModuleService
}
}
/**
* @return array
*/
public function getActiveModules(): array
{
$modules = [];
@ -222,6 +243,9 @@ class ModuleService
return $modules;
}
/**
* @return array
*/
public function getModulesRouts(): array
{
$routs = [];
@ -235,6 +259,9 @@ class ModuleService
return $routs;
}
/**
* @return array
*/
public function getModulesMigrationsPaths(): array
{
$migrationsPaths = [];
@ -249,9 +276,10 @@ class ModuleService
}
/**
* @throws \Exception
* @param string $path
* @return bool
*/
public function installModule(string $path): void
public function installModule(string $path): bool
{
$zip = new ZipArchive;
$tmpModuleDir = md5(time());
@ -260,21 +288,18 @@ class ModuleService
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
// $out->out->r('Архив распакован', 'green');
// } else {
// $this->out->r('Message: Ошибка чтения архива', 'red');
} else {
$zip->close();
throw new \Exception("unable to open zip archive");
$this->addError('unable to open zip archive');
return false;
}
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
throw new \Exception('manifest.json not found');
$this->addError('manifest.json not found');
return false;
}
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson);
// $this->out->r('manifest.json инициализирован', 'green');
$fileHelper = new Files();
$fileHelper->copy_folder($tmpModuleDirFull . '/app', $manifest['app_module_path']);
@ -284,22 +309,22 @@ class ModuleService
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
}
// $this->out->r("Удаляем временные файлы", 'green');
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
var_dump($tmpModuleDirFull);
// $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
return true;
}
/**
* @throws \Exception
* @param string $path
* @return void
*/
public function uninstallModule(string $path): void
{
$moduleInfo = $this->getModuleInfo(APP_DIR . '/modules/' . basename($path));
if ($this->isActive($moduleInfo['slug'])) {
$this->unsetActiveModule($moduleInfo['slug']);
$this->deactivateModule($moduleInfo['slug']);
}
$fileHelper = new Files();
@ -311,6 +336,10 @@ class ModuleService
}
}
/**
* @param string $path
* @return void
*/
public function packModule(string $path): void
{
$moduleName = basename($path);
@ -321,20 +350,16 @@ class ModuleService
$fileHelper->copy_folder(APP_DIR . '/modules/' . $moduleName, $tmpModuleDirFull . 'app/');
$fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');
// $this->out->r("Модуль собран во временную папку", 'green');
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.itguild');
// $this->out->r("Архив создан", 'green');
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
// $this->out->r("Временная папка удалена", 'green');
}
/**
* @throws \Exception
* @param string $path
* @return bool
*/
public function updateModule(string $path): void
public function updateModule(string $path): bool
{
$zip = new ZipArchive;
$tmpModuleDir = md5(time());
@ -343,21 +368,18 @@ class ModuleService
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
// $out->out->r('Архив распакован', 'green');
// } else {
// $this->out->r('Message: Ошибка чтения архива', 'red');
} else {
$zip->close();
throw new \Exception("unable to open zip archive");
$this->addError('unable to open zip archive');
return false;
}
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
throw new \Exception('manifest.json not found');
$this->addError('manifest.json not found');
return false;
}
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson);
// $this->out->r('manifest.json инициализирован', 'green');
$fileHelper = new Files();
if (isset($manifest['kernel_module_path'])) {
@ -365,14 +387,14 @@ class ModuleService
} else {
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
}
// $this->out->r("Удаляем временные файлы", 'green');
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
// $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
return true;
}
/**
* @return array
*/
public function getDependencies(): array
{
$modules_info = $this->getActiveModules();

View File

@ -48,5 +48,10 @@ $table->addAction(function ($row, $url) use ($moduleService){
return "<a class='btn btn-primary' href='$url/view/?slug=$slug' style='margin: 3px; width: 150px;' >Просмотр</a>";
});
$table->addAction(function ($row, $url) use ($moduleService){
$slug = $row['slug'];
return "<a class='btn btn-primary' href='$url/update/?slug=$slug' style='margin: 3px; width: 150px;' >Обновить</a>";
});
$table->create();
$table->render();