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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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 <[email protected]>
  24.  *
  25.  * @final
  26.  */
  27. class DebugHandlersListener implements EventSubscriberInterface
  28. {
  29.     private $earlyHandler;
  30.     private $exceptionHandler;
  31.     private $logger;
  32.     private $deprecationLogger;
  33.     private $levels;
  34.     private $throwAt;
  35.     private $scream;
  36.     private $fileLinkFormat;
  37.     private $scope;
  38.     private $firstCall true;
  39.     private $hasTerminatedWithException;
  40.     /**
  41.      * @param callable|null                 $exceptionHandler A handler that must support \Throwable instances that will be called on Exception
  42.      * @param array|int                     $levels           An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  43.      * @param int|null                      $throwAt          Thrown errors in a bit field of E_* constants, or null to keep the current value
  44.      * @param bool                          $scream           Enables/disables screaming mode, where even silenced errors are logged
  45.      * @param string|FileLinkFormatter|null $fileLinkFormat   The format for links to source files
  46.      * @param bool                          $scope            Enables/disables scoping mode
  47.      */
  48.     public function __construct(callable $exceptionHandler nullLoggerInterface $logger null$levels \E_ALL, ?int $throwAt \E_ALLbool $scream true$fileLinkFormat nullbool $scope trueLoggerInterface $deprecationLogger null)
  49.     {
  50.         $handler set_exception_handler('var_dump');
  51.         $this->earlyHandler \is_array($handler) ? $handler[0] : null;
  52.         restore_exception_handler();
  53.         $this->exceptionHandler $exceptionHandler;
  54.         $this->logger $logger;
  55.         $this->levels null === $levels \E_ALL $levels;
  56.         $this->throwAt \is_int($throwAt) ? $throwAt : (null === $throwAt null : ($throwAt \E_ALL null));
  57.         $this->scream $scream;
  58.         $this->fileLinkFormat $fileLinkFormat;
  59.         $this->scope $scope;
  60.         $this->deprecationLogger $deprecationLogger;
  61.     }
  62.     /**
  63.      * Configures the error handler.
  64.      */
  65.     public function configure(object $event null)
  66.     {
  67.         if ($event instanceof ConsoleEvent && !\in_array(\PHP_SAPI, ['cli''phpdbg'], true)) {
  68.             return;
  69.         }
  70.         if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  71.             return;
  72.         }
  73.         $this->firstCall $this->hasTerminatedWithException false;
  74.         $handler set_exception_handler('var_dump');
  75.         $handler \is_array($handler) ? $handler[0] : null;
  76.         restore_exception_handler();
  77.         if (!$handler instanceof ErrorHandler) {
  78.             $handler $this->earlyHandler;
  79.         }
  80.         if ($handler instanceof ErrorHandler) {
  81.             if ($this->logger || $this->deprecationLogger) {
  82.                 $this->setDefaultLoggers($handler);
  83.                 if (\is_array($this->levels)) {
  84.                     $levels 0;
  85.                     foreach ($this->levels as $type => $log) {
  86.                         $levels |= $type;
  87.                     }
  88.                 } else {
  89.                     $levels $this->levels;
  90.                 }
  91.                 if ($this->scream) {
  92.                     $handler->screamAt($levels);
  93.                 }
  94.                 if ($this->scope) {
  95.                     $handler->scopeAt($levels & ~\E_USER_DEPRECATED & ~\E_DEPRECATED);
  96.                 } else {
  97.                     $handler->scopeAt(0true);
  98.                 }
  99.                 $this->logger $this->deprecationLogger $this->levels null;
  100.             }
  101.             if (null !== $this->throwAt) {
  102.                 $handler->throwAt($this->throwAttrue);
  103.             }
  104.         }
  105.         if (!$this->exceptionHandler) {
  106.             if ($event instanceof KernelEvent) {
  107.                 if (method_exists($kernel $event->getKernel(), 'terminateWithException')) {
  108.                     $request $event->getRequest();
  109.                     $hasRun = &$this->hasTerminatedWithException;
  110.                     $this->exceptionHandler = static function (\Throwable $e) use ($kernel$request, &$hasRun) {
  111.                         if ($hasRun) {
  112.                             throw $e;
  113.                         }
  114.                         $hasRun true;
  115.                         $kernel->terminateWithException($e$request);
  116.                     };
  117.                 }
  118.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  119.                 $output $event->getOutput();
  120.                 if ($output instanceof ConsoleOutputInterface) {
  121.                     $output $output->getErrorOutput();
  122.                 }
  123.                 $this->exceptionHandler = static function (\Throwable $e) use ($app$output) {
  124.                     $app->renderThrowable($e$output);
  125.                 };
  126.             }
  127.         }
  128.         if ($this->exceptionHandler) {
  129.             if ($handler instanceof ErrorHandler) {
  130.                 $handler->setExceptionHandler($this->exceptionHandler);
  131.             }
  132.             $this->exceptionHandler null;
  133.         }
  134.     }
  135.     private function setDefaultLoggers(ErrorHandler $handler): void
  136.     {
  137.         if (\is_array($this->levels)) {
  138.             $levelsDeprecatedOnly = [];
  139.             $levelsWithoutDeprecated = [];
  140.             foreach ($this->levels as $type => $log) {
  141.                 if (\E_DEPRECATED == $type || \E_USER_DEPRECATED == $type) {
  142.                     $levelsDeprecatedOnly[$type] = $log;
  143.                 } else {
  144.                     $levelsWithoutDeprecated[$type] = $log;
  145.                 }
  146.             }
  147.         } else {
  148.             $levelsDeprecatedOnly $this->levels & (\E_DEPRECATED \E_USER_DEPRECATED);
  149.             $levelsWithoutDeprecated $this->levels & ~\E_DEPRECATED & ~\E_USER_DEPRECATED;
  150.         }
  151.         $defaultLoggerLevels $this->levels;
  152.         if ($this->deprecationLogger && $levelsDeprecatedOnly) {
  153.             $handler->setDefaultLogger($this->deprecationLogger$levelsDeprecatedOnly);
  154.             $defaultLoggerLevels $levelsWithoutDeprecated;
  155.         }
  156.         if ($this->logger && $defaultLoggerLevels) {
  157.             $handler->setDefaultLogger($this->logger$defaultLoggerLevels);
  158.         }
  159.     }
  160.     public static function getSubscribedEvents(): array
  161.     {
  162.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  163.         if (\defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  164.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  165.         }
  166.         return $events;
  167.     }
  168. }