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;
|
2024-10-09 16:42:20 +03:00
|
|
|
use kernel\helpers\Files;
|
2024-09-12 16:01:04 +03:00
|
|
|
use kernel\helpers\Manifest;
|
|
|
|
use kernel\models\Option;
|
2024-10-09 16:42:20 +03:00
|
|
|
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-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);
|
2024-09-23 12:50:50 +03:00
|
|
|
$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
|
|
|
|
*/
|
2024-09-23 12:50:50 +03:00
|
|
|
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-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
|
|
|
/**
|
2024-10-14 15:52:52 +03:00
|
|
|
* @param string $module
|
|
|
|
* @return void
|
2024-10-10 16:45:53 +03:00
|
|
|
*/
|
2024-10-09 16:42:20 +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();
|
|
|
|
$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
|
|
|
|
*/
|
2024-09-23 12:50:50 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-14 15:52:52 +03:00
|
|
|
/**
|
|
|
|
* @param $mod_info
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-09-23 12:50:50 +03:00
|
|
|
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-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-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;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
foreach ($modules as $module){
|
|
|
|
if (isset($module['routs'])){
|
|
|
|
$routs[] = $module['path'] . "/" . $module['routs'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $routs;
|
|
|
|
}
|
|
|
|
|
2024-10-14 15:52:52 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2024-09-23 12:50:50 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-10-09 16:42:20 +03:00
|
|
|
/**
|
2024-10-14 15:52:52 +03:00
|
|
|
* @param string $path
|
|
|
|
* @return bool
|
2024-10-09 16:42:20 +03:00
|
|
|
*/
|
2024-10-14 15:52:52 +03:00
|
|
|
public function installModule(string $path): bool
|
2024-10-09 16:42:20 +03:00
|
|
|
{
|
|
|
|
$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();
|
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-10-09 16:42:20 +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-09 16:42:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
|
|
|
|
$manifest = Manifest::getWithVars($manifestJson);
|
|
|
|
|
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
|
|
|
|
2024-10-14 15:52:52 +03:00
|
|
|
return true;
|
2024-10-09 16:42:20 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2024-10-09 16:42:20 +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']);
|
2024-10-09 16:42:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-14 15:52:52 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-10-09 16:42:20 +03:00
|
|
|
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/');
|
|
|
|
if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleName)){
|
|
|
|
$fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mkdir($tmpModuleDirFull . 'kernel/');
|
|
|
|
}
|
2024-10-09 16:42:20 +03:00
|
|
|
|
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-09 16:42:20 +03:00
|
|
|
|
2024-10-25 16:36:48 +03:00
|
|
|
//$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
2024-10-09 16:42:20 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
|
|
|
|
$manifest = Manifest::getWithVars($manifestJson);
|
|
|
|
|
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
$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-09-12 16:01:04 +03:00
|
|
|
}
|