vendor/symfony/http-kernel/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 KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  64.             return;
  65.         }
  66.         $this->firstCall $this->hasTerminatedWithException false;
  67.         $handler set_exception_handler('var_dump');
  68.         $handler = \is_array($handler) ? $handler[0] : null;
  69.         restore_exception_handler();
  70.         if ($this->logger || null !== $this->throwAt) {
  71.             if ($handler instanceof ErrorHandler) {
  72.                 if ($this->logger) {
  73.                     $handler->setDefaultLogger($this->logger$this->levels);
  74.                     if (\is_array($this->levels)) {
  75.                         $levels 0;
  76.                         foreach ($this->levels as $type => $log) {
  77.                             $levels |= $type;
  78.                         }
  79.                     } else {
  80.                         $levels $this->levels;
  81.                     }
  82.                     if ($this->scream) {
  83.                         $handler->screamAt($levels);
  84.                     }
  85.                     if ($this->scope) {
  86.                         $handler->scopeAt($levels & ~E_USER_DEPRECATED & ~E_DEPRECATED);
  87.                     } else {
  88.                         $handler->scopeAt(0true);
  89.                     }
  90.                     $this->logger $this->levels null;
  91.                 }
  92.                 if (null !== $this->throwAt) {
  93.                     $handler->throwAt($this->throwAttrue);
  94.                 }
  95.             }
  96.         }
  97.         if (!$this->exceptionHandler) {
  98.             if ($event instanceof KernelEvent) {
  99.                 if (method_exists($kernel $event->getKernel(), 'terminateWithException')) {
  100.                     $request $event->getRequest();
  101.                     $hasRun = &$this->hasTerminatedWithException;
  102.                     $this->exceptionHandler = static function (\Throwable $e) use ($kernel$request, &$hasRun) {
  103.                         if ($hasRun) {
  104.                             throw $e;
  105.                         }
  106.                         $hasRun true;
  107.                         $kernel->terminateWithException($e$request);
  108.                     };
  109.                 }
  110.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  111.                 $output $event->getOutput();
  112.                 if ($output instanceof ConsoleOutputInterface) {
  113.                     $output $output->getErrorOutput();
  114.                 }
  115.                 $this->exceptionHandler = static function (\Throwable $e) use ($app$output) {
  116.                     if (method_exists($app'renderThrowable')) {
  117.                         $app->renderThrowable($e$output);
  118.                     } else {
  119.                         if (!$e instanceof \Exception) {
  120.                             $e = new FatalThrowableError($e);
  121.                         }
  122.                         $app->renderException($e$output);
  123.                     }
  124.                 };
  125.             }
  126.         }
  127.         if ($this->exceptionHandler) {
  128.             if ($handler instanceof ErrorHandler) {
  129.                 $handler->setExceptionHandler($this->exceptionHandler);
  130.             }
  131.             $this->exceptionHandler null;
  132.         }
  133.     }
  134.     public static function getSubscribedEvents()
  135.     {
  136.         $events = [KernelEvents::REQUEST => ['configure'2048]];
  137.         if ('cli' === \PHP_SAPI && \defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  138.             $events[ConsoleEvents::COMMAND] = ['configure'2048];
  139.         }
  140.         return $events;
  141.     }
  142. }