我正在运行Symfony 2.7.6 dev并且我有一个应该在console.exception上触发的侦听器设置,但是它没有触发,它只像往常一样在控制台中显示异常。出于测试目的,我已经整合了一个console.terminate监听器,它工作正常。 (我也测试了console.command,也工作正常)。
对于我的生活,我无法弄清楚为什么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
}
}
?>
答案 0 :(得分:1)
尝试通过这种方式构建控制台侦听器:
namespace CompanyHidden\PortalBundle\Listener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\ConsoleEvents;
class ConsoleExceptionListener implements EventSubscriberInterface
{
/.../
public static function getSubscribedEvents()
{
return [
ConsoleEvents::EXCEPTION => 'onConsoleException',
ConsoleEvents::TERMINATE => 'onConsoleTerminate'
];
}
public function onConsoleTerminate(ConsoleTerminateEvent $event)
{
die(">>>> TERMINATE TEST >>>>");
}
public function onConsoleException(ConsoleExceptionEvent $event)
{
die(">>> EXCEPTION TEST<<<<");
}
}
答案 1 :(得分:1)
在控制台文件中,使用$ kernel-&gt; boot();
启动内核$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$kernel->boot();
$kernel->getContainer()->get('event_dispatcher')->addListener(\Symfony\Component\Console\ConsoleEvents::COMMAND,
function (\Symfony\Component\Console\Event\ConsoleCommandEvent $event) {
var_dump("COMMAND EVENT");
});
$kernel->getContainer()->get('event_dispatcher')->addListener(\Symfony\Component\Console\ConsoleEvents::EXCEPTION,
function (\Symfony\Component\Console\Event\ConsoleExceptionEvent $event) {
var_dump("EXCEPTION EVENT");
});
$kernel->getContainer()->get('event_dispatcher')->addListener(\Symfony\Component\Console\ConsoleEvents::TERMINATE,
function (\Symfony\Component\Console\Event\ConsoleTerminateEvent $event) {
var_dump("TERMINATE EVENT");
});
$application->run($input);
答案 2 :(得分:1)
Scoolnico的解决方案与symfony 3.3完美配合。 有关信息,&#39; console.exception&#39;事件已被&#39; console.error&#39;取代在symfony 3.3(https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions)
答案 3 :(得分:0)
配置和侦听器看起来正确。
检查您的应用程序/控制台文件,确保已设置调度程序。
应该看起来像这样
$dispatcher = new EventDispatcher();
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->setDispatcher($dispatcher); // <-- Needed for events to work
$application->run($input);