From d3d5e3a8e514b9fd541bfe6984cb8b25664d7603 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Mon, 14 Oct 2024 15:52:52 +0300 Subject: [PATCH] kernel pack, update and some fix --- composer.lock | 2 +- kernel/admin_themes/default/layout/login.php | 3 +- .../console/controllers/KernelController.php | 37 ++++++ kernel/console/routs/cli.php | 7 + kernel/controllers/ModuleController.php | 21 ++- kernel/helpers/Files.php | 8 +- kernel/routs/admin.php | 1 + kernel/services/ModuleService.php | 122 +++++++++++------- kernel/views/module/index.php | 5 + 9 files changed, 147 insertions(+), 59 deletions(-) create mode 100644 kernel/console/controllers/KernelController.php diff --git a/composer.lock b/composer.lock index f693541..7dd7d6d 100644 --- a/composer.lock +++ b/composer.lock @@ -2562,5 +2562,5 @@ "ext-zip": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/kernel/admin_themes/default/layout/login.php b/kernel/admin_themes/default/layout/login.php index 902e4ee..7536d3f 100644 --- a/kernel/admin_themes/default/layout/login.php +++ b/kernel/admin_themes/default/layout/login.php @@ -14,7 +14,8 @@ - + + diff --git a/kernel/console/controllers/KernelController.php b/kernel/console/controllers/KernelController.php new file mode 100644 index 0000000..4b47ee6 --- /dev/null +++ b/kernel/console/controllers/KernelController.php @@ -0,0 +1,37 @@ +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'); + } + } + +} \ No newline at end of file diff --git a/kernel/console/routs/cli.php b/kernel/console/routs/cli.php index 537a724..e48f961 100644 --- a/kernel/console/routs/cli.php +++ b/kernel/console/routs/cli.php @@ -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']); +}); + diff --git a/kernel/controllers/ModuleController.php b/kernel/controllers/ModuleController.php index f345881..799833d 100644 --- a/kernel/controllers/ModuleController.php +++ b/kernel/controllers/ModuleController.php @@ -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]); + } + } \ No newline at end of file diff --git a/kernel/helpers/Files.php b/kernel/helpers/Files.php index 10f91bd..0027357 100644 --- a/kernel/helpers/Files.php +++ b/kernel/helpers/Files.php @@ -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); diff --git a/kernel/routs/admin.php b/kernel/routs/admin.php index 15aef7a..d144bd5 100644 --- a/kernel/routs/admin.php +++ b/kernel/routs/admin.php @@ -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']); }); }); }); \ No newline at end of file diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index 2ce8f9e..af441fe 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -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,31 +77,18 @@ 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"); - } - } - } - $active_modules->modules[] = $module; - $this->runInitScript($this->getModuleInfoBySlug($module)); + $this->setActiveModule($module); } - $active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE); - $active_modules_info->save(); } /** @@ -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(); diff --git a/kernel/views/module/index.php b/kernel/views/module/index.php index 17e2c13..6cdc2fd 100644 --- a/kernel/views/module/index.php +++ b/kernel/views/module/index.php @@ -48,5 +48,10 @@ $table->addAction(function ($row, $url) use ($moduleService){ return "Просмотр"; }); +$table->addAction(function ($row, $url) use ($moduleService){ + $slug = $row['slug']; + return "Обновить"; +}); + $table->create(); $table->render();