This commit is contained in:
Билай Станислав 2024-11-19 12:27:52 +03:00
parent 202d265cab
commit 772ad48961
7 changed files with 45 additions and 53 deletions

View File

@ -3,6 +3,7 @@
namespace kernel\controllers; namespace kernel\controllers;
use DirectoryIterator; use DirectoryIterator;
use JetBrains\PhpStorm\NoReturn;
use Josantonius\Session\Facades\Session; use Josantonius\Session\Facades\Session;
use kernel\AdminController; use kernel\AdminController;
use kernel\helpers\Debug; use kernel\helpers\Debug;
@ -53,10 +54,6 @@ class ModuleController extends AdminController
$i++; $i++;
} }
} }
// foreach (ModuleShopClientService::getModuleShopClientInfo() as $module) {
// $module->id = $i++;
// $modules_info[] = $module;
// }
$module_count = count($modules_info); $module_count = count($modules_info);
$modules_info = array_slice($modules_info, $per_page*($page_number-1), $per_page); $modules_info = array_slice($modules_info, $per_page*($page_number-1), $per_page);
@ -106,23 +103,11 @@ class ModuleController extends AdminController
$this->cgView->render("view.php", ['data' => $mod_info]); $this->cgView->render("view.php", ['data' => $mod_info]);
} }
public function actionUpdate(): void #[NoReturn] public function actionUpdate(): void
{ {
$request = new Request(); $request = new Request();
$slug = $request->get('slug'); $slug = $request->get('slug');
if ($this->moduleService->isKernelModule($slug)) { $this->redirect("/admin/module_shop_client/update/?slug=" . $slug, 302);
$active_res = $this->moduleService->updateModule($request->get("slug"));
if (!$active_res){
Session::start();
Session::set("error", implode(";", $this->moduleService->getErrors()));
$this->redirect("/admin", 302);
}
$mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));
} else {
$this->redirect("/admin/module_shop_client/update/?slug=" . $slug, 302);
}
$this->cgView->render("view.php", ['data' => $mod_info]);
} }
} }

View File

@ -189,8 +189,8 @@ class ModuleService
*/ */
public function runInitScript($mod_info): void public function runInitScript($mod_info): void
{ {
if (isset($mod_info['module_class'])){ if (isset($mod_info['module_class'])) {
if (isset($mod_info['module_class_file'])){ if (isset($mod_info['module_class_file'])) {
require_once $mod_info['module_class_file']; require_once $mod_info['module_class_file'];
} }
$moduleClass = new $mod_info['module_class'](); $moduleClass = new $mod_info['module_class']();
@ -204,8 +204,8 @@ class ModuleService
*/ */
public function runDeactivateScript($mod_info): void public function runDeactivateScript($mod_info): void
{ {
if (isset($mod_info['module_class'])){ if (isset($mod_info['module_class'])) {
if (isset($mod_info['module_class_file'])){ if (isset($mod_info['module_class_file'])) {
require_once $mod_info['module_class_file']; require_once $mod_info['module_class_file'];
} }
$moduleClass = new $mod_info['module_class'](); $moduleClass = new $mod_info['module_class']();
@ -233,11 +233,11 @@ class ModuleService
foreach ($dirs as $dir) { foreach ($dirs as $dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) { foreach (new DirectoryIterator($dir) as $fileInfo) {
if($fileInfo->isDot() or !in_array($fileInfo->getFilename(), $active_modules['modules'])) continue; if ($fileInfo->isDot() or !in_array($fileInfo->getFilename(), $active_modules['modules'])) continue;
$modules[] = $this->getModuleInfo($fileInfo->getPathname()); $modules[] = $this->getModuleInfo($fileInfo->getPathname());
} }
} }
return $modules; return $modules;
} }
@ -248,8 +248,8 @@ class ModuleService
{ {
$routs = []; $routs = [];
$modules = $this->getActiveModules(); $modules = $this->getActiveModules();
foreach ($modules as $module){ foreach ($modules as $module) {
if (isset($module['routs'])){ if (isset($module['routs'])) {
$routs[] = $module['path'] . "/" . $module['routs']; $routs[] = $module['path'] . "/" . $module['routs'];
} }
} }
@ -264,8 +264,8 @@ class ModuleService
{ {
$migrationsPaths = []; $migrationsPaths = [];
$modules = $this->getActiveModules(); $modules = $this->getActiveModules();
foreach ($modules as $module){ foreach ($modules as $module) {
if (isset($module['migration_path'])){ if (isset($module['migration_path'])) {
$migrationsPaths[] = $module['path'] . "/" . $module['migration_path']; $migrationsPaths[] = $module['path'] . "/" . $module['migration_path'];
} }
} }
@ -326,7 +326,7 @@ class ModuleService
$fileHelper = new Files(); $fileHelper = new Files();
if (file_exists(APP_DIR . '/modules/' . $moduleInfo['slug'])) { if (file_exists(APP_DIR . '/modules/' . $moduleInfo['slug'])) {
$fileHelper->recursiveRemoveDir(APP_DIR . '/modules/'. $moduleInfo['slug']); $fileHelper->recursiveRemoveDir(APP_DIR . '/modules/' . $moduleInfo['slug']);
} }
if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug'])) { if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug'])) {
$fileHelper->recursiveRemoveDir(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug']); $fileHelper->recursiveRemoveDir(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug']);
@ -345,10 +345,9 @@ class ModuleService
$fileHelper = new Files(); $fileHelper = new Files();
$fileHelper->copy_folder(ROOT_DIR . $path, $tmpModuleDirFull . 'app/'); $fileHelper->copy_folder(ROOT_DIR . $path, $tmpModuleDirFull . 'app/');
if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleName)){ if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleName)) {
$fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/'); $fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');
} } else {
else {
mkdir($tmpModuleDirFull . 'kernel/'); mkdir($tmpModuleDirFull . 'kernel/');
} }
@ -419,15 +418,15 @@ class ModuleService
{ {
$module_paths = Option::where("key", "module_paths")->first(); $module_paths = Option::where("key", "module_paths")->first();
$dirs = []; $dirs = [];
if ($module_paths){ if ($module_paths) {
$path = json_decode($module_paths->value); $path = json_decode($module_paths->value);
foreach ($path->paths as $p){ foreach ($path->paths as $p) {
$dirs[] = getConst($p); $dirs[] = getConst($p);
} }
} }
foreach ($dirs as $dir){ foreach ($dirs as $dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) { foreach (new DirectoryIterator($dir) as $fileInfo) {
if($fileInfo->isDot()) continue; if ($fileInfo->isDot()) continue;
if ($this->getModuleInfo($fileInfo->getPathname())['slug'] === $slug) { if ($this->getModuleInfo($fileInfo->getPathname())['slug'] === $slug) {
return true; return true;
}; };
@ -454,17 +453,25 @@ class ModuleService
public function isKernelModule(string $slug): bool public function isKernelModule(string $slug): bool
{ {
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug'); $modules_info = $this->getKernelModules();
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) { foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug']) { if ($mod['slug'] === $slug) {
return false; return true;
} }
} }
return true; return false;
}
public function getKernelModules(): array
{
$modules_info = [];
foreach (new DirectoryIterator(KERNEL_MODULES_DIR) as $fileInfo) {
if ($fileInfo->isDot()) continue;
$modules_info[] = $this->getModuleInfo($fileInfo->getPathname());
}
return $modules_info;
} }
} }

View File

@ -60,7 +60,7 @@ $table->addAction(function ($row, $url) use ($moduleService){
return false; return false;
}); });
$table->addAction(function ($row, $url) use ($moduleService){ $table->addAction(function ($row) use ($moduleService){
$slug = $row['slug']; $slug = $row['slug'];
if (!$moduleService->isKernelModule($slug)){ if (!$moduleService->isKernelModule($slug)){
$label = "Удалить"; $label = "Удалить";

View File

@ -0,0 +1,9 @@
<?php
/**
* @var string $icon
* @var string $btn
* @var string $url
*/
?>
"<a class='btn btn-$btn' href='$url/deactivate/?slug=$slug' style='margin: 3px; width: 150px;' >$icon</a>";

View File

@ -1,9 +0,0 @@
<?php
/**
* @var string $icon
* @var string $btn
* @var string $url
*/
?>
"<a class='btn btn-$btn_type' href='$url/deactivate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";