MicroFrameWork/kernel/services/ModuleService.php

367 lines
13 KiB
PHP
Raw Normal View History

2024-09-12 16:01:04 +03:00
<?php
namespace kernel\services;
2024-09-13 15:45:02 +03:00
use DirectoryIterator;
2024-09-13 13:54:58 +03:00
use kernel\helpers\Debug;
use kernel\helpers\Files;
2024-09-12 16:01:04 +03:00
use kernel\helpers\Manifest;
use kernel\models\Option;
use ZipArchive;
2024-09-12 16:01:04 +03:00
class ModuleService
{
public function getModuleInfo(string $module): false|array|string
{
$info = [];
$info['path'] = $module;
2024-09-13 16:51:41 +03:00
if (file_exists($module . "/manifest.json")) {
2024-09-12 16:01:04 +03:00
$manifest = file_get_contents($module . "/manifest.json");
$manifest = Manifest::getWithVars($manifest);
$manifest = getConst($manifest);
2024-09-12 16:01:04 +03:00
$info = array_merge($info, $manifest);
}
return $info;
}
public function getModuleInfoBySlug(string $slug): false|array|string
{
return $this->getModuleInfo($this->getModuleDir($slug));
}
2024-09-12 16:01:04 +03:00
public function isActive(string $slug): bool
{
2024-09-13 13:54:58 +03:00
$active_modules = Option::where("key", "active_modules")->first();
2024-09-13 16:51:41 +03:00
if ($active_modules) {
2024-09-12 16:01:04 +03:00
$path = json_decode($active_modules->value);
2024-09-13 16:51:41 +03:00
foreach ($path->modules as $p) {
if ($p === $slug) {
2024-09-12 16:01:04 +03:00
return true;
}
}
}
return false;
}
2024-10-10 16:45:53 +03:00
/**
* @throws \Exception
*/
public function toggleModule(string $module): void
2024-09-13 13:54:58 +03:00
{
2024-10-10 16:45:53 +03:00
$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));
2024-09-13 13:54:58 +03:00
} else {
2024-10-10 16:45:53 +03:00
$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));
2024-09-13 13:54:58 +03:00
}
2024-10-10 16:45:53 +03:00
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
2024-09-13 13:54:58 +03:00
}
2024-10-11 15:44:15 +03:00
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();
}
2024-09-13 15:45:02 +03:00
public function getModuleDir(string $slug)
{
$module_paths = Option::where("key", "module_paths")->first();
$dirs = [];
2024-09-13 16:51:41 +03:00
if ($module_paths) {
2024-09-13 15:45:02 +03:00
$path = json_decode($module_paths->value);
2024-09-13 16:51:41 +03:00
foreach ($path->paths as $p) {
2024-09-13 15:45:02 +03:00
$dirs[] = getConst($p);
}
}
2024-09-13 16:51:41 +03:00
foreach ($dirs as $dir) {
2024-09-13 15:45:02 +03:00
foreach (new DirectoryIterator($dir) as $fileInfo) {
2024-09-13 16:51:41 +03:00
if (basename($fileInfo->getPathname()) === $slug) {
2024-09-13 15:55:52 +03:00
return $fileInfo->getPathname();
}
2024-09-13 15:45:02 +03:00
}
}
return false;
}
public function runInitScript($mod_info): void
{
if (isset($mod_info['module_class'])){
if (isset($mod_info['module_class_file'])){
require_once $mod_info['module_class_file'];
}
$moduleClass = new $mod_info['module_class']();
$moduleClass->init();
}
}
public function runDeactivateScript($mod_info): void
{
if (isset($mod_info['module_class'])){
if (isset($mod_info['module_class_file'])){
require_once $mod_info['module_class_file'];
}
$moduleClass = new $mod_info['module_class']();
$moduleClass->deactivate();
}
}
2024-09-13 16:51:41 +03:00
public function getActiveModules(): array
{
$modules = [];
$module_paths = Option::where("key", "module_paths")->first();
2024-09-18 13:48:12 +03:00
$active_modules = Option::where("key", "active_modules")->first();
2024-09-13 16:51:41 +03:00
$dirs = [];
if ($module_paths) {
$path = json_decode($module_paths->value);
foreach ($path->paths as $p) {
$dirs[] = getConst($p);
}
}
2024-09-18 13:48:12 +03:00
$active_modules = json_decode($active_modules->value, true);
2024-09-13 16:51:41 +03:00
foreach ($dirs as $dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) {
2024-09-18 13:48:12 +03:00
if($fileInfo->isDot() or !in_array($fileInfo->getFilename(), $active_modules['modules'])) continue;
2024-09-13 16:51:41 +03:00
$modules[] = $this->getModuleInfo($fileInfo->getPathname());
}
}
2024-09-18 13:48:12 +03:00
2024-09-13 16:51:41 +03:00
return $modules;
}
public function getModulesRouts(): array
{
$routs = [];
$modules = $this->getActiveModules();
foreach ($modules as $module){
if (isset($module['routs'])){
$routs[] = $module['path'] . "/" . $module['routs'];
}
}
return $routs;
}
public function getModulesMigrationsPaths(): array
{
$migrationsPaths = [];
$modules = $this->getActiveModules();
foreach ($modules as $module){
if (isset($module['migration_path'])){
$migrationsPaths[] = $module['path'] . "/" . $module['migration_path'];
}
}
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');
2024-10-10 11:19:51 +03:00
} else {
$zip->close();
throw new \Exception("unable to open zip archive");
}
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);
2024-10-10 11:19:51 +03:00
var_dump($tmpModuleDirFull);
// $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
}
2024-10-11 15:44:15 +03:00
/**
* @throws \Exception
*/
public function uninstallModule(string $path): void
{
$moduleInfo = $this->getModuleInfo(APP_DIR . '/modules/' . basename($path));
if ($this->isActive($moduleInfo['slug'])) {
2024-10-11 15:44:15 +03:00
$this->unsetActiveModule($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);
2024-10-10 11:19:51 +03:00
// $this->out->r("Временная папка удалена", 'green');
}
2024-10-10 11:19:51 +03:00
/**
* @throws \Exception
*/
public function updateModule(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');
} else {
$zip->close();
throw new \Exception("unable to open zip archive");
}
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();
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');
}
2024-10-11 15:44:15 +03:00
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;
}
2024-09-12 16:01:04 +03:00
}