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-09-12 16:01:04 +03:00
|
|
|
use kernel\helpers\Manifest;
|
|
|
|
use kernel\models\Option;
|
|
|
|
|
|
|
|
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);
|
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-09-23 12:50:50 +03:00
|
|
|
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-09-13 13:54:58 +03:00
|
|
|
public function setActiveModule(string $module): void
|
|
|
|
{
|
|
|
|
$active_modules = Option::where("key", "active_modules")->first();
|
|
|
|
$path = json_decode($active_modules->value);
|
|
|
|
if (in_array($module, $path->modules)) {
|
|
|
|
unset($path->modules[array_search($module, $path->modules)]);
|
|
|
|
$path->modules = array_values($path->modules);
|
2024-09-23 12:50:50 +03:00
|
|
|
$this->runDeactivateScript($this->getModuleInfoBySlug($module));
|
2024-09-13 13:54:58 +03:00
|
|
|
} else {
|
|
|
|
$path->modules[] = $module;
|
2024-09-23 12:50:50 +03:00
|
|
|
$this->runInitScript($this->getModuleInfoBySlug($module));
|
2024-09-13 13:54:58 +03:00
|
|
|
}
|
|
|
|
$active_modules->value = json_encode($path, JSON_UNESCAPED_UNICODE);
|
|
|
|
$active_modules->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;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-09-12 16:01:04 +03:00
|
|
|
}
|