1414use Psr \Log \LoggerInterface ;
1515use Symfony \Component \Debug \ErrorHandler ;
1616use Symfony \Component \Debug \ExceptionHandler ;
17+ use Symfony \Component \EventDispatcher \Event ;
18+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
1719use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
20+ use Symfony \Component \HttpKernel \Event \KernelEvent ;
1821use Symfony \Component \HttpKernel \KernelEvents ;
22+ use Symfony \Component \Console \ConsoleEvents ;
23+ use Symfony \Component \Console \Event \ConsoleEvent ;
24+ use Symfony \Component \Console \Output \ConsoleOutputInterface ;
1925
2026/**
2127 * Configures errors and exceptions handlers.
@@ -28,6 +34,7 @@ class DebugHandlersListener implements EventSubscriberInterface
2834 private $ logger ;
2935 private $ levels ;
3036 private $ debug ;
37+ private $ fileLinkFormat ;
3138
3239 /**
3340 * @param callable $exceptionHandler A handler that will be called on Exception
@@ -45,8 +52,18 @@ public function __construct($exceptionHandler, LoggerInterface $logger = null, $
4552 $ this ->fileLinkFormat = $ fileLinkFormat ?: ini_get ('xdebug.file_link_format ' ) ?: get_cfg_var ('xdebug.file_link_format ' );
4653 }
4754
48- public function configure ()
55+ /**
56+ * Configures the error handler.
57+ *
58+ * @param Event|null $event The triggering event
59+ * @param string|null $eventName The triggering event name
60+ * @param EventDispatcherInterface|null $eventDispatcher The dispatcher used to trigger $event
61+ */
62+ public function configure (Event $ event = null , $ eventName = null , EventDispatcherInterface $ eventDispatcher = null )
4963 {
64+ if (null !== $ eventDispatcher ) {
65+ $ eventDispatcher ->removeListener (array_keys (static ::getSubscribedEvents ()), array ($ this , 'configure ' ));
66+ }
5067 if ($ this ->logger ) {
5168 $ handler = set_error_handler ('var_dump ' , 0 );
5269 $ handler = is_array ($ handler ) ? $ handler [0 ] : null ;
@@ -65,7 +82,20 @@ public function configure()
6582 }
6683 $ handler ->screamAt ($ this ->levels );
6784 }
68- $ this ->logger = $ this ->levels = null ;
85+ $ this ->logger = $ this ->levels = $ this ->debug = $ this ->fileLinkFormat = null ;
86+ }
87+ if (!$ this ->exceptionHandler ) {
88+ if ($ event instanceof KernelEvent) {
89+ $ this ->exceptionHandler = array ($ event ->getKernel (), 'terminateWithException ' );
90+ } elseif ($ event instanceof ConsoleEvent && $ app = $ event ->getCommand ()->getApplication ()) {
91+ $ output = $ event ->getOutput ();
92+ if ($ output instanceof ConsoleOutputInterface) {
93+ $ output = $ output ->getErrorOutput ();
94+ }
95+ $ this ->exceptionHandler = function ($ e ) use ($ app , $ output ) {
96+ $ app ->renderException ($ e , $ output );
97+ };
67DE
code>98+ }
6999 }
70100 if ($ this ->exceptionHandler ) {
71101 $ handler = set_exception_handler ('var_dump ' );
@@ -86,6 +116,9 @@ public function configure()
86116
87117 public static function getSubscribedEvents ()
88118 {
89- return array (KernelEvents::REQUEST => array ('configure ' , 2048 ));
119+ return array (
120+ KernelEvents::REQUEST => array ('configure ' , 2048 ),
121+ ConsoleEvents::COMMAND => array ('configure ' , 2048 ),
122+ );
90123 }
91124}
0 commit comments