Files
zhamao-framework/src/ZM/Command/CheckConfigCommand.php

55 lines
1.8 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace ZM\Command;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand(name: 'check:config', description: '检查配置文件是否和框架当前版本有更新')]
class CheckConfigCommand extends Command
{
2022-12-17 22:18:50 +08:00
use SourceLoadModeOnly;
private bool $need_update = false;
2022-12-17 22:18:50 +08:00
protected function handle(): int
{
$current_cfg = SOURCE_ROOT_DIR . '/config/';
2022-12-17 22:18:50 +08:00
$remote_cfg = include FRAMEWORK_ROOT_DIR . '/config/global_old.php';
if (file_exists($current_cfg . 'global.php')) {
2022-12-17 22:18:50 +08:00
$this->check($remote_cfg, 'global.php');
}
if (file_exists($current_cfg . 'global.development.php')) {
2022-12-17 22:18:50 +08:00
$this->check($remote_cfg, 'global.development.php');
}
if (file_exists($current_cfg . 'global.staging.php')) {
2022-12-17 22:18:50 +08:00
$this->check($remote_cfg, 'global.staging.php');
}
if (file_exists($current_cfg . 'global.production.php')) {
2022-12-17 22:18:50 +08:00
$this->check($remote_cfg, 'global.production.php');
}
if ($this->need_update === true) {
2022-12-17 22:18:50 +08:00
$this->comment('有配置文件需要更新,详情见文档 `https://framework.zhamao.xin/update/config`');
} else {
2022-12-17 22:18:50 +08:00
$this->info('配置文件暂无更新!');
}
2022-12-17 22:18:50 +08:00
return self::SUCCESS;
}
private function check(mixed $remote, mixed $local)
{
2022-12-17 22:18:50 +08:00
$local_file = include WORKING_DIR . '/config/' . $local;
2021-07-09 01:38:30 +08:00
if ($local_file === true) {
2022-08-23 18:02:00 +08:00
$local_file = config('global');
2021-07-09 01:38:30 +08:00
}
foreach ($remote as $k => $v) {
if (!isset($local_file[$k])) {
2022-12-17 22:18:50 +08:00
$this->comment("配置文件 {$local} 需要更新!(当前配置文件缺少 `{$k}` 字段配置)");
$this->need_update = true;
}
}
}
}