diff --git a/kernel/controllers/ModuleController.php b/kernel/controllers/ModuleController.php
index 4b415d6..52d8a87 100644
--- a/kernel/controllers/ModuleController.php
+++ b/kernel/controllers/ModuleController.php
@@ -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]);
diff --git a/kernel/modules/post/manifest.json b/kernel/modules/post/manifest.json
index 51e2b4a..2de6805 100644
--- a/kernel/modules/post/manifest.json
+++ b/kernel/modules/post/manifest.json
@@ -8,5 +8,5 @@
"module_class_file": "{KERNEL_MODULES}/post/PostModule.php",
"routs": "routs/post.php",
"migration_path": "migrations",
- "dependence": "user"
+ "dependence": "user,menu"
}
\ No newline at end of file
diff --git a/kernel/modules/user/manifest.json b/kernel/modules/user/manifest.json
index 10ae6d8..2ddffae 100644
--- a/kernel/modules/user/manifest.json
+++ b/kernel/modules/user/manifest.json
@@ -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"
}
\ No newline at end of file
diff --git a/kernel/routs/admin.php b/kernel/routs/admin.php
index 121395c..15aef7a 100644
--- a/kernel/routs/admin.php
+++ b/kernel/routs/admin.php
@@ -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']);
});
});
diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php
index 24b602c..4ff4ffc 100644
--- a/kernel/services/ModuleService.php
+++ b/kernel/services/ModuleService.php
@@ -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;
+ }
+
}
\ No newline at end of file
diff --git a/kernel/views/module/index.php b/kernel/views/module/index.php
index 52a6647..261cd51 100644
--- a/kernel/views/module/index.php
+++ b/kernel/views/module/index.php
@@ -30,12 +30,14 @@ $table->addAction(function ($row, $url) use ($moduleService){
if ($moduleService->isActive($slug)){
$label = "Деактивировать";
$btn_type = "warning";
+ $btn = "$label";
+
}
else {
$label = "Активировать";
$btn_type = "primary";
+ $btn = "$label";
}
- $btn = "$label";
return $btn;
});