PhyrePanel-mirror/web/app/ModulesManager.php

87 lines
2.3 KiB
PHP
Raw Normal View History

2024-04-26 20:09:04 +00:00
<?php
namespace App;
2024-04-26 20:26:05 +00:00
use App\Events\ModuleIsInstalled;
2024-04-26 20:09:04 +00:00
use App\Models\Module;
class ModulesManager
{
public static function getModules()
{
$scanModules = scandir(base_path('Modules'));
$scanModules = array_diff($scanModules, ['.', '..']);
$modules = [];
foreach ($scanModules as $key => $module) {
$moduleInfo = self::getModuleInfo($module);
if (empty($moduleInfo)) {
continue;
}
$modules[] = $moduleInfo;
}
return $modules;
}
public static function getModuleInfo($module)
{
if (!is_dir(base_path('Modules/' . $module))) {
2024-04-29 08:04:33 +00:00
return [];
2024-04-26 20:09:04 +00:00
}
$moduleJson = file_get_contents(base_path('Modules/' . $module . '/module.json'));
$moduleJson = json_decode($moduleJson, true);
if (isset($moduleJson['hidden']) && $moduleJson['hidden'] == true) {
return [];
}
$category = 'All';
$logoIcon = 'heroicon-o-puzzle-piece';
if (isset($moduleJson['logoIcon'])) {
$logoIcon = $moduleJson['logoIcon'];
}
if (isset($moduleJson['category'])) {
$category = $moduleJson['category'];
}
2024-04-26 20:10:19 +00:00
$adminUrl = '';
if (isset($moduleJson['adminUrl'])) {
$adminUrl = $moduleJson['adminUrl'];
}
2024-04-26 20:09:04 +00:00
$url = '';
$installed = 0;
$findModule = Module::where('name', $module)->first();
if ($findModule) {
$installed = 1;
}
return [
'name' => $module,
'description' => 'A drag and drop website builder and a powerful next-generation CMS.',
'url' => $url,
2024-04-26 20:10:19 +00:00
'adminUrl' => $adminUrl,
2024-04-26 20:09:04 +00:00
'iconUrl' => url('images/modules/' . $module . '.png'),
'logoIcon' => $logoIcon,
'category' => $category,
'installed'=>$installed,
];
}
public static function saveInstalledModule($module)
{
2024-04-26 20:36:26 +00:00
$findModule = Module::where('name', $module)->first();
if ($findModule) {
return;
}
2024-04-26 20:09:04 +00:00
$newModule = new Module();
$newModule->name = $module;
$newModule->namespace = 'Modules\\' . $module;
$newModule->installed = 1;
$newModule->save();
2024-04-26 20:26:05 +00:00
event(new ModuleIsInstalled($newModule));
2024-04-26 20:09:04 +00:00
}
}