module set and unset, dependencies

This commit is contained in:
Билай Станислав 2024-10-11 15:44:15 +03:00
parent d9c4cecd48
commit 5b9386f970
6 changed files with 92 additions and 5 deletions

View File

@ -59,7 +59,16 @@ class ModuleController extends AdminController
public function actionActivate(): void
{
$request = new Request();
$this->moduleService->toggleModule($request->get("slug"));
$this->moduleService->setActiveModule($request->get("slug"));
$mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));
$this->cgView->render("view.php", ['data' => $mod_info]);
}
public function actionDeactivate(): void
{
$request = new Request();
$this->moduleService->unsetActiveModule($request->get("slug"));
$mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));
$this->cgView->render("view.php", ['data' => $mod_info]);

View File

@ -8,5 +8,5 @@
"module_class_file": "{KERNEL_MODULES}/post/PostModule.php",
"routs": "routs/post.php",
"migration_path": "migrations",
"dependence": "user"
"dependence": "user,menu"
}

View File

@ -6,5 +6,6 @@
"description": "User module",
"module_class": "kernel\\modules\\user\\UserModule",
"module_class_file": "{KERNEL_MODULES}/user/UserModule.php",
"routs": "routs/user.php"
"routs": "routs/user.php",
"dependence": "menu"
}

View File

@ -10,6 +10,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->get('/', [\kernel\controllers\ModuleController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\kernel\controllers\ModuleController::class, 'actionIndex']);
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']);
});
});

View File

@ -74,6 +74,64 @@ class ModuleService
$active_modules_info->save();
}
public function setActiveModule(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)) {
throw new \Exception("$module module is already activated");
}
$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_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
}
/**
* @throws \Exception
*/
public function unsetActiveModule(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)) {
throw new \Exception("$module module is already activated");
}
$dependence_array = $this->getDependencies();
$str_for_exception = '';
foreach ($dependence_array as $mod => $depend) {
if (in_array($module, $depend)) {
if ($str_for_exception !== '') {
$str_for_exception .= ', ';
}
$str_for_exception .= "$mod";
}
}
if ($str_for_exception !== '') {
throw new \Exception("You can not deactivate $module module. First deactivate modules: $str_for_exception");
}
unset($active_modules->modules[array_search($module, $active_modules->modules)]);
$active_modules->modules = array_values($active_modules->modules);
$this->runDeactivateScript($this->getModuleInfoBySlug($module));
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
}
public function getModuleDir(string $slug)
{
$module_paths = Option::where("key", "module_paths")->first();
@ -211,12 +269,15 @@ class ModuleService
// $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
}
/**
* @throws \Exception
*/
public function uninstallModule(string $path): void
{
$moduleInfo = $this->getModuleInfo(APP_DIR . '/modules/' . basename($path));
if ($this->isActive($moduleInfo['slug'])) {
$this->toggleModule($moduleInfo['slug']);
$this->unsetActiveModule($moduleInfo['slug']);
}
$fileHelper = new Files();
@ -290,4 +351,17 @@ class ModuleService
}
public function getDependencies(): array
{
$modules_info = $this->getActiveModules();
$dependence_array = [];
foreach ($modules_info as $mod) {
if (isset($mod['dependence'])) {
$dependence_array[$mod['slug']] = explode(',', $mod['dependence']);
}
}
return $dependence_array;
}
}

View File

@ -30,12 +30,14 @@ $table->addAction(function ($row, $url) use ($moduleService){
if ($moduleService->isActive($slug)){
$label = "Деактивировать";
$btn_type = "warning";
$btn = "<a class='btn btn-$btn_type' href='$url/deactivate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
}
else {
$label = "Активировать";
$btn_type = "primary";
$btn = "<a class='btn btn-$btn_type' href='$url/activate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
}
$btn = "<a class='btn btn-$btn_type' href='$url/activate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
return $btn;
});