vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Controller/ExceptionController.php line 75

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\WebsiteBundle\Controller;
  11. use Sulu\Bundle\WebsiteBundle\Resolver\ParameterResolverInterface;
  12. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  13. use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
  14. use Symfony\Component\Debug\Exception\FlattenException;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  18. use Twig\Environment;
  19. /**
  20.  * Custom exception controller.
  21.  */
  22. class ExceptionController
  23. {
  24.     /**
  25.      * @var BaseExceptionController
  26.      */
  27.     private $exceptionController;
  28.     /**
  29.      * @var RequestAnalyzerInterface
  30.      */
  31.     private $requestAnalyzer;
  32.     /**
  33.      * @var ParameterResolverInterface
  34.      */
  35.     private $parameterResolver;
  36.     /**
  37.      * @var Environment
  38.      */
  39.     private $twig;
  40.     /**
  41.      * @var bool
  42.      */
  43.     private $debug;
  44.     /**
  45.      * @param bool $debug
  46.      */
  47.     public function __construct(
  48.         BaseExceptionController $exceptionController,
  49.         RequestAnalyzerInterface $requestAnalyzer,
  50.         ParameterResolverInterface $parameterResolver,
  51.         Environment $twig,
  52.         $debug
  53.     ) {
  54.         $this->exceptionController $exceptionController;
  55.         $this->requestAnalyzer $requestAnalyzer;
  56.         $this->parameterResolver $parameterResolver;
  57.         $this->twig $twig;
  58.         $this->debug $debug;
  59.     }
  60.     /**
  61.      * {@see BaseExceptionController::showAction()}.
  62.      *
  63.      * @param FlattenException $exception
  64.      */
  65.     public function showAction(
  66.         Request $request,
  67.         $exception,
  68.         DebugLoggerInterface $logger null
  69.     ) {
  70.         $code $exception->getStatusCode();
  71.         $template null;
  72.         if ($webspace $this->requestAnalyzer->getWebspace()) {
  73.             $template $webspace->getTemplate('error-' $code$request->getRequestFormat());
  74.             if (null === $template) {
  75.                 $template $webspace->getTemplate('error'$request->getRequestFormat());
  76.             }
  77.         }
  78.         $showException $request->attributes->get('showException'$this->debug);
  79.         if ($showException || null === $template || !$this->twig->getLoader()->exists($template)) {
  80.             return $this->exceptionController->showAction($request$exception$logger);
  81.         }
  82.         $context $this->parameterResolver->resolve(
  83.             [
  84.                 'status_code' => $code,
  85.                 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
  86.                 'exception' => $exception,
  87.                 'currentContent' => $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)),
  88.             ],
  89.             $this->requestAnalyzer
  90.         );
  91.         return new Response(
  92.             $this->twig->render(
  93.                 $template,
  94.                 $context
  95.             ),
  96.             $code
  97.         );
  98.     }
  99.     /**
  100.      * Returns and cleans output-buffer.
  101.      *
  102.      * @param int $startObLevel
  103.      *
  104.      * @return string
  105.      */
  106.     protected function getAndCleanOutputBuffering($startObLevel)
  107.     {
  108.         if (ob_get_level() <= $startObLevel) {
  109.             return '';
  110.         }
  111.         Response::closeOutputBuffers($startObLevel 1true);
  112.         return ob_get_clean();
  113.     }
  114. }