make commands lazily loaded

This commit is contained in:
sunxyw
2022-12-17 22:18:50 +08:00
parent 3806983bc2
commit 44c25f7fdd
8 changed files with 98 additions and 50 deletions

View File

@@ -13,6 +13,8 @@ use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'build', description: '将项目构建一个phar包')]
class BuildCommand extends Command
{
use NonPharLoadModeOnly;
/**
* 配置
*/

View File

@@ -5,57 +5,52 @@ declare(strict_types=1);
namespace ZM\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'check:config', description: '检查配置文件是否和框架当前版本有更新')]
class CheckConfigCommand extends Command
{
use SourceLoadModeOnly;
private $need_update = false;
protected function execute(InputInterface $input, OutputInterface $output): int
protected function handle(): int
{
if (LOAD_MODE !== 1) {
$output->writeln('<error>仅限在Composer依赖模式中使用此命令</error>');
return 1;
}
$current_cfg = getcwd() . '/config/';
$remote_cfg = include_once FRAMEWORK_ROOT_DIR . '/config/global_old.php';
$remote_cfg = include FRAMEWORK_ROOT_DIR . '/config/global_old.php';
if (file_exists($current_cfg . 'global.php')) {
$this->check($remote_cfg, 'global.php', $output);
$this->check($remote_cfg, 'global.php');
}
if (file_exists($current_cfg . 'global.development.php')) {
$this->check($remote_cfg, 'global.development.php', $output);
$this->check($remote_cfg, 'global.development.php');
}
if (file_exists($current_cfg . 'global.staging.php')) {
$this->check($remote_cfg, 'global.staging.php', $output);
$this->check($remote_cfg, 'global.staging.php');
}
if (file_exists($current_cfg . 'global.production.php')) {
$this->check($remote_cfg, 'global.production.php', $output);
$this->check($remote_cfg, 'global.production.php');
}
if ($this->need_update === true) {
$output->writeln('<comment>有配置文件需要更新,详情见文档 `https://framework.zhamao.xin/update/config`</comment>');
$this->comment('有配置文件需要更新,详情见文档 `https://framework.zhamao.xin/update/config`');
} else {
$output->writeln('<info>配置文件暂无更新!</info>');
$this->info('配置文件暂无更新!');
}
return 0;
return self::SUCCESS;
}
/**
* @param mixed $remote
* @param mixed $local
*/
private function check($remote, $local, OutputInterface $out)
private function check($remote, $local)
{
$local_file = include_once WORKING_DIR . '/config/' . $local;
$local_file = include WORKING_DIR . '/config/' . $local;
if ($local_file === true) {
$local_file = config('global');
}
foreach ($remote as $k => $v) {
if (!isset($local_file[$k])) {
$out->writeln('<comment>配置文件 ' . $local . " 需要更新!(当前配置文件缺少 `{$k}` 字段配置)</comment>");
$this->comment("配置文件 {$local} 需要更新!当前配置文件缺少 `{$k}` 字段配置");
$this->need_update = true;
}
}

View File

@@ -32,7 +32,20 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
{
$this->input = $input;
$this->output = $output;
return $this->handle();
if ($this->shouldExecute()) {
return $this->handle();
}
return self::SUCCESS;
}
/**
* 是否应该执行
*
* @return bool 返回 true 以继续执行,返回 false 以中断执行
*/
protected function shouldExecute(): bool
{
return true;
}
/**

View File

@@ -13,6 +13,8 @@ use ZM\Exception\InitException;
#[AsCommand(name: 'init', description: '初始化框架运行的基础文件')]
class InitCommand extends Command
{
use NonPharLoadModeOnly;
private string $base_path;
private bool $force = false;

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace ZM\Command;
trait NonPharLoadModeOnly
{
protected function shouldExecute(): bool
{
if (\Phar::running() === '') {
return true;
}
if (method_exists($this, 'error')) {
$this->error('此命令只能在非 Phar 模式下使用!');
}
return false;
}
}

View File

@@ -14,7 +14,7 @@ use ZM\Exception\ZMKnownException;
use ZM\Framework;
use ZM\Process\ProcessStateManager;
#[AsCommand(name: 'server:start', description: '启动服务器', aliases: ['server'])]
#[AsCommand(name: 'server', description: '启动服务器', aliases: ['server:start'])]
class ServerStartCommand extends ServerCommand
{
public static function exportOptionArray(): array

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace ZM\Command;
trait SourceLoadModeOnly
{
protected function shouldExecute(): bool
{
if (LOAD_MODE === LOAD_MODE_SRC) {
return true;
}
if (method_exists($this, 'error')) {
$this->error('此命令只能在源码模式下使用!');
}
return false;
}
}