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,20 @@ 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
+ foreach (array_keys (static ::getSubscribedEvents ()) as $ name ) {
66
+ $ eventDispatcher ->removeListener ($ name , array ($ this , 'configure ' ));
67
+ }
68
+ }
50
69
if ($ this ->logger ) {
51
70
$ handler = set_error_handler ('var_dump ' , 0 );
52
71
$ handler = is_array ($ handler ) ? $ handler [0 ] : null ;
@@ -65,7 +84,20 @@ public function configure()
65
84
}
66
85
$ handler ->screamAt ($ this ->levels );
67
86
}
68
- $ this ->logger = $ this ->levels = null ;
87
+ $ this ->logger = null ;
88
+ }
89
+ if (!$ this ->exceptionHandler ) {
90
+ if ($ event instanceof KernelEvent) {
91
+ $ this ->exceptionHandler = array ($ event ->getKernel (), 'terminateWithException ' );
92
+ } elseif ($ event instanceof ConsoleEvent && $ app = $ event ->getCommand ()->getApplication ()) {
93
+ $ output = $ event ->getOutput ();
94
+ if ($ output instanceof ConsoleOutputInterface) {
95
+ $ output = $ output ->getErrorOutput ();
96
+ }
97
+ $ this ->exceptionHandler = function ($ e ) use ($ app , $ output ) {
98
+ $ app ->renderException ($ e , $ output );
99
+ };
100
+ }
69
101
}
70
102
if ($ this ->exceptionHandler ) {
71
103
$ handler = set_exception_handler ('var_dump ' );
@@ -86,6 +118,12 @@ public function configure()
86
118
87
119
public static function getSubscribedEvents ()
88
120
{
89
- return array (KernelEvents::REQUEST => array ('configure ' , 2048 ));
121
+ $ events = array (KernelEvents::REQUEST => array ('configure ' , 2048 ));
122
+
123
+ if (defined ('Symfony\Component\Console\ConsoleEvents::COMMAND ' )) {
124
+ $ events [ConsoleEvents::COMMAND ] = array ('configure ' , 2048 );
125
+ }
126
+
127
+ return $ events ;
90
128
}
91
129
}
0 commit comments