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

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\HttpKernel\Debug\FileLinkFormatter;
  20. use Symfony\Component\HttpKernel\Event\KernelEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. /**
  23.  * Configures errors and exceptions handlers.
  24.  *
  25.  * @author Nicolas Grekas <p@tchwork.com>
  26.  */
  27. class DebugHandlersListener implements EventSubscriberInterface
  28. {
  29.     private $exceptionHandler;
  30.     private $logger;
  31.     private $levels;
  32.     private $throwAt;
  33.     private $scream;
  34.     private $fileLinkFormat;
  35.     private $scope;
  36.     private $firstCall true;
  37.     private $hasTerminatedWithException;
  38.     /**
  39.      * @param callable|null                 $exceptionHandler A handler that will be called on Exception
  40.      * @param LoggerInterface|null          $logger           A PSR-3 logger
  41.      * @param array|int                     $levels           An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  42.      * @param int|null                      $throwAt          Thrown errors in a bit field of E_* constants, or null to keep the current value
  43.      * @param bool                          $scream           Enables/disables screaming mode, where even silenced errors are logged
  44.      * @param string|FileLinkFormatter|null $fileLinkFormat   The format for links to source files
  45.      * @param bool                          $scope            Enables/disables scoping mode
  46.      */
  47.     public function __construct(callable $exceptionHandler nullLoggerInterface $logger null$levels E_ALL$throwAt E_ALL$scream true$fileLinkFormat null$scope true)
  48.     {
  49.         $this->exceptionHandler $exceptionHandler;
  50.         $this->logger $logger;
  51.         $this->levels null === $levels E_ALL $levels;
  52.         $this->throwAt is_numeric($throwAt) ? (int) $throwAt : (null === $throwAt null : ($throwAt E_ALL null));
  53.         $this->scream = (bool) $scream;
  54.         $this->fileLinkFormat $fileLinkFormat;
  55.         $this->scope = (bool) $scope;
  56.     }
  57.     /**
  58.      * Configures the error handler.
  59.      */
  60.     public function configure(Event $event null)
  61.     {
  62.         if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  63.             return;
  64.         }
  65.         $this->firstCall $this->hasTerminatedWithException false;
  66.         $handler set_exception_handler('var_dump');
  67.         $handler = \is_array($handler) ? $handler[0] : null;
  68.         restore_exception_handler();
  69.         if ($this->logger || null !== $this->throwAt) {
  70.             if ($handler instanceof ErrorHandler) {
  71.                 if ($this->logger) {
  72.                     $handler->setDefaultLogger($this->logger$this->levels);
  73.                     if (\is_array($this->levels)) {
  74.                         $levels 0;
  75.                         foreach ($this->levels as $type => $log) {
  76.                             $levels |= $type;
  77.                         }
  78.                     } else {
  79.                         $levels $this->levels;
  80.                     }
  81.                     if ($this->scream) {
  82.                         $handler->screamAt($levels);
  83.                     }
  84.                     if ($this->scope) {
  85.                         $handler->scopeAt($levels & ~E_USER_DEPRECATED & ~E_DEPRECATED);
  86.                     } else {
  87.                         $handler->scopeAt(0true);
  88.                     }
  89.                     $this->logger $this->levels null;
  90.                 }
  91.                 if (null !== $this->throwAt) {
  92.                     $handler->throwAt($this->throwAttrue);
  93.                 }
  94.             }
  95.         }
  96.         if (!$this->exceptionHandler) {
  97.             if ($event instanceof KernelEvent) {
  98.                 if (method_exists($kernel $event->getKernel(), 'terminateWithException')) {
  99.                     $request $event->getRequest();
  100.                     $hasRun = &$this->hasTerminatedWithException;
  101.                     $this->exceptionHandler = static function (\Exception $e) use ($kernel$request, &$hasRun) {
  102.                         if ($hasRun) {
  103.                             throw $e;
  104.                         }
  105.                         $hasRun true;
  106.                         $kernel->terminateWithException($e$request);
  107.                     };
  108.                 }
  109.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  110.                 $output $event->getOutput();
  111.                 if ($output instanceof ConsoleOutputInterface) {
  112.                     $output $output->getErrorOutput();
  113.                 }
  114.                 $this->exceptionHandler = function ($e) use ($app$output) {
  115.                     $app->renderException($e$output);
  116.                 };
  117.             }
  118.         }
  119.         if ($this->exceptionHandler) {
  120.             if ($handler instanceof ErrorHandler) {
  121.                 $h $handler->setExceptionHandler('var_dump');
  122.                 if (\is_array($h) && $h[0] instanceof ExceptionHandler) {
  123.                     $handler->setExceptionHandler($h);
  124.                     $handler $h[0];
  125.                 } else {
  126.                     $handler->setExceptionHandler($this->exceptionHandler);
  127.                 }
  128.             }
  129.             if ($handler instanceof ExceptionHandler) {
  130.                 $handler->setHandler($this->exceptionHandler);
  131.                 if (null !== $this->fileLinkFormat) {
  132.                     $handler->setFileLinkFormat($this->fileLinkFormat);
  133.                 }
  134.             }
  135.             $this->exceptionHandler null;
  136.         }
  137.     }
  138.     public static function getSubscribedEvents()
  139.     {
  140.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  141.         if ('cli' === \PHP_SAPI && \defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  142.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  143.         }
  144.         return $events;
  145.     }
  146. }