MicroFrameWork/kernel/console/controllers/ModuleController.php

109 lines
4.0 KiB
PHP
Raw Normal View History

2024-10-08 12:16:41 +03:00
<?php
namespace kernel\console\controllers;
use kernel\console\ConsoleController;
2024-10-08 17:06:14 +03:00
use kernel\helpers\Debug;
2024-10-08 12:16:41 +03:00
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\models\Option;
use ZipArchive;
use kernel\services\ModuleService;
class ModuleController extends ConsoleController
{
/**
* @throws \Exception
*/
public function actionInstallModule(): void
{
if (!isset($this->argv['path'])) {
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');
} else {
$this->out->r('Message: Ошибка чтения архива', 'red');
}
2024-10-09 15:12:30 +03:00
if (file_exists($tmpModuleDirFull . "/app/manifest.json")){
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
2024-10-08 12:16:41 +03:00
$manifest = Manifest::getWithVars($manifestJson);
$this->out->r('manifest.json инициализирован', 'green');
$fileHelper = new Files();
2024-10-09 15:12:30 +03:00
$fileHelper->copy_folder($tmpModuleDirFull . '/app', $manifest['app_module_path']);
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']);
2024-10-08 12:16:41 +03:00
$this->out->r("Удаляем временные файлы", 'green');
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
$this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
}
}
/**
* @throws \Exception
*/
public function actionUninstallModule(): void
{
if (!isset($this->argv['path'])) {
throw new \Exception('Missing module path "--path" specified');
}
if (file_exists(ROOT_DIR . $this->argv['path'])) {
$moduleService = new ModuleService();
2024-10-09 15:12:30 +03:00
$moduleInfo = $moduleService->getModuleInfo(APP_DIR . '/modules/' . basename($this->argv['path']));
2024-10-08 12:16:41 +03:00
if ($moduleService->isActive($moduleInfo['slug'])) {
$moduleService->setActiveModule($moduleInfo['slug']);
}
$fileHelper = new Files();
2024-10-09 15:12:30 +03:00
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']);
}
2024-10-08 12:16:41 +03:00
$this->out->r("Модуль удален", 'green');
} else {
$this->out->r("Модуль не найден", 'red');
}
}
2024-10-08 17:06:14 +03:00
public function actionPackModule(): void
{
if (!isset($this->argv['path'])) {
throw new \Exception('Missing module path "--path" specified');
}
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/');
2024-10-09 15:12:30 +03:00
$this->out->r("Модуль собран во временную папку", 'green');
2024-10-08 17:06:14 +03:00
2024-10-09 15:12:30 +03:00
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.itguild');
2024-10-08 17:06:14 +03:00
$this->out->r("Архив создан", 'green');
2024-10-09 15:12:30 +03:00
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
$this->out->r("Временная папка удалена", 'green');
2024-10-08 17:06:14 +03:00
}
}
2024-10-08 12:16:41 +03:00
}