|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\HttpKernel\EventListener;
|
13 | 13 |
|
14 |
| -use Symfony\Component\Debug\ExceptionHandler; |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Component\Debug\ErrorHandler; |
| 16 | +use Symfony\Component\Debug\AbstractExceptionHandler; |
15 | 17 | use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
16 | 18 | use Symfony\Component\HttpKernel\KernelEvents;
|
17 | 19 |
|
18 | 20 | /**
|
19 |
| - * Configures the ExceptionHandler. |
| 21 | + * Configures errors and exceptions handlers. |
20 | 22 | *
|
21 | 23 | * @author Nicolas Grekas <[email protected]>
|
22 | 24 | */
|
23 | 25 | class DebugHandlersListener implements EventSubscriberInterface
|
24 | 26 | {
|
25 | 27 | private $exceptionHandler;
|
| 28 | + private $logger; |
| 29 | + private $levels; |
| 30 | + private $debug; |
26 | 31 |
|
27 |
| - public function __construct($exceptionHandler) |
| 32 | + /** |
| 33 | + * @param callable $exceptionHandler A handler that will be called on Exception |
| 34 | + * @param LoggerInterface|null $logger A PSR-3 logger |
| 35 | + * @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants |
| 36 | + * @param bool $debug Enables/disables debug mode |
| 37 | + */ |
| 38 | + public function __construct($exceptionHandler, LoggerInterface $logger = null, $levels = null, $debug = true) |
28 | 39 | {
|
29 | 40 | if (is_callable($exceptionHandler)) {
|
30 | 41 | $this->exceptionHandler = $exceptionHandler;
|
31 | 42 | }
|
| 43 | + $this->logger = $logger; |
| 44 | + $this->levels = $levels; |
| 45 | + $this->debug = $debug; |
32 | 46 | }
|
33 | 47 |
|
34 | 48 | public function configure()
|
35 | 49 | {
|
| 50 | + if ($this->logger) { |
| 51 | + $handler = set_error_handler('var_dump', 0); |
| 52 | + $handler = is_array($handler) ? $handler[0] : null; |
| 53 | + restore_error_handler(); |
| 54 | + if ($handler instanceof ErrorHandler) { |
| 55 | + if ($this->debug) { |
| 56 | + $handler->throwAt(-1); |
| 57 | + } |
| 58 | + $handler->setDefaultLogger($this->logger, $this->levels); |
| 59 | + if (is_array($this->levels)) { |
| 60 | + $scream = 0; |
| 61 | + foreach ($this->levels as $type => $log) { |
| 62 | + $scream |= $type; |
| 63 | + } |
| 64 | + $this->levels = $scream; |
| 65 | + } |
| 66 | + $handler->screamAt($this->levels); |
| 67 | + } |
| 68 | + $this->logger = $this->levels = null; |
| 69 | + } |
36 | 70 | if ($this->exceptionHandler) {
|
37 | 71 | $handler = set_exception_handler('var_dump');
|
38 | 72 | $handler = is_array($handler) ? $handler[0] : null;
|
39 | 73 | restore_exception_handler();
|
40 |
| - if ($handler instanceof ExceptionHandler) { |
| 74 | + if ($handler instanceof ErrorHandler) { |
| 75 | + $h = $handler->setExceptionHandler('var_dump') ?: $this->exceptionHandler; |
| 76 | + $handler->setExceptionHandler($h); |
| 77 | + $handler = is_array($h) ? $h[0] : null; |
| 78 | + } |
| 79 | + if ($handler instanceof AbstractExceptionHandler) { |
41 | 80 | $handler->setHandler($this->exceptionHandler);
|
42 | 81 | }
|
43 | 82 | $this->exceptionHandler = null;
|
|
0 commit comments