我正在運行Symfony 2.7.6 dev,並且我有一個應該在console.exception上觸發的監聽器,但它不會觸發,它只會在控制檯中顯示異常照常。出於測試目的,我已經合併了一個console.terminate監聽器,它工作正常。 (我也測試了console.command,這也很好)。Symfony console.exception不會觸發監聽器
對於我的生活,我無法弄清楚爲什麼console.exception事件不會觸發或爲什麼console.exception監聽器不觸發。在config.yml
ConsoleExceptionListener設置
kernel.listener.command_dispatch:
class: CompanyHidden\PortalBundle\Listener\ConsoleExceptionListener
arguments: ["@service_container", "@router"]
tags:
- { name: kernel.event_listener, event: console.exception, method: onConsoleException }
- { name: kernel.event_listener, event: console.terminate, method: onConsoleTerminate }
ConsoleExceptionListener.php
<?php
namespace CompanyHidden\PortalBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class ConsoleExceptionListener
{
private $container;
private $router;
function __construct($container, $router)
{
$this->container = $container;
$this->router = $router;
}
public function onConsoleTerminate()
{
die(">>>> TERMINATE TEST >>>>");
}
public function onConsoleException(ConsoleExceptionEvent $event)
{
die(">>> EXCEPTION TEST<<<<");
}
}
控制檯命令
<?php
namespace CompanyHidden\PortalBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use CompanyHidden\PortalBundle\Classes\ImapMailbox;
class getEmailsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('getEmails');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$context = $this->getContainer()->get('router')->getContext();
$context->setScheme('http');
# SIMULATED TERMINATION TEST
//print ">>>> TERMINATED <<<<<\n";
//return null;
# SIMULATED EXCEPTION TEST
$null = null;
$null->getNull();
//..... Rest of Code, not relevant
}
}
?>
我不明白你的問題。是什麼讓你認爲致命的錯誤觸發了異常監聽器? – xabbuh
@xabbuh致命錯誤觸發正常的「in-app」操作下的正常kernel.exception ExceptionListener。爲什麼在命令執行過程中致命錯誤不會觸發console.exception?它甚至在截圖的第二行顯示它是一個例外。 FatalErrorException,但仍是一個例外。 – Magnanimity