src/Listener/ExceptionListener.php line 32

Open in your IDE?
  1. <?php
  2.     namespace App\Listener;
  3.     use Doctrine\DBAL\Exception\InvalidFieldNameException;
  4.     use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5.     use Symfony\Component\HttpFoundation\RedirectResponse;
  6.     use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7.     use Symfony\Component\HttpKernel\KernelEvents;
  8.     use Symfony\Component\HttpKernel\KernelInterface;
  9.     use Symfony\Component\Routing\RouterInterface;
  10.     class ExceptionListener implements EventSubscriberInterface
  11.     {
  12.         private RouterInterface $router;
  13.         private string          $environment;
  14.         public function __constructKernelInterface $kernelRouterInterface $router )
  15.         {
  16.             $this->environment $kernel->getEnvironment();
  17.             $this->router      $router;
  18.         }
  19.         public static function getSubscribedEvents(): array
  20.         {
  21.             return [
  22.                 KernelEvents::EXCEPTION => 'onKernelException',
  23.             ];
  24.         }
  25.         public function onKernelExceptionExceptionEvent $event )
  26.         {
  27.             if ( $this->environment !== 'dev' ) {
  28.                 return;
  29.             }
  30.             $exception $event->getThrowable();
  31.             if ( $exception instanceof InvalidFieldNameException ) {
  32.                 // Génère la route dev_update_bdd
  33.                 $route $this->router->generate'dev_update_bdd' );
  34.                 // Redirige vers la route dev_update_bdd
  35.                 $response = new RedirectResponse$route );
  36.                 $event->setResponse$response );
  37.             }
  38.         }
  39.     }