14
14
use Psr \Log \LoggerInterface ;
15
15
use Symfony \Component \Debug \ErrorHandler ;
16
16
use Symfony \Component \Debug \ExceptionHandler ;
17
+ use Symfony \Component \EventDispatcher \Event ;
18
+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
17
19
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
20
+ use Symfony \Component \HttpKernel \Event \KernelEvent ;
18
21
use Symfony \Component \HttpKernel \KernelEvents ;
22
+ use Symfony \Component \Console \ConsoleEvents ;
23
+ use Symfony \Component \Console \Event \ConsoleEvent ;
24
+ use Symfony \Component \Console \Output \ConsoleOutputInterface ;
19
25
20
26
/**
21
27
* Configures errors and exceptions handlers.
@@ -28,6 +34,7 @@ class DebugHandlersListener implements EventSubscriberInterface
28
34
private $ logger ;
29
35
private $ levels ;
30
36
private $ debug ;
37
+ private $ fileLinkFormat ;
31
38
32
39
/**
33
40
* @param callable $exceptionHandler A handler that will be called on Exception
@@ -45,8 +52,18 @@ public function __construct($exceptionHandler, LoggerInterface $logger = null, $
45
52
$ this ->fileLinkFormat = $ fileLinkFormat ?: ini_get ('xdebug.file_link_format ' ) ?: get_cfg_var ('xdebug.file_link_format ' );
46
53
}
47
54
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 )
49
63
{
64
+ if (null !== $ eventDispatcher ) {
65
+ $ eventDispatcher ->removeListener (array_keys (static ::getSubscribedEvents ()), array ($ this , 'configure ' ));
66
+ }
50
67
if ($ this ->logger ) {
51
68
$ handler = set_error_handler ('var_dump ' , 0 );
52
69
$ handler = is_array ($ handler ) ? $ handler [0 ] : null ;
@@ -65,7 +82,20 @@ public function configure()
65
82
}
66
83
$ handler ->screamAt ($ this ->levels );
67
84
}
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
+ };
98
+ }
69
99
}
70
100
if ($ this ->exceptionHandler ) {
71
101
$ handler = set_exception_handler ('var_dump ' );
@@ -86,6 +116,9 @@ public function configure()
86
116
87
117
public static function getSubscribedEvents ()
88
118
{
89
- return array (KernelEvents::REQUEST => array ('configure ' , 2048 ));
119
+ return array (
120
+ KernelEvents::REQUEST => array ('configure ' , 2048 ),
121
+ ConsoleEvents::COMMAND => array ('configure ' , 2048 ),
122
+ );
90
123
}
91
124
}
0 commit comments