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

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 $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 must support \Throwable instances that will be called on Exception
  40.      * @param array|int                     $levels           An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  41.      * @param int|null                      $throwAt          Thrown errors in a bit field of E_* constants, or null to keep the current value
  42.      * @param bool                          $scream           Enables/disables screaming mode, where even silenced errors are logged
  43.      * @param string|FileLinkFormatter|null $fileLinkFormat   The format for links to source files
  44.      * @param bool                          $scope            Enables/disables scoping mode
  45.      */
  46.     public function __construct(callable $exceptionHandler nullLoggerInterface $logger null$levels E_ALL, ?int $throwAt E_ALLbool $scream true$fileLinkFormat nullbool $scope true)
  47.     {
  48.         $this->exceptionHandler $exceptionHandler;
  49.         $this->logger $logger;
  50.         $this->levels null === $levels E_ALL $levels;
  51.         $this->throwAt = \is_int($throwAt) ? $throwAt : (null === $throwAt null : ($throwAt E_ALL null));
  52.         $this->scream $scream;
  53.         $this->fileLinkFormat $fileLinkFormat;
  54.         $this->scope $scope;
  55.     }
  56.     /**
  57.      * Configures the error handler.
  58.      */
  59.     public function configure(object $event null)
  60.     {
  61.         if ($event instanceof ConsoleEvent && !\in_array(\PHP_SAPI, ['cli''phpdbg'], true)) {
  62.             return;
  63.         }
  64.         if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  65.             return;
  66.         }
  67.         $this->firstCall $this->hasTerminatedWithException false;
  68.         $handler set_exception_handler('var_dump');
  69.         $handler = \is_array($handler) ? $handler[0] : null;
  70.         restore_exception_handler();
  71.         if ($this->logger || null !== $this->throwAt) {
  72.             if ($handler instanceof ErrorHandler) {
  73.                 if ($this->logger) {
  74.                     $handler->setDefaultLogger($this->logger$this->levels);
  75.                     if (\is_array($this->levels)) {
  76.                         $levels 0;
  77.                         foreach ($this->levels as $type => $log) {
  78.                             $levels |= $type;
  79.                         }
  80.                     } else {
  81.                         $levels $this->levels;
  82.                     }
  83.                     if ($this->scream) {
  84.                         $handler->screamAt($levels);
  85.                     }
  86.                     if ($this->scope) {
  87.                         $handler->scopeAt($levels & ~E_USER_DEPRECATED & ~E_DEPRECATED);
  88.                     } else {
  89.                         $handler->scopeAt(0true);
  90.                     }
  91.                     $this->logger $this->levels null;
  92.                 }
  93.                 if (null !== $this->throwAt) {
  94.                     $handler->throwAt($this->throwAttrue);
  95.                 }
  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.     public static function getSubscribedEvents(): array
  129.     {
  130.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  131.         if (\defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  132.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  133.         }
  134.         return $events;
  135.     }
  136. }