igmf/kernel/services/ThemeService.php
2025-01-16 16:14:43 +03:00

118 lines
2.9 KiB
PHP

<?php
namespace kernel\services;
use DirectoryIterator;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\models\Option;
class ThemeService
{
protected array $errors = [];
protected Option $option;
protected string $active_theme = "";
public function __construct()
{
$this->option = new Option();
$this->findActiveTheme();
}
/**
* @return array
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* @param $msg
* @return void
*/
public function addError($msg): void
{
$this->errors[] = $msg;
}
public function findActiveTheme(): void
{
$model = $this->option::where("key", "active_theme")->first();
$this->active_theme = $model->value;
}
public function getActiveTheme(): string
{
return $this->active_theme;
}
public function setActiveTheme(string $theme): void
{
$activeTheme = Option::where("key", "active_theme")->first();
$activeTheme->value = getConst($theme);
$activeTheme->save();
}
public function getActiveThemeInfo(): false|array|string
{
return $this->getThemeInfo($this->active_theme);
}
public function getThemeInfo(string $theme): false|array|string
{
$info = [];
$theme = getConst($theme);
$info['path'] = $theme;
if (file_exists($theme . "/manifest.json")) {
$manifest = file_get_contents($theme . "/manifest.json");
$manifest = Manifest::getWithVars($manifest);
$manifest['preview'] = $manifest['resource'] . "/" . $manifest['preview'];
$info = array_merge($info, $manifest);
}
return $info;
}
public function getThemeInfoBySlug(string $slug): false|array|string
{
$dirs = $this->getThemeDirs();
foreach ($dirs as $dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->isDot()) continue;
if ($this->getThemeInfo($fileInfo->getPathname())['slug'] === $slug) {
return $this->getThemeInfo($fileInfo->getPathname());
}
}
}
return false;
}
public function getThemeDirs(): array
{
$ThemePaths = Option::where("key", "theme_paths")->first();
$dirs = [];
if ($ThemePaths) {
$path = json_decode($ThemePaths->value);
foreach ($path->paths as $p) {
$dirs[] = getConst($p);
}
}
return $dirs;
}
public function getThemeRout(string $path)
{
if (file_exists($path . "/manifest.json")){
$manifest = json_decode(file_get_contents($path . "/manifest.json"), true);
if ($manifest['routs']) {
return $manifest['routs'];
}
}
return false;
}
}