mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 15:45:36 +08:00
Merge pull request #342 from zhamao-robot/fix-symfony-no-contract
修复 Symfony Console Event Dispatcher 接口缺失
This commit is contained in:
@@ -6,12 +6,11 @@ namespace ZM;
|
|||||||
|
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
|
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
|
||||||
use Symfony\Component\Console\ConsoleEvents;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
|
||||||
use Symfony\Component\Console\Event\ConsoleErrorEvent;
|
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use ZM\Exception\SingletonViolationException;
|
use ZM\Exception\SingletonViolationException;
|
||||||
use ZM\Store\FileSystem;
|
use ZM\Store\FileSystem;
|
||||||
|
|
||||||
@@ -43,44 +42,24 @@ final class ConsoleApplication extends Application
|
|||||||
|
|
||||||
$this->registerGlobalOptions();
|
$this->registerGlobalOptions();
|
||||||
|
|
||||||
// 设置命令事件分发器(临时版)
|
|
||||||
$this->setDispatcher(new class() implements EventDispatcherInterface {
|
|
||||||
public function dispatch(object $event, string $eventName = null): object
|
|
||||||
{
|
|
||||||
$eventName ??= $event::class;
|
|
||||||
|
|
||||||
// 命令执行前
|
|
||||||
if ($eventName === ConsoleEvents::COMMAND) {
|
|
||||||
/** @var ConsoleCommandEvent $event */
|
|
||||||
$input = $event->getInput();
|
|
||||||
|
|
||||||
// 初始化内核
|
|
||||||
$kernel = Framework::getInstance();
|
|
||||||
$kernel->runtime_preferences = $kernel->runtime_preferences
|
|
||||||
->withConfigDir($input->getOption('config-dir'))
|
|
||||||
->withEnvironment($input->getOption('env'))
|
|
||||||
->enableDebugMode($input->getOption('debug'))
|
|
||||||
->withLogLevel($input->getOption('log-level'));
|
|
||||||
$kernel->bootstrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 命令执行错误
|
|
||||||
if ($eventName === ConsoleEvents::ERROR) {
|
|
||||||
/** @var ConsoleErrorEvent $event */
|
|
||||||
$e = $event->getError();
|
|
||||||
// 输出错误信息
|
|
||||||
echo zm_internal_errcode('E00005') . "{$e->getMessage()} at {$e->getFile()}({$e->getLine()})" . PHP_EOL;
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $event;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 设置单例,阻止后续实例化
|
// 设置单例,阻止后续实例化
|
||||||
self::$obj = $this;
|
self::$obj = $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function doRun(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return parent::doRun($input, $output);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
// 输出错误信息
|
||||||
|
echo zm_internal_errcode('E00005') . "{$e->getMessage()} at {$e->getFile()}({$e->getLine()})" . PHP_EOL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册全局选项,应用到所有命令
|
* 注册全局选项,应用到所有命令
|
||||||
*/
|
*/
|
||||||
@@ -94,6 +73,22 @@ final class ConsoleApplication extends Application
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
// 初始化内核
|
||||||
|
$kernel = Framework::getInstance();
|
||||||
|
$kernel->runtime_preferences = $kernel->runtime_preferences
|
||||||
|
->withConfigDir($input->getOption('config-dir'))
|
||||||
|
->withEnvironment($input->getOption('env'))
|
||||||
|
->enableDebugMode($input->getOption('debug'))
|
||||||
|
->withLogLevel($input->getOption('log-level'));
|
||||||
|
$kernel->bootstrap();
|
||||||
|
return parent::doRunCommand($command, $input, $output);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册命令加载器
|
* 注册命令加载器
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user