Theme module and them to igfs add

This commit is contained in:
2025-01-19 21:23:53 +03:00
parent dac4db96af
commit 50c6ca98d8
12 changed files with 315 additions and 17 deletions

View File

@ -3,6 +3,7 @@
namespace kernel\services;
use DirectoryIterator;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\models\Option;
@ -14,10 +15,14 @@ class ThemeService
protected Option $option;
protected string $active_theme = "";
protected ModuleService $moduleService;
public function __construct()
{
$this->option = new Option();
$this->findActiveTheme();
$this->moduleService = new ModuleService();
}
/**
@ -103,6 +108,123 @@ class ThemeService
return $dirs;
}
public function isInstall(string $slug): bool
{
$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 true;
}
}
}
return false;
}
public function isLastVersion(string $slug): bool
{
if ($this->moduleService->isServerAvailable()) {
$modulesInfo = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modulesInfo = json_decode($modulesInfo->getBody()->getContents(), true);
$themeInfo = $this->getThemeInfoBySlug($slug);
foreach ($modulesInfo as $mod) {
if ($mod['slug'] === $themeInfo['slug'] && $mod['version'] === $themeInfo['version']) {
return true;
}
}
}
return false;
}
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 . '/themes/' . $themeName, $tmpThemeDirFull . 'resources/');
if (!is_dir(RESOURCES_DIR . '/tmp/themes')) {
$old_mask = umask(0);
mkdir(RESOURCES_DIR . '/tmp/themes', 0775, true);
umask($old_mask);
}
$fileHelper->pack($tmpThemeDirFull, RESOURCES_DIR . '/tmp/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 . '/themes/' . $manifest['slug']);
}
if (isset($manifest['resource_path'])) {
$fileHelper->copy_folder($tmpThemeDirFull . "resources", $manifest['resource_path']);
} else {
$fileHelper->copy_folder($tmpThemeDirFull . "resources", RESOURCES_DIR . '/themes/' . $manifest['slug']);
}
$fileHelper->recursiveRemoveDir($tmpThemeDirFull);
unlink(ROOT_DIR . $path);
return true;
}
public function uninstall(string $path): void
{
$themeInfo = $this->getThemeInfo(APP_DIR . '/themes/' . basename($path));
$active_theme = $this->option::where("key", "active_theme")->first();
if ($active_theme->value === ROOT_DIR . $path) {
$this->setActiveTheme(KERNEL_DIR . '/themes/default');
}
$fileHelper = new Files();
if (file_exists($path)) {
$fileHelper->recursiveRemoveDir($path);
}
if (file_exists(RESOURCES_DIR . '/themes/' . $themeInfo['slug'])) {
$fileHelper->recursiveRemoveDir(RESOURCES_DIR . '/themes/' . $themeInfo['slug']);
}
}
public function update(string $path): bool
{
if ($this->install($path)) {
return true;
}
return false;
}
public function getThemeRout(string $path)
{
if (file_exists($path . "/manifest.json")){