kernel update. composer.lock to gitignore

This commit is contained in:
2024-12-17 13:00:13 +03:00
parent c7549c225f
commit 499f2a37d2
49 changed files with 986 additions and 3376 deletions

View File

@ -0,0 +1,21 @@
<?php
namespace kernel\services;
class ConsoleService
{
public function runComposerRequire(string $package): void
{
exec("composer require $package");
}
public function runComposerRemove(string $package): void
{
exec("composer remove $package");
}
public function runCommand(string $command): void
{
exec($command);
}
}

View File

@ -0,0 +1,88 @@
<?php
namespace kernel\services;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
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();
foreach ($modules_info as $mod) {
if ($mod['slug'] === $kernel_info['slug'] && $mod['version'] === $kernel_info['version']) {
return true;
}
}
}
return false;
}
public function updateKernel(string $path): bool
{
$request = new Request();
$files = $request->post('files');
$zip = new ZipArchive;
if (file_exists(ROOT_DIR . $path)) {
$tmpKernelDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
$tmpKernelDirFull = RESOURCES_DIR . '/tmp/kernel/' . $tmpKernelDir . "/";
$zip->extractTo($tmpKernelDirFull);
$zip->close();
$this->files->recursiveRemoveKernelDir();
$this->files->copy_folder($tmpKernelDirFull . 'kernel' , ROOT_DIR . "/kernel");
foreach ($files as $file) {
if ($file === 'bootstrap') {
$this->files->recursiveRemoveDir(ROOT_DIR . '/bootstrap');
$this->files->copy_folder($tmpKernelDirFull . 'bootstrap' , ROOT_DIR . '/bootstrap');
}
copy($tmpKernelDirFull . $file , ROOT_DIR . '/' . $file);
}
$this->files->recursiveRemoveDir($tmpKernelDirFull);
unlink(ROOT_DIR . $path);
return true;
}
}
return false;
}
}

View File

@ -36,4 +36,25 @@ class MigrationService
}
}
public function rollbackAtPath(string $path): void
{
$path = getConst($path);
try {
$filesystem = new Filesystem();
$dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration');
$m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), $filesystem);
$migrationFiles = $m->getMigrationFiles($path);
foreach ($migrationFiles as $name => $migrationFile){
$migrationInstance = $filesystem->getRequire($migrationFile);
$migrationInstance->migration = $name;
$migrationInstance->down();
$dmr->delete($migrationInstance);
}
} catch (\Exception $e) {
throw new \Exception('Не удалось откатить миграции');
}
}
}

View File

@ -10,7 +10,6 @@ use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\models\Option;
use MongoDB\Driver\Session;
use ZipArchive;
class ModuleService
@ -444,10 +443,11 @@ class ModuleService
public function isLastVersion(string $slug): bool
{
if ($this->isServerAvailable()){
if ($this->isServerAvailable()) {
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$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']) {
@ -473,18 +473,20 @@ class ModuleService
public function isShopModule(string $slug): bool
{
if ($this->isServerAvailable()){
if ($this->isServerAvailable()) {
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
if (!$this->issetModuleShopToken()){
if (!$this->issetModuleShopToken()) {
return false;
}
$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']) {
return true;
if (isset($modules_info)) {
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug']) {
return true;
}
}
}
}
@ -505,7 +507,7 @@ class ModuleService
public function isServerAvailable(): bool
{
if (null !== $this->serverAvailable){
if (null !== $this->serverAvailable) {
return $this->serverAvailable;
}
@ -522,11 +524,58 @@ class ModuleService
public function issetModuleShopToken(): bool
{
if (!empty($_ENV['MODULE_SHOP_TOKEN'])){
if (!empty($_ENV['MODULE_SHOP_TOKEN'])) {
return true;
}
return false;
}
public function createDirs(string $slug): void
{
mkdir(KERNEL_APP_MODULES_DIR . "/$slug");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/migrations");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/services");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views");
mkdir(APP_DIR . "/modules/$slug");
mkdir(APP_DIR . "/modules/$slug/controllers");
mkdir(APP_DIR . "/modules/$slug/routs");
}
public function createModuleByParams(array $params): void
{
$slug = $params['slug'];
$model = $params['model'];
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/manifests/manifest_template', APP_DIR . "/modules/$slug/manifest.json", $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/kernel_controller_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/app_controller_template', APP_DIR . '/modules/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/kernel_routs_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/routs/' . $slug . '.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/app_routs_template', APP_DIR . '/modules/' . $slug . '/routs/' . $slug . '.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/kernel_module_file_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/' . $model . 'Module.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/app_module_file_template', APP_DIR . '/modules/' . $slug . '/' . $model . 'Module.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/model_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/' . $model . '.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/forms/create_form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/forms/Create' . $model . 'Form.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/services/service_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/services/' . $model . 'Service.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/index_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/index.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/view_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/view.php', $params);
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/form.php', $params);
}
public function createModuleFileByTemplate(string $templatePath, string $filePath, array $params): void
{
$data = file_get_contents($templatePath);
foreach ($params as $key => $param){
$data = str_replace("{" . $key . "}", $param, $data);
}
file_put_contents($filePath, $data);
}
}

View File

@ -32,7 +32,7 @@ class TokenService
*/
public static function md5(): string
{
return md5(microtime() . self::getSalt() . time());
return md5(microtime() . self::getSalt(10) . time());
}
/**
@ -40,7 +40,7 @@ class TokenService
*/
public static function crypt(): string
{
return crypt(microtime(), self::getSalt());
return crypt(microtime(), self::getSalt(20));
}
/**
@ -48,15 +48,15 @@ class TokenService
*/
public static function hash(string $alg): string
{
return hash($alg, self::getSalt());
return hash($alg, self::getSalt(10));
}
/**
* @throws RandomException
*/
public static function getSalt(): string
public static function getSalt(int $length): string
{
return bin2hex(random_bytes(10));
return bin2hex(random_bytes($length));
}
}