vendor/symfony/twig-bridge/ErrorRenderer/TwigErrorRenderer.php line 47

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\Bridge\Twig\ErrorRenderer;
  11. use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
  12. use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
  13. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Twig\Environment;
  16. /**
  17.  * Provides the ability to render custom Twig-based HTML error pages
  18.  * in non-debug mode, otherwise falls back to HtmlErrorRenderer.
  19.  *
  20.  * @author Yonel Ceruto <yonelceruto@gmail.com>
  21.  */
  22. class TwigErrorRenderer implements ErrorRendererInterface
  23. {
  24.     private Environment $twig;
  25.     private HtmlErrorRenderer $fallbackErrorRenderer;
  26.     private \Closure|bool $debug;
  27.     /**
  28.      * @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
  29.      */
  30.     public function __construct(Environment $twigHtmlErrorRenderer $fallbackErrorRenderer nullbool|callable $debug false)
  31.     {
  32.         $this->twig $twig;
  33.         $this->fallbackErrorRenderer $fallbackErrorRenderer ?? new HtmlErrorRenderer();
  34.         $this->debug \is_bool($debug) ? $debug $debug(...);
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function render(\Throwable $exception): FlattenException
  40.     {
  41.         $exception $this->fallbackErrorRenderer->render($exception);
  42.         $debug \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
  43.         if ($debug || !$template $this->findTemplate($exception->getStatusCode())) {
  44.             return $exception;
  45.         }
  46.         return $exception->setAsString($this->twig->render($template, [
  47.             'exception' => $exception,
  48.             'status_code' => $exception->getStatusCode(),
  49.             'status_text' => $exception->getStatusText(),
  50.         ]));
  51.     }
  52.     public static function isDebug(RequestStack $requestStackbool $debug): \Closure
  53.     {
  54.         return static function () use ($requestStack$debug): bool {
  55.             if (!$request $requestStack->getCurrentRequest()) {
  56.                 return $debug;
  57.             }
  58.             return $debug && $request->attributes->getBoolean('showException'true);
  59.         };
  60.     }
  61.     private function findTemplate(int $statusCode): ?string
  62.     {
  63.         $template sprintf('@Twig/Exception/error%s.html.twig'$statusCode);
  64.         if ($this->twig->getLoader()->exists($template)) {
  65.             return $template;
  66.         }
  67.         $template '@Twig/Exception/error.html.twig';
  68.         if ($this->twig->getLoader()->exists($template)) {
  69.             return $template;
  70.         }
  71.         return null;
  72.     }
  73. }