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;
|
2024-10-09 16:42:20 +03:00
|
|
|
use kernel\helpers\Files;
|
2024-09-12 16:01:04 +03:00
|
|
|
use kernel\helpers\Manifest;
|
2024-11-18 15:55:43 +03:00
|
|
|
use kernel\helpers\RESTClient;
|
2024-12-18 15:24:25 +03:00
|
|
|
use kernel\helpers\Version;
|
2024-09-12 16:01:04 +03:00
|
|
|
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-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);
|
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-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
|
|
|
*/
|
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) {
|
2024-12-04 11:20:20 +03:00
|
|
|
if (!in_array(trim($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
|
|
|
|
*/
|
2024-09-23 12:50:50 +03:00
|
|
|
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'])) {
|
2024-09-23 12:50:50 +03:00
|
|
|
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
|
|
|
|
{
|
2024-11-19 12:27:52 +03:00
|
|
|
if (isset($mod_info['module_class'])) {
|
|
|
|
if (isset($mod_info['module_class_file'])) {
|
2024-09-23 12:50:50 +03:00
|
|
|
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
|
|
|
|
*/
|
2024-09-23 12:50:50 +03:00
|
|
|
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'])) {
|
2024-09-23 12:50:50 +03:00
|
|
|
$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) {
|
2024-11-14 01:55:04 +03:00
|
|
|
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
|
2024-10-09 16:42:20 +03:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
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-09 16:42:20 +03:00
|
|
|
}
|
|
|
|
|
2024-11-14 01:55:04 +03:00
|
|
|
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
|
2024-10-09 16:42:20 +03:00
|
|
|
$manifest = Manifest::getWithVars($manifestJson);
|
|
|
|
|
|
|
|
$fileHelper = new Files();
|
2024-11-14 01:55:04 +03:00
|
|
|
$fileHelper->copy_folder($tmpModuleDirFull . 'app', $manifest['app_module_path']);
|
2024-10-09 16:42:20 +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-09 16:42:20 +03:00
|
|
|
} else {
|
2024-11-14 01:55:04 +03:00
|
|
|
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
|
2024-10-09 16:42:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$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'])) {
|
2024-11-19 12:27:52 +03:00
|
|
|
$fileHelper->recursiveRemoveDir(APP_DIR . '/modules/' . $moduleInfo['slug']);
|
2024-10-09 16:42:20 +03:00
|
|
|
}
|
|
|
|
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/');
|
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-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-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-12 14:06:25 +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-11-18 15:55:43 +03:00
|
|
|
|
2024-12-02 16:25:28 +03:00
|
|
|
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
2024-12-17 12:23:45 +03:00
|
|
|
|
2024-12-02 16:25:28 +03:00
|
|
|
$mod_info = $this->getModuleInfoBySlug($slug);
|
2024-12-18 15:24:25 +03:00
|
|
|
|
|
|
|
$currentVersion = Version::getIntVersionByString($mod_info['version']);
|
2024-12-02 16:25:28 +03:00
|
|
|
foreach ($modules_info as $mod) {
|
2024-12-18 15:24:25 +03:00
|
|
|
$modVersion = Version::getIntVersionByString($mod['version']);
|
|
|
|
if ($mod['slug'] === $mod_info['slug'] && $modVersion <= $currentVersion) {
|
2024-12-02 16:25:28 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-11-18 15:55:43 +03:00
|
|
|
public function isKernelModule(string $slug): bool
|
|
|
|
{
|
2024-11-19 12:27:52 +03:00
|
|
|
$modules_info = $this->getKernelModules();
|
2024-11-18 15:55:43 +03:00
|
|
|
foreach ($modules_info as $mod) {
|
2024-11-19 12:27:52 +03:00
|
|
|
if ($mod['slug'] === $slug) {
|
|
|
|
return true;
|
2024-11-18 15:55:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:27:52 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-02 16:50:44 +03:00
|
|
|
public function isShopModule(string $slug): bool
|
|
|
|
{
|
2024-12-12 14:06:25 +03:00
|
|
|
if ($this->isServerAvailable()) {
|
2024-12-02 16:50:44 +03:00
|
|
|
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
|
|
|
|
2024-12-12 14:06:25 +03:00
|
|
|
if (!$this->issetModuleShopToken()) {
|
2024-12-04 15:36:11 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-02 16:50:44 +03:00
|
|
|
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
2024-12-17 12:23:45 +03:00
|
|
|
if (isset($modules_info)) {
|
|
|
|
$mod_info = $this->getModuleInfoBySlug($slug);
|
|
|
|
foreach ($modules_info as $mod) {
|
|
|
|
if ($mod['slug'] === $mod_info['slug']) {
|
|
|
|
return true;
|
|
|
|
}
|
2024-12-02 16:50:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-18 15:55:43 +03:00
|
|
|
}
|
|
|
|
|
2024-12-02 16:38:05 +03:00
|
|
|
public function isServerAvailable(): bool
|
|
|
|
{
|
2024-12-12 14:06:25 +03:00
|
|
|
if (null !== $this->serverAvailable) {
|
2024-12-02 16:38:05 +03:00
|
|
|
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-12-04 15:36:11 +03:00
|
|
|
public function issetModuleShopToken(): bool
|
|
|
|
{
|
2024-12-12 14:06:25 +03:00
|
|
|
if (!empty($_ENV['MODULE_SHOP_TOKEN'])) {
|
2024-12-04 15:36:11 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-10 15:28:34 +03:00
|
|
|
public function createDirs(string $slug): void
|
|
|
|
{
|
|
|
|
mkdir(KERNEL_APP_MODULES_DIR . "/$slug");
|
|
|
|
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers");
|
|
|
|
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/migrations");
|
2024-12-12 14:06:25 +03:00
|
|
|
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/services");
|
2024-12-10 15:28:34 +03:00
|
|
|
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models");
|
2024-12-10 17:01:25 +03:00
|
|
|
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms");
|
2024-12-10 15:28:34 +03:00
|
|
|
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs");
|
|
|
|
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views");
|
|
|
|
|
|
|
|
mkdir(APP_DIR . "/modules/$slug");
|
|
|
|
mkdir(APP_DIR . "/modules/$slug/controllers");
|
|
|
|
mkdir(APP_DIR . "/modules/$slug/routs");
|
|
|
|
}
|
|
|
|
|
2024-12-12 14:06:25 +03:00
|
|
|
public function createModuleByParams(array $params): void
|
2024-12-10 15:28:34 +03:00
|
|
|
{
|
2024-12-12 14:06:25 +03:00
|
|
|
$slug = $params['slug'];
|
|
|
|
$model = $params['model'];
|
2024-12-12 11:58:55 +03:00
|
|
|
|
2024-12-12 14:06:25 +03:00
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/manifests/manifest_template', APP_DIR . "/modules/$slug/manifest.json", $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/kernel_controller_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/app_controller_template', APP_DIR . '/modules/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/kernel_routs_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/routs/' . $slug . '.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/app_routs_template', APP_DIR . '/modules/' . $slug . '/routs/' . $slug . '.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/kernel_module_file_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/' . $model . 'Module.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/app_module_file_template', APP_DIR . '/modules/' . $slug . '/' . $model . 'Module.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/model_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/' . $model . '.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/forms/create_form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/forms/Create' . $model . 'Form.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/services/service_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/services/' . $model . 'Service.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/index_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/index.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/view_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/view.php', $params);
|
|
|
|
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/form.php', $params);
|
2024-12-12 11:58:55 +03:00
|
|
|
}
|
|
|
|
|
2024-12-12 14:06:25 +03:00
|
|
|
public function createModuleFileByTemplate(string $templatePath, string $filePath, array $params): void
|
2024-12-12 11:58:55 +03:00
|
|
|
{
|
2024-12-12 14:06:25 +03:00
|
|
|
$data = file_get_contents($templatePath);
|
2024-12-12 11:58:55 +03:00
|
|
|
|
2024-12-12 15:04:45 +03:00
|
|
|
foreach ($params as $key => $param){
|
|
|
|
$data = str_replace("{" . $key . "}", $param, $data);
|
|
|
|
}
|
2024-12-12 11:58:55 +03:00
|
|
|
|
2024-12-12 14:06:25 +03:00
|
|
|
file_put_contents($filePath, $data);
|
2024-12-12 11:58:55 +03:00
|
|
|
}
|
|
|
|
|
2024-09-12 16:01:04 +03:00
|
|
|
}
|