MicroFrameWork/kernel/services/ModuleService.php

519 lines
15 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-11-21 13:00:21 +03:00
use kernel\EntityRelation;
2024-12-02 16:25:28 +03:00
use kernel\Flash;
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\helpers\RESTClient;
2024-09-12 16:01:04 +03:00
use kernel\models\Option;
2024-12-02 16:25:28 +03:00
use MongoDB\Driver\Session;
use ZipArchive;
2024-09-12 16:01:04 +03:00
class ModuleService
{
2024-10-11 17:02:35 +03:00
protected array $errors = [];
2024-09-12 16:01:04 +03:00
2024-12-02 16:38:05 +03:00
protected null|bool $serverAvailable = null;
2024-10-14 15:52:52 +03:00
/**
* @param string $module
* @return false|array|string
*/
2024-09-12 16:01:04 +03:00
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;
}
2024-10-14 15:52:52 +03:00
/**
* @return array
*/
2024-10-11 17:02:35 +03:00
public function getErrors(): array
{
return $this->errors;
}
2024-10-14 15:52:52 +03:00
/**
* @param $msg
* @return void
*/
2024-10-11 17:02:35 +03:00
public function addError($msg): void
{
$this->errors[] = $msg;
}
2024-10-14 15:52:52 +03:00
/**
* @param string $slug
* @return false|array|string
*/
public function getModuleInfoBySlug(string $slug): false|array|string
{
return $this->getModuleInfo($this->getModuleDir($slug));
}
2024-10-14 15:52:52 +03:00
/**
* @param string $slug
* @return bool
*/
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-11-20 15:30:29 +03:00
$modules = json_decode($active_modules->value);
foreach ($modules->modules as $mod) {
if ($mod === $slug) {
2024-09-12 16:01:04 +03:00
return true;
}
}
}
return false;
}
2024-10-10 16:45:53 +03:00
/**
2024-10-14 15:52:52 +03:00
* @param string $module
* @return void
2024-10-10 16:45:53 +03:00
*/
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)) {
2024-10-14 15:52:52 +03:00
$this->deactivateModule($module);
2024-09-13 13:54:58 +03:00
} else {
2024-10-14 15:52:52 +03:00
$this->setActiveModule($module);
2024-09-13 13:54:58 +03:00
}
}
2024-10-11 17:02:35 +03:00
/**
* @param string $module
* @return bool
*/
public function setActiveModule(string $module): bool
2024-10-11 15:44:15 +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)) {
2024-10-11 17:02:35 +03:00
return true;
2024-10-11 15:44:15 +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)) {
2024-10-11 17:02:35 +03:00
$this->addError("first activate the $depend module");
return false;
2024-10-11 15:44:15 +03:00
}
}
}
$active_modules->modules[] = $module;
$this->runInitScript($this->getModuleInfoBySlug($module));
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
2024-10-11 17:02:35 +03:00
return true;
2024-10-11 15:44:15 +03:00
}
/**
2024-10-11 17:02:35 +03:00
* @param string $module
* @return bool
2024-10-11 15:44:15 +03:00
*/
2024-10-11 17:02:35 +03:00
public function deactivateModule(string $module): bool
2024-10-11 15:44:15 +03:00
{
$active_modules_info = Option::where("key", "active_modules")->first();
2024-11-21 13:00:21 +03:00
EntityRelation::removeEntityRelation($module);
EntityRelation::removePropertyRelation($module);
2024-10-11 15:44:15 +03:00
$active_modules = json_decode($active_modules_info->value);
if (!in_array($module, $active_modules->modules)) {
2024-10-11 17:02:35 +03:00
return true;
2024-10-11 15:44:15 +03:00
}
$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 .= ', ';
}
2024-10-14 15:52:52 +03:00
$str_for_exception .= $mod;
2024-10-11 15:44:15 +03:00
}
}
if ($str_for_exception !== '') {
2024-10-11 17:02:35 +03:00
$this->addError("You can not deactivate $module module. First deactivate modules: $str_for_exception");
return false;
2024-10-11 15:44:15 +03:00
}
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-10-11 17:02:35 +03:00
return true;
2024-10-11 15:44:15 +03:00
}
2024-10-15 16:15:46 +03:00
public function getModuleDir(string $slug)
2024-09-13 15:45:02 +03:00
{
$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;
}
2024-10-14 15:52:52 +03:00
/**
* @param $mod_info
* @return void
*/
public function runInitScript($mod_info): void
{
2024-11-19 12:27:52 +03:00
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();
}
}
2024-10-14 15:52:52 +03:00
/**
* @param $mod_info
* @return void
*/
public function runDeactivateScript($mod_info): void
{
2024-11-19 12:27:52 +03:00
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-10-14 15:52:52 +03:00
/**
* @return array
*/
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-11-19 12:27:52 +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-11-19 12:27:52 +03:00
2024-09-13 16:51:41 +03:00
return $modules;
}
2024-10-14 15:52:52 +03:00
/**
* @return array
*/
2024-09-13 16:51:41 +03:00
public function getModulesRouts(): array
{
$routs = [];
$modules = $this->getActiveModules();
2024-11-19 12:27:52 +03:00
foreach ($modules as $module) {
if (isset($module['routs'])) {
2024-09-13 16:51:41 +03:00
$routs[] = $module['path'] . "/" . $module['routs'];
}
}
return $routs;
}
2024-10-14 15:52:52 +03:00
/**
* @return array
*/
public function getModulesMigrationsPaths(): array
{
$migrationsPaths = [];
$modules = $this->getActiveModules();
2024-11-19 12:27:52 +03:00
foreach ($modules as $module) {
if (isset($module['migration_path'])) {
$migrationsPaths[] = $module['path'] . "/" . $module['migration_path'];
}
}
return $migrationsPaths;
}
/**
2024-10-14 15:52:52 +03:00
* @param string $path
* @return bool
*/
2024-10-14 15:52:52 +03:00
public function installModule(string $path): bool
{
$zip = new ZipArchive;
$tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
2024-11-14 01:55:04 +03:00
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
2024-10-10 11:19:51 +03:00
} else {
2024-10-14 15:52:52 +03:00
$this->addError('unable to open zip archive');
return false;
}
2024-11-14 01:55:04 +03:00
if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
2024-10-14 15:52:52 +03:00
$this->addError('manifest.json not found');
return false;
}
2024-11-14 01:55:04 +03:00
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files();
2024-11-14 01:55:04 +03:00
$fileHelper->copy_folder($tmpModuleDirFull . 'app', $manifest['app_module_path']);
if (isset($manifest['kernel_module_path'])) {
2024-11-14 01:55:04 +03:00
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
} else {
2024-11-14 01:55:04 +03:00
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
}
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
2024-10-14 15:52:52 +03:00
return true;
}
2024-10-11 15:44:15 +03:00
/**
2024-10-14 15:52:52 +03:00
* @param string $path
* @return void
2024-10-11 15:44:15 +03:00
*/
public function uninstallModule(string $path): void
{
$moduleInfo = $this->getModuleInfo(APP_DIR . '/modules/' . basename($path));
if ($this->isActive($moduleInfo['slug'])) {
2024-10-14 15:52:52 +03:00
$this->deactivateModule($moduleInfo['slug']);
}
$fileHelper = new Files();
if (file_exists(APP_DIR . '/modules/' . $moduleInfo['slug'])) {
2024-11-19 12:27:52 +03:00
$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-14 15:52:52 +03:00
/**
* @param string $path
* @return void
*/
public function packModule(string $path): void
{
$moduleName = basename($path);
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $moduleName . "/";
$fileHelper = new Files();
2024-10-25 16:09:26 +03:00
$fileHelper->copy_folder(ROOT_DIR . $path, $tmpModuleDirFull . 'app/');
2024-11-19 12:27:52 +03:00
if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleName)) {
2024-10-25 16:09:26 +03:00
$fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');
2024-11-19 12:27:52 +03:00
} else {
2024-10-25 16:09:26 +03:00
mkdir($tmpModuleDirFull . 'kernel/');
}
2024-10-25 16:09:26 +03:00
if (!is_dir(RESOURCES_DIR . '/tmp/modules')) {
2024-10-17 14:55:00 +03:00
mkdir(RESOURCES_DIR . '/tmp/modules', 0777, true);
}
2024-10-24 11:49:19 +03:00
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.igm');
}
2024-10-10 11:19:51 +03:00
/**
2024-10-14 15:52:52 +03:00
* @param string $path
* @return bool
2024-10-10 11:19:51 +03:00
*/
2024-10-14 15:52:52 +03:00
public function updateModule(string $path): bool
2024-10-10 11:19:51 +03:00
{
$zip = new ZipArchive;
$tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
2024-11-14 01:55:04 +03:00
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
2024-10-10 11:19:51 +03:00
$zip->extractTo($tmpModuleDirFull);
$zip->close();
} else {
2024-10-14 15:52:52 +03:00
$this->addError('unable to open zip archive');
return false;
2024-10-10 11:19:51 +03:00
}
2024-11-14 01:55:04 +03:00
if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
2024-10-14 15:52:52 +03:00
$this->addError('manifest.json not found');
return false;
2024-10-10 11:19:51 +03:00
}
2024-11-14 01:55:04 +03:00
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
2024-10-10 11:19:51 +03:00
$manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files();
2024-11-14 01:55:04 +03:00
$fileHelper->copy_folder($tmpModuleDirFull . 'app/manifest.json', $manifest['app_module_path'] . '/manifest.json');
2024-10-10 11:19:51 +03:00
if (isset($manifest['kernel_module_path'])) {
2024-11-14 01:55:04 +03:00
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
2024-10-10 11:19:51 +03:00
} else {
2024-11-14 01:55:04 +03:00
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
2024-10-10 11:19:51 +03:00
}
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
2024-10-14 15:52:52 +03:00
return true;
2024-10-10 11:19:51 +03:00
}
2024-10-14 15:52:52 +03:00
/**
* @return array
*/
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-10-31 16:12:02 +03:00
public function isInstall(string $slug): bool
{
$module_paths = Option::where("key", "module_paths")->first();
$dirs = [];
2024-11-19 12:27:52 +03:00
if ($module_paths) {
2024-10-31 16:12:02 +03:00
$path = json_decode($module_paths->value);
2024-11-19 12:27:52 +03:00
foreach ($path->paths as $p) {
2024-10-31 16:12:02 +03:00
$dirs[] = getConst($p);
}
}
2024-11-19 12:27:52 +03:00
foreach ($dirs as $dir) {
2024-10-31 16:12:02 +03:00
foreach (new DirectoryIterator($dir) as $fileInfo) {
2024-11-19 12:27:52 +03:00
if ($fileInfo->isDot()) continue;
2024-10-31 16:12:02 +03:00
if ($this->getModuleInfo($fileInfo->getPathname())['slug'] === $slug) {
return true;
};
}
}
return false;
}
2024-11-14 01:55:04 +03:00
public function isLastVersion(string $slug): bool
{
2024-12-02 16:38:05 +03:00
if ($this->isServerAvailable()){
2024-12-02 16:25:28 +03:00
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
2024-12-02 16:25:28 +03:00
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
return true;
}
2024-11-14 01:55:04 +03:00
}
}
2024-11-29 13:23:48 +03:00
return false;
2024-11-14 01:55:04 +03:00
}
public function isKernelModule(string $slug): bool
{
2024-11-19 12:27:52 +03:00
$modules_info = $this->getKernelModules();
foreach ($modules_info as $mod) {
2024-11-19 12:27:52 +03:00
if ($mod['slug'] === $slug) {
return true;
}
}
2024-11-19 12:27:52 +03:00
return false;
}
2024-12-02 16:50:44 +03:00
public function isShopModule(string $slug): bool
{
if ($this->isServerAvailable()){
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug']) {
return true;
}
}
}
return false;
}
2024-11-19 12:27:52 +03:00
public function getKernelModules(): array
{
$modules_info = [];
foreach (new DirectoryIterator(KERNEL_MODULES_DIR) as $fileInfo) {
if ($fileInfo->isDot()) continue;
$modules_info[] = $this->getModuleInfo($fileInfo->getPathname());
}
return $modules_info;
}
2024-12-02 16:38:05 +03:00
public function isServerAvailable(): bool
{
if (null !== $this->serverAvailable){
return $this->serverAvailable;
}
try {
RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$this->serverAvailable = true;
return true;
} catch (\Exception $e) {
$this->serverAvailable = false;
return false;
}
}
2024-09-12 16:01:04 +03:00
}