vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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\Debug\ErrorHandler;
  13. use Symfony\Component\Debug\ExceptionHandler;
  14. use Symfony\Component\EventDispatcher\Event;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpKernel\Event\KernelEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Console\ConsoleEvents;
  19. use Symfony\Component\Console\Event\ConsoleEvent;
  20. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  21. /**
  22.  * Configures errors and exceptions handlers.
  23.  *
  24.  * @author Nicolas Grekas <[email protected]>
  25.  */
  26. class DebugHandlersListener implements EventSubscriberInterface
  27. {
  28.     private $exceptionHandler;
  29.     private $logger;
  30.     private $levels;
  31.     private $throwAt;
  32.     private $scream;
  33.     private $fileLinkFormat;
  34.     private $scope;
  35.     private $firstCall true;
  36.     /**
  37.      * @param callable|null        $exceptionHandler A handler that will be called on Exception
  38.      * @param LoggerInterface|null $logger           A PSR-3 logger
  39.      * @param array|int            $levels           An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  40.      * @param int|null             $throwAt          Thrown errors in a bit field of E_* constants, or null to keep the current value
  41.      * @param bool                 $scream           Enables/disables screaming mode, where even silenced errors are logged
  42.      * @param string|array         $fileLinkFormat   The format for links to source files
  43.      * @param bool                 $scope            Enables/disables scoping mode
  44.      */
  45.     public function __construct(callable $exceptionHandler nullLoggerInterface $logger null$levels E_ALL$throwAt E_ALL$scream true$fileLinkFormat null$scope true)
  46.     {
  47.         $this->exceptionHandler $exceptionHandler;
  48.         $this->logger $logger;
  49.         $this->levels null === $levels E_ALL $levels;
  50.         $this->throwAt is_numeric($throwAt) ? (int) $throwAt : (null === $throwAt null : ($throwAt E_ALL null));
  51.         $this->scream = (bool) $scream;
  52.         $this->fileLinkFormat $fileLinkFormat;
  53.         $this->scope = (bool) $scope;
  54.     }
  55.     /**
  56.      * Configures the error handler.
  57.      */
  58.     public function configure(Event $event null)
  59.     {
  60.         if (!$this->firstCall) {
  61.             return;
  62.         }
  63.         $this->firstCall false;
  64.         if ($this->logger || null !== $this->throwAt) {
  65.             $handler set_error_handler('var_dump');
  66.             $handler is_array($handler) ? $handler[0] : null;
  67.             restore_error_handler();
  68.             if ($handler instanceof ErrorHandler) {
  69.                 if ($this->logger) {
  70.                     $handler->setDefaultLogger($this->logger$this->levels);
  71.                     if (is_array($this->levels)) {
  72.                         $levels 0;
  73.                         foreach ($this->levels as $type => $log) {
  74.                             $levels |= $type;
  75.                         }
  76.                     } else {
  77.                         $levels $this->levels;
  78.                     }
  79.                     if ($this->scream) {
  80.                         $handler->screamAt($levels);
  81.                     }
  82.                     if ($this->scope) {
  83.                         $handler->scopeAt($levels & ~E_USER_DEPRECATED & ~E_DEPRECATED);
  84.                     } else {
  85.                         $handler->scopeAt(0true);
  86.                     }
  87.                     $this->logger $this->levels null;
  88.                 }
  89.                 if (null !== $this->throwAt) {
  90.                     $handler->throwAt($this->throwAttrue);
  91.                 }
  92.             }
  93.         }
  94.         if (!$this->exceptionHandler) {
  95.             if ($event instanceof KernelEvent) {
  96.                 if (method_exists($event->getKernel(), 'terminateWithException')) {
  97.                     $this->exceptionHandler = array($event->getKernel(), 'terminateWithException');
  98.                 }
  99.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  100.                 $output $event->getOutput();
  101.                 if ($output instanceof ConsoleOutputInterface) {
  102.                     $output $output->getErrorOutput();
  103.                 }
  104.                 $this->exceptionHandler = function ($e) use ($app$output) {
  105.                     $app->renderException($e$output);
  106.                 };
  107.             }
  108.         }
  109.         if ($this->exceptionHandler) {
  110.             $handler set_exception_handler('var_dump');
  111.             $handler is_array($handler) ? $handler[0] : null;
  112.             restore_exception_handler();
  113.             if ($handler instanceof ErrorHandler) {
  114.                 $h $handler->setExceptionHandler('var_dump') ?: $this->exceptionHandler;
  115.                 $handler->setExceptionHandler($h);
  116.                 $handler is_array($h) ? $h[0] : null;
  117.             }
  118.             if ($handler instanceof ExceptionHandler) {
  119.                 $handler->setHandler($this->exceptionHandler);
  120.                 if (null !== $this->fileLinkFormat) {
  121.                     $handler->setFileLinkFormat($this->fileLinkFormat);
  122.                 }
  123.             }
  124.             $this->exceptionHandler null;
  125.         }
  126.     }
  127.     public static function getSubscribedEvents()
  128.     {
  129.         $events = array(KernelEvents::REQUEST => array('configure'2048));
  130.         if ('cli' === PHP_SAPI && defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  131.             $events[ConsoleEvents::COMMAND] = array('configure'2048);
  132.         }
  133.         return $events;
  134.     }
  135. }