MicroFrameWork/kernel/services/AdminThemeService.php

102 lines
2.9 KiB
PHP
Raw Normal View History

2024-09-03 16:29:44 +03:00
<?php
namespace kernel\services;
2024-12-18 12:27:29 +03:00
use DirectoryIterator;
2024-09-11 17:38:46 +03:00
use kernel\helpers\Debug;
use kernel\helpers\Manifest;
2024-12-18 12:27:29 +03:00
use kernel\helpers\RESTClient;
2024-09-03 16:29:44 +03:00
use kernel\models\Option;
class AdminThemeService
{
protected Option $option;
protected string $active_theme;
2024-12-18 12:27:29 +03:00
protected ModuleService $moduleService;
2024-09-03 16:29:44 +03:00
public function __construct()
{
$this->option = new Option();
$this->findActiveAdminTheme();
2024-12-18 12:27:29 +03:00
$this->moduleService = new ModuleService();
2024-09-03 16:29:44 +03:00
}
public function findActiveAdminTheme(): void
{
$model = Option::where("key", "active_admin_theme")->first();
$this->active_theme = $model->value;
}
public function getActiveAdminTheme(): string
{
return $this->active_theme;
}
public function setActiveAdminTheme(string $theme): void
{
$active_admin_theme = Option::where("key", "active_admin_theme")->first();
2024-09-24 14:57:25 +03:00
$active_admin_theme->value = getConst($theme);
2024-09-03 16:29:44 +03:00
$active_admin_theme->save();
}
public function getActiveAdminThemeInfo(): false|array|string
{
return $this->getAdminThemeInfo($this->active_theme);
}
public function getAdminThemeInfo(string $theme): false|array|string
{
$info = [];
2024-09-24 14:57:25 +03:00
$theme = getConst($theme);
2024-09-03 16:29:44 +03:00
$info['path'] = $theme;
if (file_exists($theme . "/manifest.json")){
$manifest = file_get_contents($theme . "/manifest.json");
2024-09-11 17:38:46 +03:00
$manifest = Manifest::getWithVars($manifest);
2024-09-03 16:29:44 +03:00
$manifest['preview'] = $manifest['resource'] . "/" . $manifest['preview'];
$info = array_merge($info, $manifest);
}
return $info;
}
2024-12-18 12:27:29 +03:00
public function isInstall(string $slug): bool
{
$adminThemePaths = Option::where("key", "admin_theme_paths")->first();
$dirs = [];
if ($adminThemePaths) {
$path = json_decode($adminThemePaths->value);
foreach ($path->paths as $p) {
$dirs[] = getConst($p);
}
}
foreach ($dirs as $dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->isDot()) continue;
if ($this->getAdminThemeInfo($fileInfo->getPathname())['slug'] === $slug) {
return true;
}
}
}
return false;
}
public function isLastVersion(string $slug): bool
{
if ($this->moduleService->isServerAvailable()) {
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$themeInfo = $this->getAdminThemeInfo($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $themeInfo['slug'] && $mod['version'] === $themeInfo['version']) {
return true;
}
}
}
return false;
}
2024-09-03 16:29:44 +03:00
}