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

  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.     public function render(\Throwable $exception): FlattenException
  37.     {
  38.         $flattenException FlattenException::createFromThrowable($exception);
  39.         $debug \is_bool($this->debug) ? $this->debug : ($this->debug)($flattenException);
  40.         if ($debug || !$template $this->findTemplate($flattenException->getStatusCode())) {
  41.             return $this->fallbackErrorRenderer->render($exception);
  42.         }
  43.         return $flattenException->setAsString($this->twig->render($template, [
  44.             'exception' => $flattenException,
  45.             'status_code' => $flattenException->getStatusCode(),
  46.             'status_text' => $flattenException->getStatusText(),
  47.         ]));
  48.     }
  49.     public static function isDebug(RequestStack $requestStackbool $debug): \Closure
  50.     {
  51.         return static function () use ($requestStack$debug): bool {
  52.             if (!$request $requestStack->getCurrentRequest()) {
  53.                 return $debug;
  54.             }
  55.             return $debug && $request->attributes->getBoolean('showException'true);
  56.         };
  57.     }
  58.     private function findTemplate(int $statusCode): ?string
  59.     {
  60.         $template sprintf('@Twig/Exception/error%s.html.twig'$statusCode);
  61.         if ($this->twig->getLoader()->exists($template)) {
  62.             return $template;
  63.         }
  64.         $template '@Twig/Exception/error.html.twig';
  65.         if ($this->twig->getLoader()->exists($template)) {
  66.             return $template;
  67.         }
  68.         return null;
  69.     }
  70. }