MicroFrameWork/kernel/services/KernelService.php

96 lines
3.0 KiB
PHP
Raw Normal View History

2024-12-17 12:23:45 +03:00
<?php
namespace kernel\services;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
2024-12-18 15:24:25 +03:00
use kernel\helpers\Version;
2024-12-17 12:23:45 +03:00
use kernel\Request;
use ZipArchive;
class KernelService
{
protected null|bool $serverAvailable = null;
protected ModuleService $moduleService;
protected Files $files;
public function __construct()
{
$this->moduleService = new ModuleService();
$this->files = new Files();
}
public function getKernelInfo(): false|array|string
{
$info = [];
$info['path'] = KERNEL_DIR;
if (file_exists(KERNEL_DIR . "/manifest.json")) {
$manifest = json_decode(file_get_contents(KERNEL_DIR . "/manifest.json"), true);
$manifest = getConst($manifest);
$info = array_merge($info, $manifest);
}
return $info;
}
public function isLastVersion(): bool
{
if ($this->moduleService->isServerAvailable()) {
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$kernel_info = $this->getKernelInfo();
2024-12-18 15:24:25 +03:00
$kernelVersion = Version::getIntVersionByString($kernel_info['version']);
2024-12-17 12:23:45 +03:00
foreach ($modules_info as $mod) {
2024-12-18 15:24:25 +03:00
$modVersion = Version::getIntVersionByString($mod['version']);
if ($mod['slug'] === $kernel_info['slug'] && $modVersion <= $kernelVersion) {
2024-12-17 12:23:45 +03:00
return true;
}
}
}
return false;
}
public function updateKernel(string $path): bool
{
$request = new Request();
$files = $request->post('files');
2024-12-17 12:47:45 +03:00
2024-12-17 12:23:45 +03:00
$zip = new ZipArchive;
2024-12-17 12:41:30 +03:00
if (file_exists(ROOT_DIR . $path)) {
2024-12-17 12:23:45 +03:00
$tmpKernelDir = md5(time());
2024-12-17 12:41:30 +03:00
$res = $zip->open(ROOT_DIR . $path);
2024-12-17 12:23:45 +03:00
if ($res === TRUE) {
$tmpKernelDirFull = RESOURCES_DIR . '/tmp/kernel/' . $tmpKernelDir . "/";
$zip->extractTo($tmpKernelDirFull);
$zip->close();
$this->files->recursiveRemoveKernelDir();
2024-12-18 15:24:25 +03:00
$this->files->copyKernelFolder($tmpKernelDirFull . 'kernel' , ROOT_DIR . "/kernel");
2024-12-17 12:23:45 +03:00
foreach ($files as $file) {
2024-12-18 16:32:51 +03:00
Debug::prn($file);
2024-12-17 12:23:45 +03:00
if ($file === 'bootstrap') {
2024-12-18 16:32:51 +03:00
Debug::prn(123);
2024-12-17 12:23:45 +03:00
$this->files->recursiveRemoveDir(ROOT_DIR . '/bootstrap');
2024-12-18 15:24:25 +03:00
$this->files->copyKernelFolder($tmpKernelDirFull . 'bootstrap' , ROOT_DIR . '/bootstrap');
2024-12-17 12:23:45 +03:00
}
copy($tmpKernelDirFull . $file , ROOT_DIR . '/' . $file);
}
$this->files->recursiveRemoveDir($tmpKernelDirFull);
2024-12-17 13:02:25 +03:00
unlink(ROOT_DIR . $path);
2024-12-18 16:32:51 +03:00
Debug::dd("update kernel success");
2024-12-17 12:23:45 +03:00
return true;
}
}
return false;
}
}