vendor/friendsofsymfony/rest-bundle/EventListener/ExceptionListener.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\RestBundle\EventListener;
  11. use FOS\RestBundle\FOSRestBundle;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
  14. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  18. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  19. use Symfony\Component\HttpKernel\EventListener\ExceptionListener as LegacyExceptionListener;
  20. use Symfony\Component\HttpKernel\EventListener\ErrorListener;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. /**
  23.  * ExceptionListener.
  24.  *
  25.  * @author Ener-Getick <egetick@gmail.com>
  26.  *
  27.  * @internal
  28.  */
  29. class ExceptionListener implements EventSubscriberInterface
  30. {
  31.     private $exceptionListener;
  32.     private $dispatcher;
  33.     public function __construct($controller, ?LoggerInterface $loggerEventDispatcherInterface $dispatcher)
  34.     {
  35.         if (class_exists(ErrorListener::class)) {
  36.             $this->exceptionListener = new ErrorListener($controller$logger);
  37.         } else {
  38.             $this->exceptionListener = new LegacyExceptionListener($controller$logger);
  39.         }
  40.         $this->dispatcher $dispatcher;
  41.     }
  42.     /**
  43.      * @param ExceptionEvent $event
  44.      */
  45.     public function onKernelException($event)
  46.     {
  47.         $request $event->getRequest();
  48.         if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTEtrue)) {
  49.             return;
  50.         }
  51.         if (method_exists($event'getThrowable')) {
  52.             $exception $event->getThrowable();
  53.         } else {
  54.             $exception $event->getException();
  55.         }
  56.         $controllerArgsListener = function ($event) use (&$controllerArgsListener$exception) {
  57.             /** @var ControllerArgumentsEvent $event */
  58.             $arguments $event->getArguments();
  59.             foreach ($arguments as $k => $argument) {
  60.                 if ($argument instanceof FlattenException || $argument instanceof LegacyFlattenException) {
  61.                     $arguments[$k] = $exception;
  62.                     $event->setArguments($arguments);
  63.                     break;
  64.                 }
  65.             }
  66.             $this->dispatcher->removeListener(KernelEvents::CONTROLLER_ARGUMENTS$controllerArgsListener);
  67.         };
  68.         $this->dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS$controllerArgsListener, -100);
  69.         $this->exceptionListener->onKernelException($event);
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public static function getSubscribedEvents()
  75.     {
  76.         return array(
  77.             KernelEvents::EXCEPTION => array('onKernelException', -100),
  78.         );
  79.     }
  80. }