diff --git a/kernel/manifest.json b/kernel/manifest.json index c621680..e0897f6 100755 --- a/kernel/manifest.json +++ b/kernel/manifest.json @@ -1,6 +1,6 @@ { "name": "Kernel", - "version": "0.1.4", + "version": "0.1.5", "author": "ITGuild", "slug": "kernel", "type": "kernel", diff --git a/kernel/modules/themes/controllers/ThemeController.php b/kernel/modules/themes/controllers/ThemeController.php index fbc23a3..46a204a 100644 --- a/kernel/modules/themes/controllers/ThemeController.php +++ b/kernel/modules/themes/controllers/ThemeController.php @@ -3,18 +3,31 @@ namespace kernel\modules\themes\controllers; use DirectoryIterator; +use GuzzleHttp\Exception\GuzzleException; use JetBrains\PhpStorm\NoReturn; +use Josantonius\Session\Exceptions\HeadersSentException; +use Josantonius\Session\Exceptions\SessionNotStartedException; +use Josantonius\Session\Exceptions\SessionStartedException; +use Josantonius\Session\Exceptions\WrongSessionOptionException; +use Josantonius\Session\Facades\Session; use kernel\AdminController; use kernel\helpers\Debug; use kernel\models\Option; use kernel\Request; +use kernel\services\ModuleService; +use kernel\services\ThemeService; class ThemeController extends AdminController { + public ThemeService $themeService; + + public ModuleService $moduleService; protected function init(): void { parent::init(); $this->cgView->viewPath = KERNEL_MODULES_DIR . "/themes/views/"; + $this->themeService = new ThemeService(); + $this->moduleService = new ModuleService(); } public function actionIndex(): void @@ -62,10 +75,19 @@ class ThemeController extends AdminController $this->cgView->render("index.php", ['json' => json_encode($infoToTable, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)]); } + /** + * @throws HeadersSentException + * @throws WrongSessionOptionException + * @throws SessionStartedException + * @throws GuzzleException + * @throws SessionNotStartedException + */ #[NoReturn] public function actionActivate(): void { $request = new Request(); - $this->themeService->setActiveTheme($request->get("p")); + if(!$this->themeService->setActiveTheme($request->get("p"))){ + $this->redirect("/admin/settings/themes/", 302); + } $this->cgView->render("view.php", ['data' => $this->themeService->getThemeInfo($request->get("p"))]); } diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index ebace82..8c10564 100755 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -21,6 +21,7 @@ class ModuleService protected null|bool $serverAvailable = null; public ModuleShopService $moduleShopService; + public function __construct() { $this->moduleShopService = new ModuleShopService(); @@ -601,11 +602,41 @@ class ModuleService { $data = file_get_contents($templatePath); - foreach ($params as $key => $param){ + foreach ($params as $key => $param) { $data = str_replace("{" . $key . "}", $param, $data); } file_put_contents($filePath, $data); } + /** + * @throws GuzzleException + */ + public function recursiveActivateDependencies(array $dependencies): bool + { + $notActiveDependencies = []; + foreach ($dependencies as $depend) { + if (!$this->isInstall($depend)) { + $this->moduleShopService->installModule($depend); + if (!$this->setActiveModule($depend)) { + $notActiveDependencies[] = $depend; + } + } else { + if (!$this->isActive($depend)) { + if (!$this->setActiveModule($depend)) { + $notActiveDependencies[] = $depend; + } + } + } + } + + if ($notActiveDependencies) { + if (!$this->recursiveActivateDependencies($notActiveDependencies)) { + return false; + } + } + + return true; + } + } \ No newline at end of file diff --git a/kernel/services/ModuleShopService.php b/kernel/services/ModuleShopService.php index fcdffe3..85b3d51 100755 --- a/kernel/services/ModuleShopService.php +++ b/kernel/services/ModuleShopService.php @@ -88,4 +88,21 @@ class ModuleShopService return false; } + /** + * @param string $slug + * @return bool + * @throws GuzzleException + */ + public function existsInModuleShop(string $slug): bool + { + $modulesInfo = $this->getGroupedBySlugModules(); + foreach ($modulesInfo as $module) { + if ($module['slug'] === $slug) { + return true; + } + } + + return false; + } + } \ No newline at end of file diff --git a/kernel/services/ThemeService.php b/kernel/services/ThemeService.php index cc81e98..a93f33d 100644 --- a/kernel/services/ThemeService.php +++ b/kernel/services/ThemeService.php @@ -3,6 +3,7 @@ namespace kernel\services; use DirectoryIterator; +use Josantonius\Session\Facades\Session; use kernel\Flash; use kernel\helpers\Debug; use kernel\helpers\Files; @@ -22,6 +23,7 @@ class ThemeService protected ModuleShopService $moduleShopService; + public function __construct() { $this->option = new Option(); @@ -58,7 +60,6 @@ class ThemeService return $this->active_theme; } - /** * @param string $theme * @return bool @@ -69,27 +70,24 @@ class ThemeService $activeTheme = $this->option::where("key", "active_theme")->first(); $themeInfo = $this->getThemeInfo(getConst($theme)); - if (isset($themeInfo['dependence'])) { - $dependence_array = explode(',', $themeInfo['dependence']); - foreach ($dependence_array as $depend) { - if (!$this->moduleService->isInstall($depend)) { - if ($this->moduleService->isShopModule($depend)) { - $this->moduleShopService->installModule($depend); - } else { - Flash::setMessage("error", "Модуль не найден в IT Guild Framework Shop."); - return false; - } - } else { - if (!$this->moduleService->isActive($depend)) { - $this->moduleService->setActiveModule($depend); - } + $dependenceArray = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/get_all_dependencies/' . $themeInfo['slug']); + $dependenceArray = json_decode($dependenceArray->getBody()->getContents(), true); + + foreach ($dependenceArray as $depend) { + if (!$this->moduleService->isInstall($depend)) { + if (!$this->moduleShopService->existsInModuleShop($depend)) { + Flash::setMessage('error', "Модуль $depend не найден в IT Guild Framework Shop."); + return false; } } } + if (!$this->moduleService->recursiveActivateDependencies($dependenceArray)){ + return false; + } + $activeTheme->value = getConst($theme); $activeTheme->save(); - return true; }