From 2711c0258d0840312ef4ca222f091c8f07edf31f Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Thu, 10 Oct 2024 16:45:53 +0300 Subject: [PATCH] dependences --- bootstrap.php | 1 + kernel/controllers/ModuleController.php | 6 ++--- kernel/modules/post/manifest.json | 3 ++- kernel/services/ModuleService.php | 29 +++++++++++++++++-------- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/bootstrap.php b/bootstrap.php index afca5a1..da9207e 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -33,6 +33,7 @@ function getConst($text): array|false|string ]; $str = $text; + foreach($constStr as $key => $value) { $str = str_replace($key, $value, $str); diff --git a/kernel/controllers/ModuleController.php b/kernel/controllers/ModuleController.php index cbae844..458671e 100644 --- a/kernel/controllers/ModuleController.php +++ b/kernel/controllers/ModuleController.php @@ -24,10 +24,10 @@ class ModuleController extends AdminController public function actionIndex(): void { - $admin_theme_paths = Option::where("key", "module_paths")->first(); + $module_paths = Option::where("key", "module_paths")->first(); $dirs = []; - if ($admin_theme_paths){ - $path = json_decode($admin_theme_paths->value); + if ($module_paths){ + $path = json_decode($module_paths->value); foreach ($path->paths as $p){ $dirs[] = getConst($p); } diff --git a/kernel/modules/post/manifest.json b/kernel/modules/post/manifest.json index fb2c610..51e2b4a 100644 --- a/kernel/modules/post/manifest.json +++ b/kernel/modules/post/manifest.json @@ -7,5 +7,6 @@ "module_class": "kernel\\modules\\post\\PostModule", "module_class_file": "{KERNEL_MODULES}/post/PostModule.php", "routs": "routs/post.php", - "migration_path": "migrations" + "migration_path": "migrations", + "dependence": "user" } \ No newline at end of file diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index 8d9d228..24b602c 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -3,7 +3,6 @@ namespace kernel\services; use DirectoryIterator; -use kernel\console\Out; use kernel\helpers\Debug; use kernel\helpers\Files; use kernel\helpers\Manifest; @@ -47,20 +46,32 @@ class ModuleService return false; } + /** + * @throws \Exception + */ public function toggleModule(string $module): void { - $active_modules = Option::where("key", "active_modules")->first(); - $path = json_decode($active_modules->value); - if (in_array($module, $path->modules)) { - unset($path->modules[array_search($module, $path->modules)]); - $path->modules = array_values($path->modules); + $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)); } else { - $path->modules[] = $module; + $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)); } - $active_modules->value = json_encode($path, JSON_UNESCAPED_UNICODE); - $active_modules->save(); + $active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE); + $active_modules_info->save(); } public function getModuleDir(string $slug)