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

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\ErrorHandler\ErrorHandler;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  18. use Symfony\Component\HttpKernel\Event\KernelEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. /**
  21.  * Configures errors and exceptions handlers.
  22.  *
  23.  * @author Nicolas Grekas <p@tchwork.com>
  24.  *
  25.  * @final
  26.  */
  27. class DebugHandlersListener implements EventSubscriberInterface
  28. {
  29.     private $exceptionHandler;
  30.     private $logger;
  31.     private $deprecationLogger;
  32.     private $levels;
  33.     private $throwAt;
  34.     private $scream;
  35.     private $fileLinkFormat;
  36.     private $scope;
  37.     private $firstCall true;
  38.     private $hasTerminatedWithException;
  39.     /**
  40.      * @param callable|null                 $exceptionHandler A handler that must support \Throwable instances that will be called on Exception
  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, ?int $throwAt E_ALLbool $scream true$fileLinkFormat nullbool $scope trueLoggerInterface $deprecationLogger null)
  48.     {
  49.         $this->exceptionHandler $exceptionHandler;
  50.         $this->logger $logger;
  51.         $this->levels null === $levels E_ALL $levels;
  52.         $this->throwAt = \is_int($throwAt) ? $throwAt : (null === $throwAt null : ($throwAt E_ALL null));
  53.         $this->scream $scream;
  54.         $this->fileLinkFormat $fileLinkFormat;
  55.         $this->scope $scope;
  56.         $this->deprecationLogger $deprecationLogger;
  57.     }
  58.     /**
  59.      * Configures the error handler.
  60.      */
  61.     public function configure(object $event null)
  62.     {
  63.         if ($event instanceof ConsoleEvent && !\in_array(\PHP_SAPI, ['cli''phpdbg'], true)) {
  64.             return;
  65.         }
  66.         if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  67.             return;
  68.         }
  69.         $this->firstCall $this->hasTerminatedWithException false;
  70.         $handler set_exception_handler('var_dump');
  71.         $handler = \is_array($handler) ? $handler[0] : null;
  72.         restore_exception_handler();
  73.         if ($handler instanceof ErrorHandler) {
  74.             if ($this->logger || $this->deprecationLogger) {
  75.                 $this->setDefaultLoggers($handler);
  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->deprecationLogger $this->levels null;
  93.             }
  94.             if (null !== $this->throwAt) {
  95.                 $handler->throwAt($this->throwAttrue);
  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 (\Throwable $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 = static function (\Throwable $e) use ($app$output) {
  117.                     $app->renderThrowable($e$output);
  118.                 };
  119.             }
  120.         }
  121.         if ($this->exceptionHandler) {
  122.             if ($handler instanceof ErrorHandler) {
  123.                 $handler->setExceptionHandler($this->exceptionHandler);
  124.             }
  125.             $this->exceptionHandler null;
  126.         }
  127.     }
  128.     private function setDefaultLoggers(ErrorHandler $handler): void
  129.     {
  130.         if (\is_array($this->levels)) {
  131.             $levelsDeprecatedOnly = [];
  132.             $levelsWithoutDeprecated = [];
  133.             foreach ($this->levels as $type => $log) {
  134.                 if (E_DEPRECATED == $type || E_USER_DEPRECATED == $type) {
  135.                     $levelsDeprecatedOnly[$type] = $log;
  136.                 } else {
  137.                     $levelsWithoutDeprecated[$type] = $log;
  138.                 }
  139.             }
  140.         } else {
  141.             $levelsDeprecatedOnly $this->levels & (E_DEPRECATED E_USER_DEPRECATED);
  142.             $levelsWithoutDeprecated $this->levels & ~E_DEPRECATED & ~E_USER_DEPRECATED;
  143.         }
  144.         $defaultLoggerLevels $this->levels;
  145.         if ($this->deprecationLogger && $levelsDeprecatedOnly) {
  146.             $handler->setDefaultLogger($this->deprecationLogger$levelsDeprecatedOnly);
  147.             $defaultLoggerLevels $levelsWithoutDeprecated;
  148.         }
  149.         if ($this->logger && $defaultLoggerLevels) {
  150.             $handler->setDefaultLogger($this->logger$defaultLoggerLevels);
  151.         }
  152.     }
  153.     public static function getSubscribedEvents(): array
  154.     {
  155.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  156.         if (\defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  157.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  158.         }
  159.         return $events;
  160.     }
  161. }