From e904cedf40448f652757a288423fe791d7e33c85 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 24 Jan 2025 14:28:07 +0300 Subject: [PATCH] some --- ...stomService.php => CustomThemeService.php} | 2 +- kernel/services/ThemeService.php | 78 +++++++++++++++++-- 2 files changed, 72 insertions(+), 8 deletions(-) rename kernel/app_themes/custom/services/{CustomService.php => CustomThemeService.php} (68%) diff --git a/kernel/app_themes/custom/services/CustomService.php b/kernel/app_themes/custom/services/CustomThemeService.php similarity index 68% rename from kernel/app_themes/custom/services/CustomService.php rename to kernel/app_themes/custom/services/CustomThemeService.php index f7a9fbd..44d01f3 100644 --- a/kernel/app_themes/custom/services/CustomService.php +++ b/kernel/app_themes/custom/services/CustomThemeService.php @@ -2,7 +2,7 @@ namespace kernel\app_themes\custom\services; -class CustomService +class CustomThemeService { } \ No newline at end of file diff --git a/kernel/services/ThemeService.php b/kernel/services/ThemeService.php index a93f33d..2f79738 100644 --- a/kernel/services/ThemeService.php +++ b/kernel/services/ThemeService.php @@ -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,11 +288,36 @@ 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; } - 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)