src/EventListener/AppExceptionListener.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Exception\AppException;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Boab\CmsBundle\View\ViewManagerInterface;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  8. class AppExceptionListener
  9. {
  10.     private $viewManager;
  11.     public function __construct(ViewManagerInterface $viewManager)
  12.     {
  13.         $this->viewManager $viewManager;
  14.     }
  15.     public function onKernelException(ExceptionEvent $event
  16.     {
  17.         $exception $event->getThrowable();
  18.         if(!$exception instanceof AppException){
  19.             return;
  20.         }
  21.         $statusCode = ($exception instanceof HttpExceptionInterface) ? $exception->getStatusCode() : $exception->getCode();
  22.         $view $this->viewManager->load(sprintf('exception/exception_%s.html.twig'$statusCode));
  23.         $view->exception $exception;
  24.         $response = new Response($view->render());
  25.         $response->setStatusCode($statusCode);
  26.         $event->setResponse($response);
  27.     }