Symfony覆盖来自第三方Bundle的命令类

时间:2017-12-19 19:26:03

标签: php symfony exception override symfony-console

我有一个项目,我正在使用JMose's Command Scheduler Bundle来创建一个cronjob管理器。 cronjob管理器处理我自己的自定义Symfony命令 parse:source

问题是这个repo正在处理Exceptions并在命令控制台上抛出错误,如果它包含了$ e-> getLine()方法,那么它将非常整洁,以便将异常消息与产生它的线。 你可以在下面看到他的代码。

    try {
        $output->writeln('<info>Execute</info> : <comment>' . $scheduledCommand->getCommand()
            . ' ' . $scheduledCommand->getArguments() . '</comment>');
        $result = $command->run($input, $logOutput);
    } catch (\Exception $e) {
        $logOutput->writeln($e->getMessage());
        $logOutput->writeln($e->getTraceAsString());
        $result = -1;
    }

我想要的是重写他的类以添加该行代码。我没试过就试过了,我无法在Symfony文档站点上找到帮助。

我目前的项目结构看起来像那样。我已经突出显示了类ExecuteCommand,因为它是我想要覆盖的类。

enter image description here

1 个答案:

答案 0 :(得分:1)

以详细模式运行命令:

php bin/console parse:source -v

使用Symfony控制台事件: https://symfony.com/doc/3.4/components/console/events.html

有一个名为EXCEPTIONconsole.exception的事件,您可以捕获任何命令异常。