install, uninstall. pack to ModuleService

This commit is contained in:
2024-10-09 16:42:20 +03:00
parent fa2676ddb2
commit 9abfdd2fd9
70 changed files with 2317 additions and 57 deletions

View File

@ -22,31 +22,11 @@ class ModuleController extends ConsoleController
throw new \Exception('Missing module path "--path" specified');
}
$zip = new ZipArchive;
$tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $this->argv['path']);
if ($res === TRUE) {
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
$this->out->r('Архив распакован', 'green');
if (file_exists(ROOT_DIR . $this->argv['path'])) {
$moduleService = new ModuleService();
$moduleService->installModule($this->argv['path']);
} else {
$this->out->r('Message: Ошибка чтения архива', 'red');
}
if (file_exists($tmpModuleDirFull . "/app/manifest.json")){
$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']);
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']);
$this->out->r("Удаляем временные файлы", 'green');
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
$this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
$this->out->r("Модуль не найден", 'red');
}
}
@ -61,25 +41,15 @@ class ModuleController extends ConsoleController
if (file_exists(ROOT_DIR . $this->argv['path'])) {
$moduleService = new ModuleService();
$moduleInfo = $moduleService->getModuleInfo(APP_DIR . '/modules/' . basename($this->argv['path']));
if ($moduleService->isActive($moduleInfo['slug'])) {
$moduleService->setActiveModule($moduleInfo['slug']);
}
$fileHelper = new Files();
if (file_exists(APP_DIR . '/modules/' . $moduleInfo['slug'])) {
$fileHelper->recursiveRemoveDir(APP_DIR . '/modules/'. $moduleInfo['slug']);
}
if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug'])) {
$fileHelper->recursiveRemoveDir(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug']);
}
$this->out->r("Модуль удален", 'green');
$moduleService->uninstallModule($this->argv['path']);
} else {
$this->out->r("Модуль не найден", 'red');
}
}
/**
* @throws \Exception
*/
public function actionPackModule(): void
{
if (!isset($this->argv['path'])) {
@ -87,22 +57,10 @@ class ModuleController extends ConsoleController
}
if (file_exists(ROOT_DIR . $this->argv['path'])) {
$moduleName = basename($this->argv['path']);
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $moduleName . "/";
$fileHelper = new Files();
$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');
$moduleService = new ModuleService();
$moduleService->packModule($this->argv['path']);
} else {
$this->out->r("Модуль не найден", 'red');
}
}

View File

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

View File

@ -3,9 +3,12 @@
namespace kernel\services;
use DirectoryIterator;
use kernel\console\Out;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\models\Option;
use ZipArchive;
class ModuleService
{
@ -44,7 +47,7 @@ class ModuleService
return false;
}
public function setActiveModule(string $module): void
public function toggleModule(string $module): void
{
$active_modules = Option::where("key", "active_modules")->first();
$path = json_decode($active_modules->value);
@ -154,4 +157,79 @@ class ModuleService
return $migrationsPaths;
}
/**
* @throws \Exception
*/
public function installModule(string $path): void
{
$zip = new ZipArchive;
$tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
// $out->out->r('Архив распакован', 'green');
// } else {
// $this->out->r('Message: Ошибка чтения архива', 'red');
}
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
throw new \Exception('manifest.json not found');
}
$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']);
if (isset($manifest['kernel_module_path'])) {
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']);
} 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');
}
public function uninstallModule(string $path): void
{
$moduleInfo = $this->getModuleInfo(APP_DIR . '/modules/' . basename($path));
if ($this->isActive($moduleInfo['slug'])) {
$this->toggleModule($moduleInfo['slug']);
}
$fileHelper = new Files();
if (file_exists(APP_DIR . '/modules/' . $moduleInfo['slug'])) {
$fileHelper->recursiveRemoveDir(APP_DIR . '/modules/'. $moduleInfo['slug']);
}
if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug'])) {
$fileHelper->recursiveRemoveDir(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug']);
}
}
public function packModule(string $path): void
{
$moduleName = basename($path);
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $moduleName . "/";
$fileHelper = new Files();
$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');
}
}