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

@ -3,6 +3,7 @@
namespace kernel\services;
use DirectoryIterator;
use GuzzleHttp\Client;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
@ -281,7 +282,7 @@ class ModuleService
$tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
} else {
@ -289,20 +290,20 @@ class ModuleService
return false;
}
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
$this->addError('manifest.json not found');
return false;
}
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson);
$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'])) {
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']);
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
} 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);
@ -368,7 +369,7 @@ class ModuleService
$tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
} else {
@ -376,19 +377,21 @@ class ModuleService
return false;
}
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
$this->addError('manifest.json not found');
return false;
}
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files();
$fileHelper->copy_folder($tmpModuleDirFull . 'app/manifest.json', $manifest['app_module_path'] . '/manifest.json');
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 {
$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);
@ -433,4 +436,24 @@ class ModuleService
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;
}
}