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

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