mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 16:45:35 +08:00
fix symfony event dispatcher unavailable
This commit is contained in:
@@ -11,7 +11,7 @@ use Symfony\Component\Console\ConsoleEvents;
|
|||||||
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
||||||
use Symfony\Component\Console\Event\ConsoleErrorEvent;
|
use Symfony\Component\Console\Event\ConsoleErrorEvent;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||||
use ZM\Exception\SingletonViolationException;
|
use ZM\Exception\SingletonViolationException;
|
||||||
use ZM\Store\FileSystem;
|
use ZM\Store\FileSystem;
|
||||||
|
|
||||||
@@ -43,31 +43,38 @@ final class ConsoleApplication extends Application
|
|||||||
|
|
||||||
$this->registerGlobalOptions();
|
$this->registerGlobalOptions();
|
||||||
|
|
||||||
// 设置命令事件分发器
|
// 设置命令事件分发器(临时版)
|
||||||
$dispatcher = new EventDispatcher();
|
$this->setDispatcher(new class() implements EventDispatcherInterface {
|
||||||
$this->setDispatcher($dispatcher);
|
public function dispatch(object $event, string $eventName = null): object
|
||||||
|
{
|
||||||
|
$eventName ??= $event::class;
|
||||||
|
|
||||||
// 注册命令执行前监听器
|
// 命令执行前
|
||||||
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
|
if ($eventName === ConsoleEvents::COMMAND) {
|
||||||
$input = $event->getInput();
|
/** @var ConsoleCommandEvent $event */
|
||||||
|
$input = $event->getInput();
|
||||||
|
|
||||||
// 初始化内核
|
// 初始化内核
|
||||||
/** @var Framework $kernel */
|
$kernel = Framework::getInstance();
|
||||||
$kernel = Framework::getInstance();
|
$kernel->runtime_preferences = $kernel->runtime_preferences
|
||||||
$kernel->runtime_preferences = $kernel->runtime_preferences
|
->withConfigDir($input->getOption('config-dir'))
|
||||||
->withConfigDir($input->getOption('config-dir'))
|
->withEnvironment($input->getOption('env'))
|
||||||
->withEnvironment($input->getOption('env'))
|
->enableDebugMode($input->getOption('debug'))
|
||||||
->enableDebugMode($input->getOption('debug'))
|
->withLogLevel($input->getOption('log-level'));
|
||||||
->withLogLevel($input->getOption('log-level'));
|
$kernel->bootstrap();
|
||||||
$kernel->bootstrap();
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// 注册命令执行错误监听器
|
// 命令执行错误
|
||||||
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) {
|
if ($eventName === ConsoleEvents::ERROR) {
|
||||||
$e = $event->getError();
|
/** @var ConsoleErrorEvent $event */
|
||||||
// 输出错误信息
|
$e = $event->getError();
|
||||||
echo zm_internal_errcode('E00005') . "{$e->getMessage()} at {$e->getFile()}({$e->getLine()})" . PHP_EOL;
|
// 输出错误信息
|
||||||
exit(1);
|
echo zm_internal_errcode('E00005') . "{$e->getMessage()} at {$e->getFile()}({$e->getLine()})" . PHP_EOL;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $event;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 设置单例,阻止后续实例化
|
// 设置单例,阻止后续实例化
|
||||||
|
|||||||
Reference in New Issue
Block a user