0

我正在運行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 
    } 
} 
?> 

時拋出(而不是引發異常監聽器)控制檯異常 Console Exception

+0

我不明白你的問題。是什麼讓你認爲致命的錯誤觸發了異常監聽器? – xabbuh

+0

@xabbuh致命錯誤觸發正常的「in-app」操作下的正常kernel.exception ExceptionListener。爲什麼在命令執行過程中致命錯誤不會觸發console.exception?它甚至在截圖的第二行顯示它是一個例外。 FatalErrorException,但仍是一個例外。 – Magnanimity

回答

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<<<<"); 
    } 
} 
+0

它似乎沒有區別。我需要在我的config.yml中更新監聽器服務嗎? – Magnanimity

+0

不,你的config.yml看起來不錯...我嘗試上面的代碼,它的工作原理。 – scoolnico

+0

嘗試測試你的命令,無一例外地檢查'console.terminate'監聽器是否工作。 – scoolnico

1

在控制檯文件,啓動與$內核內核級>啓動();

$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); 
+0

謝謝,我會在接下來的一週左右考慮這個,如果它有效,接受你的答案。 – Magnanimity