This commit is contained in:
Билай Станислав 2025-01-24 14:28:07 +03:00
parent 7ab241daa2
commit e904cedf40
2 changed files with 72 additions and 8 deletions

View File

@ -2,7 +2,7 @@
namespace kernel\app_themes\custom\services;
class CustomService
class CustomThemeService
{
}

View File

@ -86,11 +86,40 @@ class ThemeService
return false;
}
$this->runDeactivateScript($this->getActiveThemeInfo());
$this->runInitScript($themeInfo);
$activeTheme->value = getConst($theme);
$activeTheme->save();
return true;
}
public function runInitScript($themeInfo): void
{
if (isset($themeInfo['theme_class'])) {
if (isset($themeInfo['theme_class_file'])) {
require_once $themeInfo['theme_class_file'];
}
$themeClass = new $themeInfo['theme_class']();
$themeClass->init();
}
}
/**
* @param $themeInfo
* @return void
*/
public function runDeactivateScript($themeInfo): void
{
if (isset($themeInfo['theme_class'])) {
if (isset($themeInfo['theme_class_file'])) {
require_once $themeInfo['theme_class_file'];
}
$themeClass = new $themeInfo['theme_class']();
$themeClass->deactivate();
}
}
public function getActiveThemeInfo(): false|array|string
{
return $this->getThemeInfo($this->active_theme);
@ -179,7 +208,8 @@ class ThemeService
$tmpThemeDirFull = RESOURCES_DIR . '/tmp/ad/' . $themeName . "/";
$fileHelper = new Files();
$fileHelper->copy_folder(ROOT_DIR . $path, $tmpThemeDirFull . 'meta/');
$fileHelper->copy_folder(ROOT_DIR . $path, $tmpThemeDirFull . 'meta/app/');
$fileHelper->copy_folder(KERNEL_DIR . '/app_themes/' . $themeName, $tmpThemeDirFull . 'meta/kernel/');
$fileHelper->copy_folder(RESOURCES_DIR . '/themes/' . $themeName, $tmpThemeDirFull . 'resources/');
if (!is_dir(RESOURCES_DIR . '/tmp/themes')) {
@ -213,10 +243,16 @@ class ThemeService
$manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files();
if (isset($manifest['theme_path'])) {
$fileHelper->copy_folder($tmpThemeDirFull . "meta", $manifest['theme_path']);
if (isset($manifest['theme_app_path'])) {
$fileHelper->copy_folder($tmpThemeDirFull . "meta/app", $manifest['theme_app_path']);
} else {
$fileHelper->copy_folder($tmpThemeDirFull . "meta", APP_DIR . '/themes/' . $manifest['slug']);
$fileHelper->copy_folder($tmpThemeDirFull . "meta/app", APP_DIR . '/themes/' . $manifest['slug']);
}
if (isset($manifest['theme_kernel_path'])) {
$fileHelper->copy_folder($tmpThemeDirFull . "meta/kernel", $manifest['theme_app_path']);
} else {
$fileHelper->copy_folder($tmpThemeDirFull . "meta/kernel", APP_DIR . '/themes/' . $manifest['slug']);
}
if (isset($manifest['resource_path'])) {
@ -242,6 +278,9 @@ class ThemeService
if (file_exists($path)) {
$fileHelper->recursiveRemoveDir($path);
}
if (file_exists(KERNEL_DIR . '/app_themes/' . $themeInfo['slug'])) {
$fileHelper->recursiveRemoveDir(KERNEL_DIR . '/app_themes/' . $themeInfo['slug']);
}
if (file_exists(RESOURCES_DIR . '/themes/' . $themeInfo['slug'])) {
$fileHelper->recursiveRemoveDir(RESOURCES_DIR . '/themes/' . $themeInfo['slug']);
}
@ -249,13 +288,38 @@ class ThemeService
public function update(string $path): bool
{
if ($this->install($path)) {
return true;
$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_kernel_path'])) {
$fileHelper->copy_folder($tmpThemeDirFull . "meta/kernel", $manifest['theme_app_path']);
} else {
$fileHelper->copy_folder($tmpThemeDirFull . "meta/kernel", APP_DIR . '/themes/' . $manifest['slug']);
}
$fileHelper->recursiveRemoveDir($tmpThemeDirFull);
unlink(ROOT_DIR . $path);
return true;
}
public function getThemeRout(string $path)
{
if (file_exists($path . "/manifest.json")){