vendor/symfony/twig-bundle/Controller/ExceptionController.php line 59

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\TwigBundle\Controller;
  11. use Symfony\Component\Debug\Exception\FlattenException;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  15. use Twig\Environment;
  16. use Twig\Error\LoaderError;
  17. use Twig\Loader\ExistsLoaderInterface;
  18. use Twig\Loader\SourceContextLoaderInterface;
  19. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4, use "%s" instead.'ExceptionController::class, \Symfony\Component\HttpKernel\Controller\ErrorController::class), \E_USER_DEPRECATED);
  20. /**
  21.  * ExceptionController renders error or exception pages for a given
  22.  * FlattenException.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  * @author Matthias Pigulla <mp@webfactory.de>
  26.  *
  27.  * @deprecated since Symfony 4.4, use Symfony\Component\HttpKernel\Controller\ErrorController instead.
  28.  */
  29. class ExceptionController
  30. {
  31.     protected $twig;
  32.     protected $debug;
  33.     /**
  34.      * @param bool $debug Show error (false) or exception (true) pages by default
  35.      */
  36.     public function __construct(Environment $twigbool $debug)
  37.     {
  38.         $this->twig $twig;
  39.         $this->debug $debug;
  40.     }
  41.     /**
  42.      * Converts an Exception to a Response.
  43.      *
  44.      * A "showException" request parameter can be used to force display of an error page (when set to false) or
  45.      * the exception page (when true). If it is not present, the "debug" value passed into the constructor will
  46.      * be used.
  47.      *
  48.      * @return Response
  49.      *
  50.      * @throws \InvalidArgumentException When the exception template does not exist
  51.      */
  52.     public function showAction(Request $requestFlattenException $exceptionDebugLoggerInterface $logger null)
  53.     {
  54.         $currentContent $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
  55.         $showException $request->attributes->get('showException'$this->debug); // As opposed to an additional parameter, this maintains BC
  56.         $code $exception->getStatusCode();
  57.         return new Response($this->twig->render(
  58.             $this->findTemplate($request$request->getRequestFormat(), $code$showException),
  59.             [
  60.                 'status_code' => $code,
  61.                 'status_text' => Response::$statusTexts[$code] ?? '',
  62.                 'exception' => $exception,
  63.                 'logger' => $logger,
  64.                 'currentContent' => $currentContent,
  65.             ]
  66.         ), 200, ['Content-Type' => $request->getMimeType($request->getRequestFormat()) ?: 'text/html']);
  67.     }
  68.     /**
  69.      * @param int $startObLevel
  70.      *
  71.      * @return string
  72.      */
  73.     protected function getAndCleanOutputBuffering($startObLevel)
  74.     {
  75.         if (ob_get_level() <= $startObLevel) {
  76.             return '';
  77.         }
  78.         Response::closeOutputBuffers($startObLevel 1true);
  79.         return ob_get_clean();
  80.     }
  81.     /**
  82.      * @param string $format
  83.      * @param int    $code          An HTTP response status code
  84.      * @param bool   $showException
  85.      *
  86.      * @return string
  87.      */
  88.     protected function findTemplate(Request $request$format$code$showException)
  89.     {
  90.         $name $showException 'exception' 'error';
  91.         if ($showException && 'html' == $format) {
  92.             $name 'exception_full';
  93.         }
  94.         // For error pages, try to find a template for the specific HTTP status code and format
  95.         if (!$showException) {
  96.             $template sprintf('@Twig/Exception/%s%s.%s.twig'$name$code$format);
  97.             if ($this->templateExists($template)) {
  98.                 return $template;
  99.             }
  100.         }
  101.         // try to find a template for the given format
  102.         $template sprintf('@Twig/Exception/%s.%s.twig'$name$format);
  103.         if ($this->templateExists($template)) {
  104.             return $template;
  105.         }
  106.         // default to a generic HTML exception
  107.         $request->setRequestFormat('html');
  108.         return sprintf('@Twig/Exception/%s.html.twig'$showException 'exception_full' $name);
  109.     }
  110.     // to be removed when the minimum required version of Twig is >= 2.0
  111.     protected function templateExists($template)
  112.     {
  113.         $template = (string) $template;
  114.         $loader $this->twig->getLoader();
  115.         if (=== Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
  116.             try {
  117.                 if ($loader instanceof SourceContextLoaderInterface) {
  118.                     $loader->getSourceContext($template);
  119.                 } else {
  120.                     $loader->getSource($template);
  121.                 }
  122.                 return true;
  123.             } catch (LoaderError $e) {
  124.             }
  125.             return false;
  126.         }
  127.         return $loader->exists($template);
  128.     }
  129. }