kernel pack, update and some fix
This commit is contained in:
@ -13,6 +13,10 @@ class ModuleService
|
||||
{
|
||||
protected array $errors = [];
|
||||
|
||||
/**
|
||||
* @param string $module
|
||||
* @return false|array|string
|
||||
*/
|
||||
public function getModuleInfo(string $module): false|array|string
|
||||
{
|
||||
$info = [];
|
||||
@ -27,21 +31,36 @@ class ModuleService
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors(): array
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $msg
|
||||
* @return void
|
||||
*/
|
||||
public function addError($msg): void
|
||||
{
|
||||
$this->errors[] = $msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return false|array|string
|
||||
*/
|
||||
public function getModuleInfoBySlug(string $slug): false|array|string
|
||||
{
|
||||
return $this->getModuleInfo($this->getModuleDir($slug));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return bool
|
||||
*/
|
||||
public function isActive(string $slug): bool
|
||||
{
|
||||
$active_modules = Option::where("key", "active_modules")->first();
|
||||
@ -58,31 +77,18 @@ class ModuleService
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
* @param string $module
|
||||
* @return void
|
||||
*/
|
||||
public function toggleModule(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)) {
|
||||
unset($active_modules->modules[array_search($module, $active_modules->modules)]);
|
||||
$active_modules->modules = array_values($active_modules->modules);
|
||||
$this->runDeactivateScript($this->getModuleInfoBySlug($module));
|
||||
$this->deactivateModule($module);
|
||||
} else {
|
||||
$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->setActiveModule($module);
|
||||
}
|
||||
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
|
||||
$active_modules_info->save();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -135,7 +141,7 @@ class ModuleService
|
||||
if ($str_for_exception !== '') {
|
||||
$str_for_exception .= ', ';
|
||||
}
|
||||
$str_for_exception .= "$mod";
|
||||
$str_for_exception .= $mod;
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,7 +160,11 @@ class ModuleService
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getModuleDir(string $slug)
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return false
|
||||
*/
|
||||
public function getModuleDir(string $slug): false
|
||||
{
|
||||
$module_paths = Option::where("key", "module_paths")->first();
|
||||
$dirs = [];
|
||||
@ -175,6 +185,10 @@ class ModuleService
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mod_info
|
||||
* @return void
|
||||
*/
|
||||
public function runInitScript($mod_info): void
|
||||
{
|
||||
if (isset($mod_info['module_class'])){
|
||||
@ -186,6 +200,10 @@ class ModuleService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mod_info
|
||||
* @return void
|
||||
*/
|
||||
public function runDeactivateScript($mod_info): void
|
||||
{
|
||||
if (isset($mod_info['module_class'])){
|
||||
@ -197,6 +215,9 @@ class ModuleService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getActiveModules(): array
|
||||
{
|
||||
$modules = [];
|
||||
@ -222,6 +243,9 @@ class ModuleService
|
||||
return $modules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getModulesRouts(): array
|
||||
{
|
||||
$routs = [];
|
||||
@ -235,6 +259,9 @@ class ModuleService
|
||||
return $routs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getModulesMigrationsPaths(): array
|
||||
{
|
||||
$migrationsPaths = [];
|
||||
@ -249,9 +276,10 @@ class ModuleService
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
public function installModule(string $path): void
|
||||
public function installModule(string $path): bool
|
||||
{
|
||||
$zip = new ZipArchive;
|
||||
$tmpModuleDir = md5(time());
|
||||
@ -260,21 +288,18 @@ class ModuleService
|
||||
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
|
||||
$zip->extractTo($tmpModuleDirFull);
|
||||
$zip->close();
|
||||
// $out->out->r('Архив распакован', 'green');
|
||||
// } else {
|
||||
// $this->out->r('Message: Ошибка чтения архива', 'red');
|
||||
} else {
|
||||
$zip->close();
|
||||
throw new \Exception("unable to open zip archive");
|
||||
$this->addError('unable to open zip archive');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
|
||||
throw new \Exception('manifest.json not found');
|
||||
$this->addError('manifest.json not found');
|
||||
return false;
|
||||
}
|
||||
|
||||
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
|
||||
$manifest = Manifest::getWithVars($manifestJson);
|
||||
// $this->out->r('manifest.json инициализирован', 'green');
|
||||
|
||||
$fileHelper = new Files();
|
||||
$fileHelper->copy_folder($tmpModuleDirFull . '/app', $manifest['app_module_path']);
|
||||
@ -284,22 +309,22 @@ class ModuleService
|
||||
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
|
||||
}
|
||||
|
||||
// $this->out->r("Удаляем временные файлы", 'green');
|
||||
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
||||
var_dump($tmpModuleDirFull);
|
||||
|
||||
// $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
public function uninstallModule(string $path): void
|
||||
{
|
||||
$moduleInfo = $this->getModuleInfo(APP_DIR . '/modules/' . basename($path));
|
||||
|
||||
if ($this->isActive($moduleInfo['slug'])) {
|
||||
$this->unsetActiveModule($moduleInfo['slug']);
|
||||
$this->deactivateModule($moduleInfo['slug']);
|
||||
}
|
||||
|
||||
$fileHelper = new Files();
|
||||
@ -311,6 +336,10 @@ class ModuleService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
public function packModule(string $path): void
|
||||
{
|
||||
$moduleName = basename($path);
|
||||
@ -321,20 +350,16 @@ class ModuleService
|
||||
$fileHelper->copy_folder(APP_DIR . '/modules/' . $moduleName, $tmpModuleDirFull . 'app/');
|
||||
$fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');
|
||||
|
||||
// $this->out->r("Модуль собран во временную папку", 'green');
|
||||
|
||||
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.itguild');
|
||||
|
||||
// $this->out->r("Архив создан", 'green');
|
||||
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
||||
|
||||
// $this->out->r("Временная папка удалена", 'green');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
public function updateModule(string $path): void
|
||||
public function updateModule(string $path): bool
|
||||
{
|
||||
$zip = new ZipArchive;
|
||||
$tmpModuleDir = md5(time());
|
||||
@ -343,21 +368,18 @@ class ModuleService
|
||||
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
|
||||
$zip->extractTo($tmpModuleDirFull);
|
||||
$zip->close();
|
||||
// $out->out->r('Архив распакован', 'green');
|
||||
// } else {
|
||||
// $this->out->r('Message: Ошибка чтения архива', 'red');
|
||||
} else {
|
||||
$zip->close();
|
||||
throw new \Exception("unable to open zip archive");
|
||||
$this->addError('unable to open zip archive');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
|
||||
throw new \Exception('manifest.json not found');
|
||||
$this->addError('manifest.json not found');
|
||||
return false;
|
||||
}
|
||||
|
||||
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
|
||||
$manifest = Manifest::getWithVars($manifestJson);
|
||||
// $this->out->r('manifest.json инициализирован', 'green');
|
||||
|
||||
$fileHelper = new Files();
|
||||
if (isset($manifest['kernel_module_path'])) {
|
||||
@ -365,14 +387,14 @@ class ModuleService
|
||||
} else {
|
||||
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
|
||||
}
|
||||
|
||||
// $this->out->r("Удаляем временные файлы", 'green');
|
||||
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
||||
|
||||
// $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getDependencies(): array
|
||||
{
|
||||
$modules_info = $this->getActiveModules();
|
||||
|
Reference in New Issue
Block a user