vendor/symfony/http-kernel/EventListener/ExceptionListener.php line 58

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\Exception\FlattenException;
  13. use Symfony\Component\Debug\ExceptionHandler;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  19. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  20. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  21. use Symfony\Component\HttpKernel\HttpKernelInterface;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  24. /**
  25.  * ExceptionListener.
  26.  *
  27.  * @author Fabien Potencier <fabien@symfony.com>
  28.  */
  29. class ExceptionListener implements EventSubscriberInterface
  30. {
  31.     protected $controller;
  32.     protected $logger;
  33.     protected $debug;
  34.     private $charset;
  35.     private $fileLinkFormat;
  36.     private $isTerminating false;
  37.     public function __construct($controllerLoggerInterface $logger null$debug falsestring $charset null$fileLinkFormat null)
  38.     {
  39.         $this->controller $controller;
  40.         $this->logger $logger;
  41.         $this->debug $debug;
  42.         $this->charset $charset;
  43.         $this->fileLinkFormat $fileLinkFormat;
  44.     }
  45.     public function logKernelException(GetResponseForExceptionEvent $event)
  46.     {
  47.         $e FlattenException::create($event->getException());
  48.         $this->logException($event->getException(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s'$e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
  49.     }
  50.     public function onKernelException(GetResponseForExceptionEvent $event)
  51.     {
  52.         if (null === $this->controller) {
  53.             if (!$event->isMasterRequest()) {
  54.                 return;
  55.             }
  56.             if (!$this->isTerminating) {
  57.                 $this->isTerminating true;
  58.                 return;
  59.             }
  60.             $this->isTerminating false;
  61.         }
  62.         $exception $event->getException();
  63.         $request $this->duplicateRequest($exception$event->getRequest());
  64.         $eventDispatcher = \func_num_args() > func_get_arg(2) : null;
  65.         try {
  66.             $response $event->getKernel()->handle($requestHttpKernelInterface::SUB_REQUESTfalse);
  67.         } catch (\Exception $e) {
  68.             $f FlattenException::create($e);
  69.             $this->logException($esprintf('Exception thrown when handling an exception (%s: %s at %s line %s)'$f->getClass(), $f->getMessage(), $e->getFile(), $e->getLine()));
  70.             $prev $e;
  71.             do {
  72.                 if ($exception === $wrapper $prev) {
  73.                     throw $e;
  74.                 }
  75.             } while ($prev $wrapper->getPrevious());
  76.             $prev = new \ReflectionProperty($wrapper instanceof \Exception ? \Exception::class : \Error::class, 'previous');
  77.             $prev->setAccessible(true);
  78.             $prev->setValue($wrapper$exception);
  79.             throw $e;
  80.         }
  81.         $event->setResponse($response);
  82.         if ($this->debug && $eventDispatcher instanceof EventDispatcherInterface) {
  83.             $cspRemovalListener = function (FilterResponseEvent $event) use (&$cspRemovalListener$eventDispatcher) {
  84.                 $event->getResponse()->headers->remove('Content-Security-Policy');
  85.                 $eventDispatcher->removeListener(KernelEvents::RESPONSE$cspRemovalListener);
  86.             };
  87.             $eventDispatcher->addListener(KernelEvents::RESPONSE$cspRemovalListener, -128);
  88.         }
  89.     }
  90.     public function reset()
  91.     {
  92.         $this->isTerminating false;
  93.     }
  94.     public static function getSubscribedEvents()
  95.     {
  96.         return [
  97.             KernelEvents::EXCEPTION => [
  98.                 ['logKernelException'0],
  99.                 ['onKernelException', -128],
  100.             ],
  101.         ];
  102.     }
  103.     /**
  104.      * Logs an exception.
  105.      *
  106.      * @param \Exception $exception The \Exception instance
  107.      * @param string     $message   The error message to log
  108.      */
  109.     protected function logException(\Exception $exception$message)
  110.     {
  111.         if (null !== $this->logger) {
  112.             if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
  113.                 $this->logger->critical($message, ['exception' => $exception]);
  114.             } else {
  115.                 $this->logger->error($message, ['exception' => $exception]);
  116.             }
  117.         }
  118.     }
  119.     /**
  120.      * Clones the request for the exception.
  121.      *
  122.      * @param \Exception $exception The thrown exception
  123.      * @param Request    $request   The original request
  124.      *
  125.      * @return Request The cloned request
  126.      */
  127.     protected function duplicateRequest(\Exception $exceptionRequest $request)
  128.     {
  129.         $attributes = [
  130.             'exception' => $exception FlattenException::create($exception),
  131.             '_controller' => $this->controller ?: function () use ($exception) {
  132.                 $handler = new ExceptionHandler($this->debug$this->charset$this->fileLinkFormat);
  133.                 return new Response($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders());
  134.             },
  135.             'logger' => $this->logger instanceof DebugLoggerInterface $this->logger null,
  136.         ];
  137.         $request $request->duplicate(nullnull$attributes);
  138.         $request->setMethod('GET');
  139.         return $request;
  140.     }
  141. }