MicroFrameWork/kernel/console/controllers/KernelController.php

103 lines
3.6 KiB
PHP
Raw Normal View History

2024-10-14 15:52:52 +03:00
<?php
namespace kernel\console\controllers;
use kernel\console\ConsoleController;
use kernel\helpers\Files;
2024-10-17 14:55:00 +03:00
use ZipArchive;
2024-10-14 15:52:52 +03:00
class KernelController extends ConsoleController
{
2024-10-17 14:55:00 +03:00
protected Files $files;
public function __construct()
{
parent::__construct();
$this->files = new Files();
}
2024-10-14 15:52:52 +03:00
/**
* @throws \Exception
*/
public function actionPackKernel(): void
{
if (!isset($this->argv['path'])) {
throw new \Exception('Missing kernel path "--path" specified');
}
if (file_exists(ROOT_DIR . $this->argv['path'])) {
$tmpKernelDirFull = RESOURCES_DIR . '/tmp/ad/app/kernel';
$this->files->copy_folder(KERNEL_DIR, $tmpKernelDirFull);
$this->out->r("Ядро скопировано во временную папку", 'green');
} else {
$this->out->r("Ядро не найдено", 'red');
}
2024-10-14 15:52:52 +03:00
if (isset($this->argv['bootstrap'])) {
if (file_exists(ROOT_DIR . '/bootstrap')) {
$tmpBootstrapDirFull = RESOURCES_DIR . '/tmp/ad/app/bootstrap';
$this->files->copy_folder(ROOT_DIR . '/bootstrap', $tmpBootstrapDirFull);
$this->out->r("/bootstrap скопирован во временную папку", 'green');
} else {
$this->out->r("/bootstrap не найден", 'red');
}
}
2024-10-14 15:52:52 +03:00
if (isset($this->argv['.env'])) {
if (file_exists(ROOT_DIR . '/.env.example')) {
$tmpEnvFull = RESOURCES_DIR . '/tmp/ad/app/env.example';
copy(ROOT_DIR . '/.env.example', $tmpEnvFull);
$this->out->r("/.env.example скопирован во временную папку", 'green');
} else {
$this->out->r("/.env.example не найден", 'red');
2024-10-17 16:47:04 +03:00
}
}
2024-10-14 15:52:52 +03:00
if (!is_dir(RESOURCES_DIR . '/tmp/app')) {
mkdir(RESOURCES_DIR . '/tmp/app');
2024-10-14 15:52:52 +03:00
}
$this->files->pack(RESOURCES_DIR . '/tmp/ad/app/', RESOURCES_DIR . '/tmp/app/app.iga');
$this->files->recursiveRemoveDir(RESOURCES_DIR . '/tmp/ad/app/');
2024-10-14 15:52:52 +03:00
}
2024-10-17 14:55:00 +03:00
/**
* @throws \Exception
*/
public function actionUpdateKernel(): void
2024-10-17 14:55:00 +03:00
{
if (!isset($this->argv['path'])) {
throw new \Exception('Missing kernel path "--path" specified');
}
$zip = new ZipArchive;
if (file_exists(ROOT_DIR . $this->argv['path'])) {
$tmpAppDir = md5(time());
$res = $zip->open(ROOT_DIR . $this->argv['path']);
2024-10-17 14:55:00 +03:00
if ($res === TRUE) {
$tmpAppDirFull = RESOURCES_DIR . '/tmp/app/' . $tmpAppDir . "/";
$zip->extractTo($tmpAppDirFull);
2024-10-17 14:55:00 +03:00
$zip->close();
2024-10-17 16:47:04 +03:00
$this->files->recursiveRemoveKernelDir();
$this->files->copy_folder($tmpAppDirFull . 'kernel' , ROOT_DIR . "/kernel");
if (isset($this->argv['bootstrap'])) {
$this->files->recursiveRemoveDir(ROOT_DIR . '/bootstrap');
$this->files->copy_folder($tmpAppDirFull . 'bootstrap' , ROOT_DIR . '/bootstrap');
}
if (isset($this->argv['.env'])) {
copy($tmpAppDirFull . 'env.example', ROOT_DIR . '/.env.example');
}
$this->files->recursiveRemoveDir($tmpAppDirFull);
$this->out->r('Приложение обновлено.', 'green');
2024-10-17 14:55:00 +03:00
} else {
$this->out->r('unable to open zip archive', 'red');
}
} else {
$this->out->r("archive not found", 'red');
2024-10-17 14:55:00 +03:00
}
}
2024-10-14 15:52:52 +03:00
}