vendor/symfony/http-kernel/EventListener/DebugHandlersListener.php line 70

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\EventListener;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Console\ConsoleEvents;
  13. use Symfony\Component\Console\Event\ConsoleEvent;
  14. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  15. use Symfony\Component\Debug\ErrorHandler;
  16. use Symfony\Component\Debug\ExceptionHandler;
  17. use Symfony\Component\EventDispatcher\Event;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  22. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  23. use Symfony\Component\HttpKernel\Event\KernelEvent;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. /**
  26.  * Configures errors and exceptions handlers.
  27.  *
  28.  * @author Nicolas Grekas <p@tchwork.com>
  29.  */
  30. class DebugHandlersListener implements EventSubscriberInterface {
  31.     private $exceptionHandler;
  32.     private $logger;
  33.     private $levels;
  34.     private $throwAt;
  35.     private $scream;
  36.     private $fileLinkFormat;
  37.     private $scope;
  38.     private $charset;
  39.     private $firstCall true;
  40.     private $hasTerminatedWithException;
  41.     /**
  42.      * @param callable|null                 $exceptionHandler A handler that will be called on Exception
  43.      * @param LoggerInterface|null          $logger           A PSR-3 logger
  44.      * @param array|int                     $levels           An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  45.      * @param int|null                      $throwAt          Thrown errors in a bit field of E_* constants, or null to keep the current value
  46.      * @param bool                          $scream           Enables/disables screaming mode, where even silenced errors are logged
  47.      * @param string|FileLinkFormatter|null $fileLinkFormat   The format for links to source files
  48.      * @param bool                          $scope            Enables/disables scoping mode
  49.      */
  50.     public function __construct(callable $exceptionHandler nullLoggerInterface $logger null$levels E_ALL, ?int $throwAt E_ALLbool $scream true$fileLinkFormat nullbool $scope truestring $charset null) {
  51.         $this->exceptionHandler $exceptionHandler;
  52.         $this->logger $logger;
  53.         $this->levels null === $levels E_ALL $levels;
  54.         $this->throwAt = \is_int($throwAt) ? $throwAt : (null === $throwAt null : ($throwAt E_ALL null));
  55.         $this->scream $scream;
  56.         $this->fileLinkFormat $fileLinkFormat;
  57.         $this->scope $scope;
  58.         $this->charset $charset;
  59.     }
  60.     /**
  61.      * Configures the error handler.
  62.      */
  63.     public function configure(Event $event null) {
  64.         if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  65.             return;
  66.         }
  67.         $this->firstCall $this->hasTerminatedWithException false;
  68.         $handler set_exception_handler('var_dump');
  69.         $handler = \is_array($handler) ? $handler[0] : null;
  70.         restore_exception_handler();
  71.         if ($this->logger || null !== $this->throwAt) {
  72.             if ($handler instanceof ErrorHandler) {
  73.                 if ($this->logger) {
  74.                     $handler->setDefaultLogger($this->logger$this->levels);
  75.                     if (\is_array($this->levels)) {
  76.                         $levels 0;
  77.                         foreach ($this->levels as $type => $log) {
  78.                             $levels |= $type;
  79.                         }
  80.                     } else {
  81.                         $levels $this->levels;
  82.                     }
  83.                     if ($this->scream) {
  84.                         $handler->screamAt($levels);
  85.                     }
  86.                     if ($this->scope) {
  87.                         $handler->scopeAt($levels & ~E_USER_DEPRECATED & ~E_DEPRECATED);
  88.                     } else {
  89.                         $handler->scopeAt(0true);
  90.                     }
  91.                     $this->logger $this->levels null;
  92.                 }
  93.                 if (null !== $this->throwAt) {
  94.                     $handler->throwAt($this->throwAttrue);
  95.                 }
  96.             }
  97.         }
  98.         if (!$this->exceptionHandler) {
  99.             if ($event instanceof KernelEvent) {
  100.                 if (method_exists($kernel $event->getKernel(), 'terminateWithException')) {
  101.                     $request $event->getRequest();
  102.                     $hasRun = &$this->hasTerminatedWithException;
  103.                     $this->exceptionHandler = static function (\Exception $e) use ($kernel$request, &$hasRun) {
  104.                         if ($hasRun) {
  105.                             throw $e;
  106.                         }
  107.                         $hasRun true;
  108.                         $kernel->terminateWithException($e$request);
  109.                     };
  110.                 }
  111.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  112.                 $output $event->getOutput();
  113.                 if ($output instanceof ConsoleOutputInterface) {
  114.                     $output $output->getErrorOutput();
  115.                 }
  116.                 $this->exceptionHandler = function ($e) use ($app$output) {
  117.                     $app->renderException($e$output);
  118.                 };
  119.             }
  120.         }
  121.         if ($this->exceptionHandler) {
  122.             if ($handler instanceof ErrorHandler) {
  123.                 $h $handler->setExceptionHandler('var_dump');
  124.                 if (\is_array($h) && $h[0] instanceof ExceptionHandler) {
  125.                     $handler->setExceptionHandler($h);
  126.                     $handler $h[0];
  127.                 } else {
  128.                     $handler->setExceptionHandler($this->exceptionHandler);
  129.                 }
  130.             }
  131.             if ($handler instanceof ExceptionHandler) {
  132.                 $handler->setHandler($this->exceptionHandler);
  133.                 if (null !== $this->fileLinkFormat) {
  134.                     $handler->setFileLinkFormat($this->fileLinkFormat);
  135.                 }
  136.             }
  137.             $this->exceptionHandler null;
  138.         }
  139.     }
  140.     /**
  141.      * @internal
  142.      */
  143.     public function onKernelException(GetResponseForExceptionEvent $event) {
  144.         if (!$this->hasTerminatedWithException || !$event->isMasterRequest()) {
  145.             return;
  146.         }
  147.         $debug $this->scream && $this->scope;
  148.         $controller = function (Request $request) use ($debug) {
  149.             $e $request->attributes->get('exception');
  150.             $handler = new ExceptionHandler($debug$this->charset$this->fileLinkFormat);
  151.             return new Response($handler->getHtml($e), $e->getStatusCode(), $e->getHeaders());
  152.         };
  153.         (new ExceptionListener($controller$this->logger$debug))->onKernelException($event);
  154.     }
  155.     public static function getSubscribedEvents() {
  156.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  157.         if ('cli' === \PHP_SAPI && \defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  158.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  159.         }
  160.         $events[KernelEvents::EXCEPTION] = ['onKernelException', -2048];
  161.         return $events;
  162.     }
  163. }