update modules from ms

This commit is contained in:
Билай Станислав 2024-11-14 01:55:04 +03:00
parent 44c94689c6
commit d2028e926b
6 changed files with 95 additions and 58 deletions

View File

@ -8,10 +8,6 @@ use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable; use Itguild\EloquentTable\ListEloquentTable;
use kernel\app_modules\tag\models\Tag; use kernel\app_modules\tag\models\Tag;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\models\Menu;
use kernel\modules\menu\table\columns\MenuDeleteActionColumn;
use kernel\modules\menu\table\columns\MenuEditActionColumn;
use kernel\modules\menu\table\columns\MenuViewActionColumn;
$table = new ListEloquentTable(new EloquentDataProvider(Tag::class, [ $table = new ListEloquentTable(new EloquentDataProvider(Tag::class, [
'currentPage' => $page_number, 'currentPage' => $page_number,

View File

@ -0,0 +1,21 @@
<?php
namespace kernel\helpers;
use http\Client;
class RESTClient
{
public static function request(string $url, string $method = 'GET')
{
$client = new \GuzzleHttp\Client();
return $client->request($method, $url, [
'headers' => [
'Authorization' => 'Bearer ' . $_ENV['MODULE_SHOP_TOKEN']
]
]);
}
}

View File

@ -6,8 +6,10 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController; use kernel\AdminController;
use kernel\Flash;
use kernel\helpers\Debug; use kernel\helpers\Debug;
use kernel\helpers\Files; use kernel\helpers\Files;
use kernel\helpers\RESTClient;
use kernel\modules\module_shop_client\services\ModuleShopClientService; use kernel\modules\module_shop_client\services\ModuleShopClientService;
use kernel\Request; use kernel\Request;
use kernel\services\ModuleService; use kernel\services\ModuleService;
@ -33,13 +35,7 @@ class ModuleShopClientController extends AdminController
public function actionIndex(int $page_number = 1): void public function actionIndex(int $page_number = 1): void
{ {
$per_page = 8; $per_page = 8;
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$token = $_ENV['MODULE_SHOP_TOKEN'];
$modules_info = $this->client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$modules_info = json_decode($modules_info->getBody()->getContents(), true); $modules_info = json_decode($modules_info->getBody()->getContents(), true);
$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);
@ -52,17 +48,9 @@ class ModuleShopClientController extends AdminController
]); ]);
} }
/**
* @throws GuzzleException
*/
public function actionView(int $id): void public function actionView(int $id): void
{ {
$token = $_ENV['MODULE_SHOP_TOKEN']; $module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id);
$module_info = $this->client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$module_info = json_decode($module_info->getBody()->getContents(), true); $module_info = json_decode($module_info->getBody()->getContents(), true);
$this->cgView->render("view.php", ['data' => $module_info]); $this->cgView->render("view.php", ['data' => $module_info]);
} }
@ -72,39 +60,31 @@ class ModuleShopClientController extends AdminController
*/ */
#[NoReturn] public function actionInstall(): void #[NoReturn] public function actionInstall(): void
{ {
// $request = new Request();
// $id = $request->get('id');
// $token = $_ENV['MODULE_SHOP_TOKEN'];
// Debug::prn(123);
// $module = $this->client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $id, [
// 'headers' => [
// 'Authorization' => 'Bearer ' . $token,
// 'Accept' => 'application/itguild',
// 'sink' => RESOURCES_DIR . '/tmp/ms/some.itguild'
// ]
// ]);
//
// $module = json_decode($module->getBody()->getContents(), true);
//// Debug::dd(123);
// $this->moduleService->installModule(RESOURCES_DIR . '/tmp/ms/some.igm');
// $this->redirect('/admin/module_shop_client', 302);
$request = new Request(); $request = new Request();
$id = $request->get("id"); $id = $request->get("id");
$token = $_ENV['MODULE_SHOP_TOKEN']; $module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $id);
$module_info = $this->client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$module_info = json_decode($module_info->getBody()->getContents(), true); $module_info = json_decode($module_info->getBody()->getContents(), true);
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $module_info['path_to_archive'], RESOURCES_DIR . "/tmp/modules"); Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $module_info['path_to_archive'], RESOURCES_DIR . "/tmp/modules");
$this->moduleService->installModule('/resources/tmp/modules/' . basename($module_info['path_to_archive'])); $this->moduleService->installModule('/resources/tmp/modules/' . basename($module_info['path_to_archive']));
// Debug::prn($this->moduleService->getErrors());
// Debug::dd(RESOURCES_DIR . '/tmp/modules/' . basename($module_info['path_to_archive'])); Flash::setMessage("success", "Модуль успешно установлен.");
$this->moduleService->installModule(RESOURCES_DIR . '/tmp/ms/some.igm');
$this->redirect('/admin/module_shop_client', 302); $this->redirect('/admin/module_shop_client', 302);
}
#[NoReturn] public function actionUpdate(): void
{
$request = new Request();
$id = $request->get("id");
$module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id);
$module_info = json_decode($module_info->getBody()->getContents(), true);
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $module_info['path_to_archive'], RESOURCES_DIR . "/tmp/modules");
$this->moduleService->updateModule('/resources/tmp/modules/' . basename($module_info['path_to_archive']));
Flash::setMessage("success", "Модуль успешно обновлен.");
$this->redirect('/admin/module_shop_client', 302);
} }
@ -114,6 +94,8 @@ class ModuleShopClientController extends AdminController
$slug = $request->get("slug"); $slug = $request->get("slug");
$module_info = $this->moduleService->getModuleInfoBySlug($slug); $module_info = $this->moduleService->getModuleInfoBySlug($slug);
$this->moduleService->uninstallModule($module_info['app_module_path']); $this->moduleService->uninstallModule($module_info['app_module_path']);
Flash::setMessage("success", "Модуль успешно удален.");
$this->redirect('/admin/module_shop_client', 302); $this->redirect('/admin/module_shop_client', 302);
} }

View File

@ -14,6 +14,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->get('/install', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionInstall']); App::$collector->get('/install', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionInstall']);
App::$collector->get('/view/{id}', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionView']); App::$collector->get('/view/{id}', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionView']);
App::$collector->get('/delete', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionDelete']); App::$collector->get('/delete', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionDelete']);
App::$collector->get('/update', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionUpdate']);
}); });
}); });
}); });

View File

@ -48,6 +48,20 @@ $table->addAction(function ($row, $url) use ($moduleService){
return $btn; return $btn;
}); });
$table->addAction(function ($row, $url) use ($moduleService){
$slug = $row['slug'];
$id = $row['id'];
if ($moduleService->isInstall($slug)){
if (!$moduleService->isLastVersion($slug)) {
$label = "Обновить";
$btn_type = "danger";
return "<a class='btn btn-$btn_type' href='$url/update/?id=$id' style='margin: 3px; width: 150px;' >$label</a>";
}
}
return false;
});
$table->create(); $table->create();

View File

@ -3,6 +3,7 @@
namespace kernel\services; namespace kernel\services;
use DirectoryIterator; use DirectoryIterator;
use GuzzleHttp\Client;
use kernel\helpers\Debug; use kernel\helpers\Debug;
use kernel\helpers\Files; use kernel\helpers\Files;
use kernel\helpers\Manifest; use kernel\helpers\Manifest;
@ -281,7 +282,7 @@ class ModuleService
$tmpModuleDir = md5(time()); $tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path); $res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) { if ($res === TRUE) {
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/"; $tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull); $zip->extractTo($tmpModuleDirFull);
$zip->close(); $zip->close();
} else { } else {
@ -289,20 +290,20 @@ class ModuleService
return false; return false;
} }
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) { if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
$this->addError('manifest.json not found'); $this->addError('manifest.json not found');
return false; return false;
} }
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json")); $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson); $manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files(); $fileHelper = new Files();
$fileHelper->copy_folder($tmpModuleDirFull . '/app', $manifest['app_module_path']); $fileHelper->copy_folder($tmpModuleDirFull . 'app', $manifest['app_module_path']);
if (isset($manifest['kernel_module_path'])) { if (isset($manifest['kernel_module_path'])) {
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']); $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
} else { } else {
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']); $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
} }
$fileHelper->recursiveRemoveDir($tmpModuleDirFull); $fileHelper->recursiveRemoveDir($tmpModuleDirFull);
@ -368,7 +369,7 @@ class ModuleService
$tmpModuleDir = md5(time()); $tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path); $res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) { if ($res === TRUE) {
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/"; $tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull); $zip->extractTo($tmpModuleDirFull);
$zip->close(); $zip->close();
} else { } else {
@ -376,19 +377,21 @@ class ModuleService
return false; return false;
} }
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) { if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
$this->addError('manifest.json not found'); $this->addError('manifest.json not found');
return false; return false;
} }
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json")); $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson); $manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files(); $fileHelper = new Files();
$fileHelper->copy_folder($tmpModuleDirFull . 'app/manifest.json', $manifest['app_module_path'] . '/manifest.json');
if (isset($manifest['kernel_module_path'])) { if (isset($manifest['kernel_module_path'])) {
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']); $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
} else { } else {
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']); $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
} }
$fileHelper->recursiveRemoveDir($tmpModuleDirFull); $fileHelper->recursiveRemoveDir($tmpModuleDirFull);
@ -433,4 +436,24 @@ class ModuleService
return false; return false;
} }
public function isLastVersion(string $slug): bool
{
$client = new Client();
$token = $_ENV['MODULE_SHOP_TOKEN'];
$modules_info = $client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
return true;
}
}
return false;
}
} }