如何在收到警告或通知时阻止Symfony退出整个程序

use*_*554 5 php warnings command notice symfony

在Symfony中,当我们运行命令时,如果有任何通知或警告,Symfony会显示一条红色的大消息并退出整个程序.

我希望将错误发送到日志,但我不希望整个程序退出.任何人都可以告诉我如何实现这一目标.谢谢.

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class NotQuitWithWarningOrNoticeCommand extends ContainerAwareCommand
{

protected function configure()
{
    $this
        ->setName('try:nored')
        ->setDescription('Avoid to show big red message')
        ->addArgument('type');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $type = $input->getArgument('type');
    if ($type == 'warning' || !$type) {
        trigger_error("a warning", E_USER_WARNING);
    }
    if ($type == 'notice' || !$type) {
        trigger_error("a notice", E_USER_NOTICE);
    }
    echo "Hi there!\n";
}

} 
Run Code Online (Sandbox Code Playgroud)

(这里的代码只是为了重现问题.获取警告或通知是我的方案.)

小智 1

程序退出的原因是 Symfony 将警告提升为异常,而代码中没有任何内容捕获它。--no-debug您可以通过在调用命令时添加命令行选项来防止此行为。