kernel to v0.1.4 update
This commit is contained in:
@ -16,6 +16,7 @@ class AdminThemeService
|
||||
|
||||
protected Option $option;
|
||||
protected string $active_theme;
|
||||
|
||||
protected ModuleService $moduleService;
|
||||
|
||||
|
||||
@ -82,12 +83,22 @@ class AdminThemeService
|
||||
return $info;
|
||||
}
|
||||
|
||||
public function getAdminThemeInfoBySlug(string $slug)
|
||||
public function getAdminThemeInfoBySlug(string $slug): false|array|string
|
||||
{
|
||||
// TODO
|
||||
$dirs = $this->getAdminThemeDirs();
|
||||
foreach ($dirs as $dir) {
|
||||
foreach (new DirectoryIterator($dir) as $fileInfo) {
|
||||
if ($fileInfo->isDot()) continue;
|
||||
if ($this->getAdminThemeInfo($fileInfo->getPathname())['slug'] === $slug) {
|
||||
return $this->getAdminThemeInfo($fileInfo->getPathname());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isInstall(string $slug): bool
|
||||
public function getAdminThemeDirs(): array
|
||||
{
|
||||
$adminThemePaths = Option::where("key", "admin_theme_paths")->first();
|
||||
$dirs = [];
|
||||
@ -97,6 +108,12 @@ class AdminThemeService
|
||||
$dirs[] = getConst($p);
|
||||
}
|
||||
}
|
||||
return $dirs;
|
||||
}
|
||||
|
||||
public function isInstall(string $slug): bool
|
||||
{
|
||||
$dirs = $this->getAdminThemeDirs();
|
||||
foreach ($dirs as $dir) {
|
||||
foreach (new DirectoryIterator($dir) as $fileInfo) {
|
||||
if ($fileInfo->isDot()) continue;
|
||||
@ -116,8 +133,7 @@ class AdminThemeService
|
||||
|
||||
$modulesInfo = json_decode($modulesInfo->getBody()->getContents(), true);
|
||||
|
||||
$themeInfo = $this->getAdminThemeInfo($slug);
|
||||
// Debug::dd($themeInfo);
|
||||
$themeInfo = $this->getAdminThemeInfoBySlug($slug);
|
||||
foreach ($modulesInfo as $mod) {
|
||||
if ($mod['slug'] === $themeInfo['slug'] && $mod['version'] === $themeInfo['version']) {
|
||||
return true;
|
||||
@ -136,7 +152,7 @@ class AdminThemeService
|
||||
|
||||
$fileHelper = new Files();
|
||||
$fileHelper->copy_folder(ROOT_DIR . $path, $tmpThemeDirFull . 'meta/');
|
||||
$fileHelper->copy_folder(RESOURCES_DIR . '/' . $themeName, $tmpThemeDirFull . 'resources/');
|
||||
$fileHelper->copy_folder(RESOURCES_DIR . '/admin_themes/' . $themeName, $tmpThemeDirFull . 'resources/');
|
||||
|
||||
if (!is_dir(RESOURCES_DIR . '/tmp/admin_themes')) {
|
||||
$old_mask = umask(0);
|
||||
@ -178,7 +194,7 @@ class AdminThemeService
|
||||
if (isset($manifest['resource_path'])) {
|
||||
$fileHelper->copy_folder($tmpThemeDirFull . "resources", $manifest['resource_path']);
|
||||
} else {
|
||||
$fileHelper->copy_folder($tmpThemeDirFull . "resources", RESOURCES_DIR . '/' . $manifest['slug']);
|
||||
$fileHelper->copy_folder($tmpThemeDirFull . "resources", RESOURCES_DIR . '/admin_themes/' . $manifest['slug']);
|
||||
}
|
||||
|
||||
$fileHelper->recursiveRemoveDir($tmpThemeDirFull);
|
||||
@ -195,11 +211,20 @@ class AdminThemeService
|
||||
$this->setActiveAdminTheme(KERNEL_ADMIN_THEMES_DIR . '/default');
|
||||
}
|
||||
$fileHelper = new Files();
|
||||
if (file_exists(ROOT_DIR . $path)) {
|
||||
$fileHelper->recursiveRemoveDir(ROOT_DIR . $path);
|
||||
if (file_exists($path)) {
|
||||
$fileHelper->recursiveRemoveDir($path);
|
||||
}
|
||||
if (file_exists(RESOURCES_DIR . '/' . $themeInfo['slug'])) {
|
||||
$fileHelper->recursiveRemoveDir(RESOURCES_DIR . '/' . $themeInfo['slug']);
|
||||
if (file_exists(RESOURCES_DIR . '/admin_themes/' . $themeInfo['slug'])) {
|
||||
$fileHelper->recursiveRemoveDir(RESOURCES_DIR . '/admin_themes/' . $themeInfo['slug']);
|
||||
}
|
||||
}
|
||||
|
||||
public function update(string $path): bool
|
||||
{
|
||||
if ($this->install($path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace kernel\services;
|
||||
|
||||
use DirectoryIterator;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
@ -19,6 +20,12 @@ class ModuleService
|
||||
|
||||
protected null|bool $serverAvailable = null;
|
||||
|
||||
public ModuleShopService $moduleShopService;
|
||||
public function __construct()
|
||||
{
|
||||
$this->moduleShopService = new ModuleShopService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $module
|
||||
* @return false|array|string
|
||||
@ -458,12 +465,13 @@ class ModuleService
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function isLastVersion(string $slug): bool
|
||||
{
|
||||
if ($this->isServerAvailable()) {
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
$modules_info = $this->moduleShopService->getGroupedBySlugModules();
|
||||
|
||||
$mod_info = $this->getModuleInfoBySlug($slug);
|
||||
|
||||
@ -491,16 +499,18 @@ class ModuleService
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function isShopModule(string $slug): bool
|
||||
{
|
||||
if ($this->isServerAvailable()) {
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
$modules_info = $this->moduleShopService->getGroupedBySlugModules();
|
||||
|
||||
if (!$this->issetModuleShopToken()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
if (isset($modules_info)) {
|
||||
$mod_info = $this->getModuleInfoBySlug($slug);
|
||||
foreach ($modules_info as $mod) {
|
||||
|
@ -3,7 +3,11 @@
|
||||
namespace kernel\services;
|
||||
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Files;
|
||||
use kernel\helpers\RESTClient;
|
||||
use kernel\Request;
|
||||
|
||||
class ModuleShopService
|
||||
{
|
||||
@ -17,20 +21,71 @@ class ModuleShopService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @return mixed
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function email_auth(string $email)
|
||||
public function email_auth(string $email): mixed
|
||||
{
|
||||
$request = RESTClient::post($this->url . "/api/secure/email_auth", ['email' => $email], false);
|
||||
|
||||
return json_decode($request->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
public function code_check(string $code)
|
||||
/**
|
||||
* @param string $code
|
||||
* @return mixed
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function code_check(string $code): mixed
|
||||
{
|
||||
$request = RESTClient::post($this->url . "/api/secure/code_check", ['code' => $code], false);
|
||||
|
||||
return json_decode($request->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getGroupedBySlugModules(): mixed
|
||||
{
|
||||
$modulesInfo = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
return json_decode($modulesInfo->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return void
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function installModule(string $slug): void
|
||||
{
|
||||
$moduleInfo = $this->getModuleInfoBySlug($slug);
|
||||
|
||||
$moduleInfo = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $moduleInfo['id']);
|
||||
$moduleInfo = json_decode($moduleInfo->getBody()->getContents(), true);
|
||||
|
||||
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $moduleInfo['path_to_archive'], RESOURCES_DIR . "/tmp/modules");
|
||||
|
||||
(new ModuleService())->installModule('/resources/tmp/modules/' . basename($moduleInfo['path_to_archive']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return false|mixed
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getModuleInfoBySlug(string $slug): mixed
|
||||
{
|
||||
$modulesInfo = $this->getGroupedBySlugModules();
|
||||
foreach ($modulesInfo as $module) {
|
||||
if ($module['slug'] === $slug) {
|
||||
return $module;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
273
kernel/services/ThemeService.php
Normal file
273
kernel/services/ThemeService.php
Normal file
@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\services;
|
||||
|
||||
use DirectoryIterator;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Files;
|
||||
use kernel\helpers\Manifest;
|
||||
use kernel\helpers\RESTClient;
|
||||
use kernel\models\Option;
|
||||
use ZipArchive;
|
||||
|
||||
class ThemeService
|
||||
{
|
||||
protected array $errors = [];
|
||||
|
||||
protected Option $option;
|
||||
protected string $active_theme = "";
|
||||
|
||||
protected ModuleService $moduleService;
|
||||
|
||||
protected ModuleShopService $moduleShopService;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->option = new Option();
|
||||
$this->findActiveTheme();
|
||||
$this->moduleService = new ModuleService();
|
||||
$this->moduleShopService = new ModuleShopService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors(): array
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $msg
|
||||
* @return void
|
||||
*/
|
||||
public function addError($msg): void
|
||||
{
|
||||
$this->errors[] = $msg;
|
||||
}
|
||||
|
||||
public function findActiveTheme(): void
|
||||
{
|
||||
$model = $this->option::where("key", "active_theme")->first();
|
||||
$this->active_theme = $model->value;
|
||||
}
|
||||
|
||||
public function getActiveTheme(): string
|
||||
{
|
||||
return $this->active_theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $theme
|
||||
* @return bool
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function setActiveTheme(string $theme): bool
|
||||
{
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$activeTheme->value = getConst($theme);
|
||||
$activeTheme->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getActiveThemeInfo(): false|array|string
|
||||
{
|
||||
return $this->getThemeInfo($this->active_theme);
|
||||
}
|
||||
|
||||
public function getThemeInfo(string $theme): false|array|string
|
||||
{
|
||||
$info = [];
|
||||
$theme = getConst($theme);
|
||||
$info['path'] = $theme;
|
||||
if (file_exists($theme . "/manifest.json")) {
|
||||
$manifest = file_get_contents($theme . "/manifest.json");
|
||||
$manifest = Manifest::getWithVars($manifest);
|
||||
$manifest['preview'] = $manifest['resource'] . "/" . $manifest['preview'];
|
||||
$info = array_merge($info, $manifest);
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
public function getThemeInfoBySlug(string $slug): false|array|string
|
||||
{
|
||||
$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 $this->getThemeInfo($fileInfo->getPathname());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getThemeDirs(): array
|
||||
{
|
||||
$ThemePaths = Option::where("key", "theme_paths")->first();
|
||||
$dirs = [];
|
||||
if ($ThemePaths) {
|
||||
$path = json_decode($ThemePaths->value);
|
||||
foreach ($path->paths as $p) {
|
||||
$dirs[] = getConst($p);
|
||||
}
|
||||
}
|
||||
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")){
|
||||
$manifest = json_decode(file_get_contents($path . "/manifest.json"), true);
|
||||
if ($manifest['routs']) {
|
||||
return $manifest['routs'];
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user