backend/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php

120 lines
4.2 KiB
PHP
Raw Normal View History

2024-10-29 14:53:52 +03:00
<?php
namespace kernel\modules\module_shop_client\controllers;
use GuzzleHttp\Client;
2024-11-05 12:15:09 +03:00
use GuzzleHttp\Exception\GuzzleException;
2024-10-31 16:12:02 +03:00
use JetBrains\PhpStorm\NoReturn;
2024-10-29 14:53:52 +03:00
use kernel\AdminController;
use kernel\helpers\Debug;
2024-11-08 16:19:11 +03:00
use kernel\helpers\Files;
2024-10-29 14:53:52 +03:00
use kernel\modules\module_shop_client\services\ModuleShopClientService;
2024-10-31 16:12:02 +03:00
use kernel\Request;
2024-10-29 14:53:52 +03:00
use kernel\services\ModuleService;
class ModuleShopClientController extends AdminController
{
protected Client $client;
protected ModuleService $moduleService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/module_shop_client/views/";
$this->client = new Client();
$this->moduleService = new ModuleService();
}
2024-11-05 12:15:09 +03:00
/**
* @throws GuzzleException
*/
2024-10-29 14:53:52 +03:00
public function actionIndex(int $page_number = 1): void
{
$per_page = 8;
2024-11-05 13:44:08 +03:00
$token = $_ENV['MODULE_SHOP_TOKEN'];
2024-11-08 14:47:59 +03:00
$modules_info = $this->client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug', [
2024-10-29 14:53:52 +03:00
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$module_count = count($modules_info);
$modules_info = array_slice($modules_info, $per_page*($page_number-1), $per_page);
$this->cgView->render("index.php", [
'modules_info' => $modules_info,
2024-10-31 16:12:02 +03:00
'moduleService' => $this->moduleService,
2024-10-29 14:53:52 +03:00
'page_number' => $page_number,
'module_count' => $module_count,
'per_page' => $per_page,
]);
}
2024-11-05 12:15:09 +03:00
/**
* @throws GuzzleException
*/
2024-10-29 14:53:52 +03:00
public function actionView(int $id): void
{
2024-11-08 14:47:59 +03:00
$token = $_ENV['MODULE_SHOP_TOKEN'];
$module_info = $this->client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id, [
2024-10-29 14:53:52 +03:00
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
$module_info = json_decode($module_info->getBody()->getContents(), true);
$this->cgView->render("view.php", ['data' => $module_info]);
}
2024-11-05 12:15:09 +03:00
/**
* @throws GuzzleException
*/
2024-11-08 14:47:59 +03:00
#[NoReturn] public function actionInstall(): void
2024-10-29 14:53:52 +03:00
{
2024-11-08 14:47:59 +03:00
// $request = new Request();
2024-11-08 16:19:11 +03:00
// $id = $request->get('id');
2024-11-08 14:47:59 +03:00
// $token = $_ENV['MODULE_SHOP_TOKEN'];
2024-11-08 16:19:11 +03:00
// Debug::prn(123);
// $module = $this->client->request('GET', $_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $id, [
2024-11-08 14:47:59 +03:00
// 'headers' => [
// 'Authorization' => 'Bearer ' . $token,
2024-11-08 16:19:11 +03:00
// 'Accept' => 'application/itguild',
// 'sink' => RESOURCES_DIR . '/tmp/ms/some.itguild'
2024-11-08 14:47:59 +03:00
// ]
// ]);
2024-11-08 16:19:11 +03:00
//
// $module = json_decode($module->getBody()->getContents(), true);
//// Debug::dd(123);
2024-11-08 14:47:59 +03:00
// $this->moduleService->installModule(RESOURCES_DIR . '/tmp/ms/some.igm');
// $this->redirect('/admin/module_shop_client', 302);
2024-10-29 14:53:52 +03:00
2024-11-08 16:19:11 +03:00
$request = new Request();
$id = $request->get("id");
$token = $_ENV['MODULE_SHOP_TOKEN'];
$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);
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']));
// Debug::prn($this->moduleService->getErrors());
// Debug::dd(RESOURCES_DIR . '/tmp/modules/' . basename($module_info['path_to_archive']));
$this->moduleService->installModule(RESOURCES_DIR . '/tmp/ms/some.igm');
$this->redirect('/admin/module_shop_client', 302);
2024-10-29 14:53:52 +03:00
}
2024-10-31 16:12:02 +03:00
#[NoReturn] public function actionDelete(): void
{
$request = new Request();
$slug = $request->get("slug");
$module_info = $this->moduleService->getModuleInfoBySlug($slug);
$this->moduleService->uninstallModule($module_info['app_module_path']);
$this->redirect('/admin/module_shop_client', 302);
}
2024-10-29 14:53:52 +03:00
}