zhamao-framework/src/ZM/ConsoleApplication.php

104 lines
3.9 KiB
PHP
Raw Normal View History

2020-08-31 10:11:06 +08:00
<?php
declare(strict_types=1);
2020-08-31 10:11:06 +08:00
namespace ZM;
use Symfony\Component\Console\Application;
2022-12-17 22:18:50 +08:00
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
2023-02-18 20:56:47 +08:00
use ZM\Command\Server\ServerStartCommand;
use ZM\Exception\SingletonViolationException;
2022-12-17 22:18:50 +08:00
use ZM\Store\FileSystem;
2020-08-31 10:11:06 +08:00
/**
* 命令行启动的入口文件,用于初始化环境变量,并启动命令行应用
*
* 这里启动的不是框架,而是框架相关的命令行环境
*/
2022-08-13 17:00:29 +08:00
final class ConsoleApplication extends Application
2020-08-31 10:11:06 +08:00
{
2023-02-18 20:56:47 +08:00
protected array $bootstrappers = [
Bootstrap\LoadConfiguration::class, // 加载配置文件
Bootstrap\LoadGlobalDefines::class, // 加载框架级别的全局常量声明
Bootstrap\RegisterLogger::class, // 加载 Logger
Bootstrap\HandleExceptions::class, // 注册异常处理器
Bootstrap\RegisterEventProvider::class, // 绑定框架的 EventProvider 到 libob 的 Driver 上
Bootstrap\SetInternalTimezone::class, // 设置时区
];
private static ?ConsoleApplication $obj = null;
2021-07-04 15:45:30 +08:00
public function __construct(string $name = 'zhamao-framework')
{
if (self::$obj !== null) {
throw new SingletonViolationException(self::class);
}
// 初始化命令
2022-12-21 00:28:02 +08:00
$command_classes = [];
// 先加载框架内置命令
$command_classes = array_merge(
$command_classes,
FileSystem::getClassesPsr4(FRAMEWORK_ROOT_DIR . '/src/ZM/Command', 'ZM\\Command')
);
// 再加载用户自定义命令(如存在)
2022-12-21 01:19:45 +08:00
if (is_dir(SOURCE_ROOT_DIR . '/src/Command')) {
2022-12-21 00:28:02 +08:00
$command_classes = array_merge(
$command_classes,
2022-12-21 01:19:45 +08:00
FileSystem::getClassesPsr4(SOURCE_ROOT_DIR . '/src/Command', 'Command')
2022-12-21 00:28:02 +08:00
);
}
// 初始化 Composer 变量
if (file_exists(WORKING_DIR . '/runtime/composer.phar')) {
echo '* Using native composer' . PHP_EOL;
putenv('COMPOSER_EXECUTABLE=' . WORKING_DIR . '/runtime/composer.phar');
}
2022-12-17 22:18:50 +08:00
$commands = [];
foreach ($command_classes as $command_class) {
try {
$command_class_ref = new \ReflectionClass($command_class);
} catch (\ReflectionException $e) {
logger()->error("命令 {$command_class} 无法加载!反射失败:" . $e->getMessage());
continue;
}
if ($command_class_ref->isAbstract()) {
continue;
}
// 从 AsCommand 注解中获取命令名称
$attr = $command_class_ref->getAttributes(AsCommand::class);
if (count($attr) > 0) {
$commands[$attr[0]->getArguments()['name']] = fn () => new $command_class();
} else {
logger()->warning("命令 {$command_class} 没有使用 AsCommand 注解,无法被加载");
}
2021-06-16 00:17:30 +08:00
}
2022-12-17 22:18:50 +08:00
// 命令工厂,用于延迟加载命令
$command_loader = new FactoryCommandLoader($commands);
$this->setCommandLoader($command_loader);
self::$obj = $this; // 用于标记已经初始化完成
parent::__construct($name, ZM_VERSION);
2020-08-31 10:11:06 +08:00
}
/**
* {@inheritdoc}
*/
public function run(InputInterface $input = null, OutputInterface $output = null): int
{
2023-02-18 20:56:47 +08:00
$options = $input?->getOptions() ?? ServerStartCommand::exportOptionArray();
foreach ($this->bootstrappers as $bootstrapper) {
resolve($bootstrapper)->bootstrap($options);
}
2020-08-31 10:11:06 +08:00
try {
return parent::run($input, $output);
} catch (\Exception $e) {
echo zm_internal_errcode('E00005') . "{$e->getMessage()} at {$e->getFile()}({$e->getLine()})" . PHP_EOL;
exit(1);
2020-08-31 10:11:06 +08:00
}
}
}