MicroFrameWork/kernel/services/AdminThemeService.php

205 lines
6.2 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;
2024-12-23 14:14:59 +03:00
use kernel\helpers\Files;
2024-09-11 17:38:46 +03:00
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;
2024-12-23 14:14:59 +03:00
use ZipArchive;
2024-09-03 16:29:44 +03:00
class AdminThemeService
{
2024-12-23 14:14:59 +03:00
protected array $errors = [];
2024-09-03 16:29:44 +03:00
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
}
2024-12-23 14:14:59 +03:00
/**
* @return array
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* @param $msg
* @return void
*/
public function addError($msg): void
{
$this->errors[] = $msg;
}
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;
2024-12-23 14:14:59 +03:00
if (file_exists($theme . "/manifest.json")) {
2024-09-03 16:29:44 +03:00
$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-23 14:14:59 +03:00
public function getAdminThemeInfoBySlug(string $slug)
{
// TODO
}
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()) {
2024-12-23 14:14:59 +03:00
$modulesInfo = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
2024-12-18 12:27:29 +03:00
2024-12-23 14:14:59 +03:00
$modulesInfo = json_decode($modulesInfo->getBody()->getContents(), true);
2024-12-18 12:27:29 +03:00
$themeInfo = $this->getAdminThemeInfo($slug);
2024-12-23 15:47:03 +03:00
// Debug::dd($themeInfo);
2024-12-23 14:14:59 +03:00
foreach ($modulesInfo as $mod) {
2024-12-18 12:27:29 +03:00
if ($mod['slug'] === $themeInfo['slug'] && $mod['version'] === $themeInfo['version']) {
return true;
}
}
}
return false;
}
2024-12-23 14:14:59 +03:00
public function pack(string $path): void
{
$themeName = basename($path);
$tmpThemeDirFull = RESOURCES_DIR . '/tmp/ad/' . $themeName . "/";
$fileHelper = new Files();
$fileHelper->copy_folder(ROOT_DIR . $path, $tmpThemeDirFull . 'meta/');
$fileHelper->copy_folder(RESOURCES_DIR . '/' . $themeName, $tmpThemeDirFull . 'resources/');
if (!is_dir(RESOURCES_DIR . '/tmp/admin_themes')) {
$old_mask = umask(0);
mkdir(RESOURCES_DIR . '/tmp/admin_themes', 0775, true);
umask($old_mask);
}
$fileHelper->pack($tmpThemeDirFull, RESOURCES_DIR . '/tmp/admin_themes/' . $themeName . '.igt');
}
public function install(string $path): bool
{
$zip = new ZipArchive;
$tmpThemeDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
$tmpThemeDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpThemeDir . "/";
$zip->extractTo($tmpThemeDirFull);
$zip->close();
} else {
$this->addError('unable to open zip archive');
return false;
}
if (!file_exists($tmpThemeDirFull . "meta/manifest.json")){
$this->addError('manifest.json not found');
return false;
}
$manifestJson = getConst(file_get_contents($tmpThemeDirFull . "meta/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files();
if (isset($manifest['theme_path'])) {
$fileHelper->copy_folder($tmpThemeDirFull . "meta", $manifest['theme_path']);
} else {
$fileHelper->copy_folder($tmpThemeDirFull . "meta", APP_DIR . '/admin_themes/' . $manifest['slug']);
}
if (isset($manifest['resource_path'])) {
$fileHelper->copy_folder($tmpThemeDirFull . "resources", $manifest['resource_path']);
} else {
$fileHelper->copy_folder($tmpThemeDirFull . "resources", RESOURCES_DIR . '/' . $manifest['slug']);
}
$fileHelper->recursiveRemoveDir($tmpThemeDirFull);
unlink(ROOT_DIR . $path);
return true;
}
public function uninstall(string $path): void
{
$themeInfo = $this->getAdminThemeInfo(APP_DIR . '/admin_themes/' . basename($path));
$active_admin_theme = Option::where("key", "active_admin_theme")->first();
if ($active_admin_theme->value === ROOT_DIR . $path) {
$this->setActiveAdminTheme(KERNEL_ADMIN_THEMES_DIR . '/default');
}
$fileHelper = new Files();
if (file_exists(ROOT_DIR . $path)) {
$fileHelper->recursiveRemoveDir(ROOT_DIR . $path);
}
if (file_exists(RESOURCES_DIR . '/' . $themeInfo['slug'])) {
$fileHelper->recursiveRemoveDir(RESOURCES_DIR . '/' . $themeInfo['slug']);
}
}
2024-09-03 16:29:44 +03:00
}