vendor/symfony/symfony/src/Symfony/Component/HttpKernel/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\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 <p@tchwork.com>
  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.     private $hasTerminatedWithException;
  37.     /**
  38.      * @param callable|null        $exceptionHandler A handler that will be called on Exception
  39.      * @param LoggerInterface|null $logger           A PSR-3 logger
  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|array         $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$throwAt E_ALL$scream true$fileLinkFormat null$scope true)
  47.     {
  48.         $this->exceptionHandler $exceptionHandler;
  49.         $this->logger $logger;
  50.         $this->levels null === $levels E_ALL $levels;
  51.         $this->throwAt is_numeric($throwAt) ? (int) $throwAt : (null === $throwAt null : ($throwAt E_ALL null));
  52.         $this->scream = (bool) $scream;
  53.         $this->fileLinkFormat $fileLinkFormat;
  54.         $this->scope = (bool) $scope;
  55.     }
  56.     /**
  57.      * Configures the error handler.
  58.      */
  59.     public function configure(Event $event null)
  60.     {
  61.         if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  62.             return;
  63.         }
  64.         $this->firstCall $this->hasTerminatedWithException false;
  65.         $handler set_exception_handler('var_dump');
  66.         $handler is_array($handler) ? $handler[0] : null;
  67.         restore_exception_handler();
  68.         if ($this->logger || null !== $this->throwAt) {
  69.             if ($handler instanceof ErrorHandler) {
  70.                 if ($this->logger) {
  71.                     $handler->setDefaultLogger($this->logger$this->levels);
  72.                     if (is_array($this->levels)) {
  73.                         $levels 0;
  74.                         foreach ($this->levels as $type => $log) {
  75.                             $levels |= $type;
  76.                         }
  77.                     } else {
  78.                         $levels $this->levels;
  79.                     }
  80.                     if ($this->scream) {
  81.                         $handler->screamAt($levels);
  82.                     }
  83.                     if ($this->scope) {
  84.                         $handler->scopeAt($levels & ~E_USER_DEPRECATED & ~E_DEPRECATED);
  85.                     } else {
  86.                         $handler->scopeAt(0true);
  87.                     }
  88.                     $this->logger $this->levels null;
  89.                 }
  90.                 if (null !== $this->throwAt) {
  91.                     $handler->throwAt($this->throwAttrue);
  92.                 }
  93.             }
  94.         }
  95.         if (!$this->exceptionHandler) {
  96.             if ($event instanceof KernelEvent) {
  97.                 if (method_exists($kernel $event->getKernel(), 'terminateWithException')) {
  98.                     $request $event->getRequest();
  99.                     $hasRun = &$this->hasTerminatedWithException;
  100.                     $this->exceptionHandler = function (\Exception $e) use ($kernel$request, &$hasRun) {
  101.                         if ($hasRun) {
  102.                             throw $e;
  103.                         }
  104.                         $hasRun true;
  105.                         $kernel->terminateWithException($e$request);
  106.                     };
  107.                 }
  108.             } elseif ($event instanceof ConsoleEvent && $app $event->getCommand()->getApplication()) {
  109.                 $output $event->getOutput();
  110.                 if ($output instanceof ConsoleOutputInterface) {
  111.                     $output $output->getErrorOutput();
  112.                 }
  113.                 $this->exceptionHandler = function ($e) use ($app$output) {
  114.                     $app->renderException($e$output);
  115.                 };
  116.             }
  117.         }
  118.         if ($this->exceptionHandler) {
  119.             if ($handler instanceof ErrorHandler) {
  120.                 $h $handler->setExceptionHandler('var_dump');
  121.                 if (is_array($h) && $h[0] instanceof ExceptionHandler) {
  122.                     $handler->setExceptionHandler($h);
  123.                     $handler $h[0];
  124.                 } else {
  125.                     $handler->setExceptionHandler($this->exceptionHandler);
  126.                 }
  127.             }
  128.             if ($handler instanceof ExceptionHandler) {
  129.                 $handler->setHandler($this->exceptionHandler);
  130.                 if (null !== $this->fileLinkFormat) {
  131.                     $handler->setFileLinkFormat($this->fileLinkFormat);
  132.                 }
  133.             }
  134.             $this->exceptionHandler null;
  135.         }
  136.     }
  137.     public static function getSubscribedEvents()
  138.     {
  139.         $events = array(KernelEvents::REQUEST => array('configure'2048));
  140.         if ('cli' === PHP_SAPI && defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
  141.             $events[ConsoleEvents::COMMAND] = array('configure'2048);
  142.         }
  143.         return $events;
  144.     }
  145. }