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

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