session errors

This commit is contained in:
2024-10-11 17:02:35 +03:00
parent 5b9386f970
commit 7cf3708e4d
9 changed files with 146 additions and 8174 deletions

View File

@ -11,6 +11,7 @@ use ZipArchive;
class ModuleService
{
protected array $errors = [];
public function getModuleInfo(string $module): false|array|string
{
@ -26,6 +27,16 @@ class ModuleService
return $info;
}
public function getErrors(): array
{
return $this->errors;
}
public function addError($msg): void
{
$this->errors[] = $msg;
}
public function getModuleInfoBySlug(string $slug): false|array|string
{
return $this->getModuleInfo($this->getModuleDir($slug));
@ -74,12 +85,16 @@ class ModuleService
$active_modules_info->save();
}
public function setActiveModule(string $module): void
/**
* @param string $module
* @return bool
*/
public function setActiveModule(string $module): bool
{
$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");
return true;
}
$module_info = $this->getModuleInfoBySlug($module);
@ -87,7 +102,8 @@ class ModuleService
$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");
$this->addError("first activate the $depend module");
return false;
}
}
}
@ -96,17 +112,20 @@ class ModuleService
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
return true;
}
/**
* @throws \Exception
* @param string $module
* @return bool
*/
public function unsetActiveModule(string $module): void
public function deactivateModule(string $module): bool
{
$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");
return true;
}
$dependence_array = $this->getDependencies();
@ -121,7 +140,8 @@ class ModuleService
}
if ($str_for_exception !== '') {
throw new \Exception("You can not deactivate $module module. First deactivate modules: $str_for_exception");
$this->addError("You can not deactivate $module module. First deactivate modules: $str_for_exception");
return false;
}
unset($active_modules->modules[array_search($module, $active_modules->modules)]);
@ -130,6 +150,8 @@ class ModuleService
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
return true;
}
public function getModuleDir(string $slug)