vendor/symfony/symfony/src/Symfony/Component/HttpKernel/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\Debug\Exception\FatalThrowableError;
  16. use Symfony\Component\ErrorHandler\ErrorHandler;
  17. use Symfony\Component\EventDispatcher\Event;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  20. use Symfony\Component\HttpKernel\Event\KernelEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. /**
  23.  * Configures errors and exceptions handlers.
  24.  *
  25.  * @author Nicolas Grekas <p@tchwork.com>
  26.  *
  27.  * @final since Symfony 4.4
  28.  */
  29. class DebugHandlersListener implements EventSubscriberInterface
  30. {
  31.     private $exceptionHandler;
  32.     private $logger;
  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 true)
  49.     {
  50.         $this->exceptionHandler $exceptionHandler;
  51.         $this->logger $logger;
  52.         $this->levels null === $levels E_ALL $levels;
  53.         $this->throwAt = \is_int($throwAt) ? $throwAt : (null === $throwAt null : ($throwAt E_ALL null));
  54.         $this->scream $scream;
  55.         $this->fileLinkFormat $fileLinkFormat;
  56.         $this->scope $scope;
  57.     }
  58.     /**
  59.      * Configures the error handler.
  60.      */
  61.     public function configure(Event $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 ($this->logger || null !== $this->throwAt) {
  74.             if ($handler instanceof ErrorHandler) {
  75.                 if ($this->logger) {
  76.                     $handler->setDefaultLogger($this->logger$this->levels);
  77.                     if (\is_array($this->levels)) {
  78.                         $levels 0;
  79.                         foreach ($this->levels as $type => $log) {
  80.                             $levels |= $type;
  81.                         }
  82.                     } else {
  83.                         $levels $this->levels;
  84.                     }
  85.                     if ($this->scream) {
  86.                         $handler->screamAt($levels);
  87.                     }
  88.                     if ($this->scope) {
  89.                         $handler->scopeAt($levels & ~E_USER_DEPRECATED & ~E_DEPRECATED);
  90.                     } else {
  91.                         $handler->scopeAt(0true);
  92.                     }
  93.                     $this->logger $this->levels null;
  94.                 }
  95.                 if (null !== $this->throwAt) {
  96.                     $handler->throwAt($this->throwAttrue);
  97.                 }
  98.             }
  99.         }
  100.         if (!$this->exceptionHandler) {
  101.             if ($event instanceof KernelEvent) {
  102.                 if (method_exists($kernel $event->getKernel(), 'terminateWithException')) {
  103.                     $request $event->getRequest();
  104.                     $hasRun = &$this->hasTerminatedWithException;
  105.                     $this->exceptionHandler = static function (\Throwable $e) use ($kernel$request, &$hasRun) {
  106.                         if ($hasRun) {
  107.                             throw $e;
  108.                         }
  109.                         $hasRun true;
  110.                         $kernel->terminateWithException($e$request);
  111.                     };
  112.                 }
  113.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  114.                 $output $event->getOutput();
  115.                 if ($output instanceof ConsoleOutputInterface) {
  116.                     $output $output->getErrorOutput();
  117.                 }
  118.                 $this->exceptionHandler = static function (\Throwable $e) use ($app$output) {
  119.                     if (method_exists($app'renderThrowable')) {
  120.                         $app->renderThrowable($e$output);
  121.                     } else {
  122.                         if (!$e instanceof \Exception) {
  123.                             $e = new FatalThrowableError($e);
  124.                         }
  125.                         $app->renderException($e$output);
  126.                     }
  127.                 };
  128.             }
  129.         }
  130.         if ($this->exceptionHandler) {
  131.             if ($handler instanceof ErrorHandler) {
  132.                 $handler->setExceptionHandler($this->exceptionHandler);
  133.             }
  134.             $this->exceptionHandler null;
  135.         }
  136.     }
  137.     public static function getSubscribedEvents()
  138.     {
  139.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  140.         if (\defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  141.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  142.         }
  143.         return $events;
  144.     }
  145. }