dependences

This commit is contained in:
Билай Станислав 2024-10-10 16:45:53 +03:00
parent e29108f21d
commit 2711c0258d
4 changed files with 26 additions and 13 deletions

View File

@ -33,6 +33,7 @@ function getConst($text): array|false|string
]; ];
$str = $text; $str = $text;
foreach($constStr as $key => $value) foreach($constStr as $key => $value)
{ {
$str = str_replace($key, $value, $str); $str = str_replace($key, $value, $str);

View File

@ -24,10 +24,10 @@ class ModuleController extends AdminController
public function actionIndex(): void public function actionIndex(): void
{ {
$admin_theme_paths = Option::where("key", "module_paths")->first(); $module_paths = Option::where("key", "module_paths")->first();
$dirs = []; $dirs = [];
if ($admin_theme_paths){ if ($module_paths){
$path = json_decode($admin_theme_paths->value); $path = json_decode($module_paths->value);
foreach ($path->paths as $p){ foreach ($path->paths as $p){
$dirs[] = getConst($p); $dirs[] = getConst($p);
} }

View File

@ -7,5 +7,6 @@
"module_class": "kernel\\modules\\post\\PostModule", "module_class": "kernel\\modules\\post\\PostModule",
"module_class_file": "{KERNEL_MODULES}/post/PostModule.php", "module_class_file": "{KERNEL_MODULES}/post/PostModule.php",
"routs": "routs/post.php", "routs": "routs/post.php",
"migration_path": "migrations" "migration_path": "migrations",
"dependence": "user"
} }

View File

@ -3,7 +3,6 @@
namespace kernel\services; namespace kernel\services;
use DirectoryIterator; use DirectoryIterator;
use kernel\console\Out;
use kernel\helpers\Debug; use kernel\helpers\Debug;
use kernel\helpers\Files; use kernel\helpers\Files;
use kernel\helpers\Manifest; use kernel\helpers\Manifest;
@ -47,20 +46,32 @@ class ModuleService
return false; return false;
} }
/**
* @throws \Exception
*/
public function toggleModule(string $module): void public function toggleModule(string $module): void
{ {
$active_modules = Option::where("key", "active_modules")->first(); $active_modules_info = Option::where("key", "active_modules")->first();
$path = json_decode($active_modules->value); $active_modules = json_decode($active_modules_info->value);
if (in_array($module, $path->modules)) { if (in_array($module, $active_modules->modules)) {
unset($path->modules[array_search($module, $path->modules)]); unset($active_modules->modules[array_search($module, $active_modules->modules)]);
$path->modules = array_values($path->modules); $active_modules->modules = array_values($active_modules->modules);
$this->runDeactivateScript($this->getModuleInfoBySlug($module)); $this->runDeactivateScript($this->getModuleInfoBySlug($module));
} else { } 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)); $this->runInitScript($this->getModuleInfoBySlug($module));
} }
$active_modules->value = json_encode($path, JSON_UNESCAPED_UNICODE); $active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules->save(); $active_modules_info->save();
} }
public function getModuleDir(string $slug) public function getModuleDir(string $slug)