module set and unset, dependencies
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user