lib/boab/cms-bundle/src/View/EventListener/ExceptionListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\View\EventListener;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Boab\CmsBundle\View\ViewManagerInterface;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  9. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  10. class ExceptionListener
  11. {
  12.     private $viewManager;
  13.     public function __construct(ViewManagerInterface $viewManager)
  14.     {
  15.         $this->viewManager $viewManager;
  16.     }
  17.     public function onKernelException(ExceptionEvent $event
  18.     {
  19.         $exception $event->getThrowable();
  20.         $request $event->getRequest();
  21.         $response $this->handleException($exception$request);
  22.         if($response instanceof Response){
  23.             $event->setResponse($response);
  24.         }
  25.     }
  26.     
  27.     private function handleException($exception$request)
  28.     {
  29.         if ($exception instanceof NotFoundHttpException) {
  30.             return $this->getHtmlResponse(404$exception'Page Not Found');
  31.         }
  32.         return;
  33.     }
  34.     private function getHtmlResponse($code$exception$title)
  35.     {
  36.         $statusCode = ($exception instanceof HttpExceptionInterface) ? $exception->getStatusCode() : $exception->getCode();
  37.         $view $this->viewManager->load(sprintf('@BoabCms/Exception/exception_%s.html.twig'$code));
  38.         $view->exception $exception;
  39.         $view->pageTitle $title;
  40.         $response = new Response($view->render());
  41.         $response->setStatusCode($code);
  42.         return $response;
  43.     }