MicroFrameWork/kernel/services/ModuleService.php

75 lines
2.1 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;
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;
if (file_exists($module . "/manifest.json")){
$manifest = file_get_contents($module . "/manifest.json");
$manifest = Manifest::getWithVars($manifest);
$info = array_merge($info, $manifest);
}
return $info;
}
public function isActive(string $slug): bool
{
2024-09-13 13:54:58 +03:00
$active_modules = Option::where("key", "active_modules")->first();
2024-09-12 16:01:04 +03:00
if ($active_modules){
$path = json_decode($active_modules->value);
foreach ($path->modules as $p){
if ($p === $slug){
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);
} else {
$path->modules[] = $module;
}
$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 = [];
if ($module_paths){
$path = json_decode($module_paths->value);
foreach ($path->paths as $p){
$dirs[] = getConst($p);
}
}
foreach ($dirs as $dir){
foreach (new DirectoryIterator($dir) as $fileInfo) {
if(basename($fileInfo->getPathname()) !== $slug) continue;
return $fileInfo->getPathname();
}
}
return false;
}
2024-09-12 16:01:04 +03:00
}