vendor/symfony/symfony/src/Symfony/Component/HttpKernel/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 ConsoleEvent && !\in_array(\PHP_SAPI, ['cli''phpdbg'], true)) {
  63.             return;
  64.         }
  65.         if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  66.             return;
  67.         }
  68.         $this->firstCall $this->hasTerminatedWithException false;
  69.         $handler set_exception_handler('var_dump');
  70.         $handler = \is_array($handler) ? $handler[0] : null;
  71.         restore_exception_handler();
  72.         if ($this->logger || null !== $this->throwAt) {
  73.             if ($handler instanceof ErrorHandler) {
  74.                 if ($this->logger) {
  75.                     $handler->setDefaultLogger($this->logger$this->levels);
  76.                     if (\is_array($this->levels)) {
  77.                         $levels 0;
  78.                         foreach ($this->levels as $type => $log) {
  79.                             $levels |= $type;
  80.                         }
  81.                     } else {
  82.                         $levels $this->levels;
  83.                     }
  84.                     if ($this->scream) {
  85.                         $handler->screamAt($levels);
  86.                     }
  87.                     if ($this->scope) {
  88.                         $handler->scopeAt($levels & ~\E_USER_DEPRECATED & ~\E_DEPRECATED);
  89.                     } else {
  90.                         $handler->scopeAt(0true);
  91.                     }
  92.                     $this->logger $this->levels null;
  93.                 }
  94.                 if (null !== $this->throwAt) {
  95.                     $handler->throwAt($this->throwAttrue);
  96.                 }
  97.             }
  98.         }
  99.         if (!$this->exceptionHandler) {
  100.             if ($event instanceof KernelEvent) {
  101.                 if (method_exists($kernel $event->getKernel(), 'terminateWithException')) {
  102.                     $request $event->getRequest();
  103.                     $hasRun = &$this->hasTerminatedWithException;
  104.                     $this->exceptionHandler = static function (\Exception $e) use ($kernel$request, &$hasRun) {
  105.                         if ($hasRun) {
  106.                             throw $e;
  107.                         }
  108.                         $hasRun true;
  109.                         $kernel->terminateWithException($e$request);
  110.                     };
  111.                 }
  112.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  113.                 $output $event->getOutput();
  114.                 if ($output instanceof ConsoleOutputInterface) {
  115.                     $output $output->getErrorOutput();
  116.                 }
  117.                 $this->exceptionHandler = function ($e) use ($app$output) {
  118.                     $app->renderException($e$output);
  119.                 };
  120.             }
  121.         }
  122.         if ($this->exceptionHandler) {
  123.             if ($handler instanceof ErrorHandler) {
  124.                 $h $handler->setExceptionHandler('var_dump');
  125.                 if (\is_array($h) && $h[0] instanceof ExceptionHandler) {
  126.                     $handler->setExceptionHandler($h);
  127.                     $handler $h[0];
  128.                 } else {
  129.                     $handler->setExceptionHandler($this->exceptionHandler);
  130.                 }
  131.             }
  132.             if ($handler instanceof ExceptionHandler) {
  133.                 $handler->setHandler($this->exceptionHandler);
  134.                 if (null !== $this->fileLinkFormat) {
  135.                     $handler->setFileLinkFormat($this->fileLinkFormat);
  136.                 }
  137.             }
  138.             $this->exceptionHandler null;
  139.         }
  140.     }
  141.     public static function getSubscribedEvents()
  142.     {
  143.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  144.         if (\defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  145.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  146.         }
  147.         return $events;
  148.     }
  149. }