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

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 as LegacyErrorHandler;
  16. use Symfony\Component\Debug\Exception\FatalThrowableError;
  17. use Symfony\Component\ErrorHandler\ErrorHandler;
  18. use Symfony\Component\EventDispatcher\Event;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  21. use Symfony\Component\HttpKernel\Event\KernelEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. /**
  24.  * Configures errors and exceptions handlers.
  25.  *
  26.  * @author Nicolas Grekas <p@tchwork.com>
  27.  *
  28.  * @final since Symfony 4.4
  29.  */
  30. class DebugHandlersListener implements EventSubscriberInterface
  31. {
  32.     private $earlyHandler;
  33.     private $exceptionHandler;
  34.     private $logger;
  35.     private $levels;
  36.     private $throwAt;
  37.     private $scream;
  38.     private $fileLinkFormat;
  39.     private $scope;
  40.     private $firstCall true;
  41.     private $hasTerminatedWithException;
  42.     /**
  43.      * @param callable|null                 $exceptionHandler A handler that must support \Throwable instances that will be called on Exception
  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 true)
  51.     {
  52.         $handler set_exception_handler('var_dump');
  53.         $this->earlyHandler = \is_array($handler) ? $handler[0] : null;
  54.         restore_exception_handler();
  55.         $this->exceptionHandler $exceptionHandler;
  56.         $this->logger $logger;
  57.         $this->levels $levels ?? \E_ALL;
  58.         $this->throwAt = \is_int($throwAt) ? $throwAt : (null === $throwAt null : ($throwAt ? \E_ALL null));
  59.         $this->scream $scream;
  60.         $this->fileLinkFormat $fileLinkFormat;
  61.         $this->scope $scope;
  62.     }
  63.     /**
  64.      * Configures the error handler.
  65.      */
  66.     public function configure(Event $event null)
  67.     {
  68.         if ($event instanceof ConsoleEvent && !\in_array(\PHP_SAPI, ['cli''phpdbg'], true)) {
  69.             return;
  70.         }
  71.         if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  72.             return;
  73.         }
  74.         $this->firstCall $this->hasTerminatedWithException false;
  75.         $handler set_exception_handler('var_dump');
  76.         $handler = \is_array($handler) ? $handler[0] : null;
  77.         restore_exception_handler();
  78.         if (!$handler instanceof ErrorHandler && !$handler instanceof LegacyErrorHandler) {
  79.             $handler $this->earlyHandler;
  80.         }
  81.         if ($this->logger || null !== $this->throwAt) {
  82.             if ($handler instanceof ErrorHandler || $handler instanceof LegacyErrorHandler) {
  83.                 if ($this->logger) {
  84.                     $handler->setDefaultLogger($this->logger$this->levels);
  85.                     if (\is_array($this->levels)) {
  86.                         $levels 0;
  87.                         foreach ($this->levels as $type => $log) {
  88.                             $levels |= $type;
  89.                         }
  90.                     } else {
  91.                         $levels $this->levels;
  92.                     }
  93.                     if ($this->scream) {
  94.                         $handler->screamAt($levels);
  95.                     }
  96.                     if ($this->scope) {
  97.                         $handler->scopeAt($levels & ~\E_USER_DEPRECATED & ~\E_DEPRECATED);
  98.                     } else {
  99.                         $handler->scopeAt(0true);
  100.                     }
  101.                     $this->logger $this->levels null;
  102.                 }
  103.                 if (null !== $this->throwAt) {
  104.                     $handler->throwAt($this->throwAttrue);
  105.                 }
  106.             }
  107.         }
  108.         if (!$this->exceptionHandler) {
  109.             if ($event instanceof KernelEvent) {
  110.                 if (method_exists($kernel $event->getKernel(), 'terminateWithException')) {
  111.                     $request $event->getRequest();
  112.                     $hasRun = &$this->hasTerminatedWithException;
  113.                     $this->exceptionHandler = static function (\Throwable $e) use ($kernel$request, &$hasRun) {
  114.                         if ($hasRun) {
  115.                             throw $e;
  116.                         }
  117.                         $hasRun true;
  118.                         $kernel->terminateWithException($e$request);
  119.                     };
  120.                 }
  121.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  122.                 $output $event->getOutput();
  123.                 if ($output instanceof ConsoleOutputInterface) {
  124.                     $output $output->getErrorOutput();
  125.                 }
  126.                 $this->exceptionHandler = static function (\Throwable $e) use ($app$output) {
  127.                     if (method_exists($app'renderThrowable')) {
  128.                         $app->renderThrowable($e$output);
  129.                     } else {
  130.                         if (!$e instanceof \Exception) {
  131.                             $e = new FatalThrowableError($e);
  132.                         }
  133.                         $app->renderException($e$output);
  134.                     }
  135.                 };
  136.             }
  137.         }
  138.         if ($this->exceptionHandler) {
  139.             if ($handler instanceof ErrorHandler || $handler instanceof LegacyErrorHandler) {
  140.                 $handler->setExceptionHandler($this->exceptionHandler);
  141.             }
  142.             $this->exceptionHandler null;
  143.         }
  144.     }
  145.     public static function getSubscribedEvents()
  146.     {
  147.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  148.         if (\defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  149.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  150.         }
  151.         return $events;
  152.     }
  153. }