src/Controller/ExceptionController.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Tools\LocaleHelper;
  4. use Symfony\Bundle\TwigBundle\Controller\ExceptionController as TwigExceptionController;
  5. use Symfony\Component\Debug\Exception\FlattenException;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  9. use Twig\Environment as Twig;
  10. class ExceptionController extends TwigExceptionController
  11. {
  12.     /**
  13.      * @var LocaleHelper
  14.      */
  15.     private $localeHelper;
  16.     /**
  17.      * @param Twig $twig
  18.      * @param bool $debug Show error (false) or exception (true) pages by default
  19.      * @param LocaleHelper $localeHelper
  20.      */
  21.     public function __construct(Twig $twigbool $debug=trueLocaleHelper $localeHelper)
  22.     {
  23.         parent::__construct($twig$debug);
  24.         $this->localeHelper $localeHelper;
  25.     }
  26.     /**
  27.      * @param Request $request
  28.      * @param FlattenException $exception
  29.      * @param DebugLoggerInterface|null $logger
  30.      *
  31.      * @return Response
  32.      * @throws
  33.      * @todo Theme
  34.      */
  35.     public function showAction(Request $requestFlattenException $exception, ?DebugLoggerInterface $logger null)
  36.     {
  37.         $currentContent $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
  38.         $showException $request->attributes->get('showException'$this->debug); // As opposed to an additional parameter, this maintains BC
  39.         $code $exception->getStatusCode();
  40.         $route $request->get('_route');
  41.         if ($showException || $route === 'api_synchronize') {
  42.             return parent::showAction($request$exception);
  43.         }
  44.         $locale $this->localeHelper->guessLocaleFromUri($request->getRequestUri());
  45.         if (!empty($locale)) {
  46.             $request->setLocale($locale);
  47.         }
  48.         $template 'bundles/TwigBundle/Exception/error.html.twig';
  49.         if ($code === 404) {
  50.             $template 'bundles/TwigBundle/Exception/error404.html.twig';
  51.         }
  52.         return new Response($this->twig->render(
  53.             $template,
  54.             array(
  55.                 'status_code' => $code,
  56.                 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
  57.                 'exception' => $exception,
  58.                 'logger' => $logger,
  59.                 'currentContent' => $currentContent
  60.             )
  61.         ), 200, array('Content-Type' => $request->getMimeType($request->getRequestFormat()) ?: 'text/html'));
  62.     }
  63. }