TwigErrorRenderer.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 $twig;
  25. private $fallbackErrorRenderer;
  26. private $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 $twig, HtmlErrorRenderer $fallbackErrorRenderer = null, $debug = false)
  31. {
  32. if (!\is_bool($debug) && !\is_callable($debug)) {
  33. throw new \TypeError(sprintf('Argument 3 passed to "%s()" must be a boolean or a callable, "%s" given.', __METHOD__, get_debug_type($debug)));
  34. }
  35. $this->twig = $twig;
  36. $this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
  37. $this->debug = $debug;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function render(\Throwable $exception): FlattenException
  43. {
  44. $exception = $this->fallbackErrorRenderer->render($exception);
  45. $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
  46. if ($debug || !$template = $this->findTemplate($exception->getStatusCode())) {
  47. return $exception;
  48. }
  49. return $exception->setAsString($this->twig->render($template, [
  50. 'exception' => $exception,
  51. 'status_code' => $exception->getStatusCode(),
  52. 'status_text' => $exception->getStatusText(),
  53. ]));
  54. }
  55. public static function isDebug(RequestStack $requestStack, bool $debug): \Closure
  56. {
  57. return static function () use ($requestStack, $debug): bool {
  58. if (!$request = $requestStack->getCurrentRequest()) {
  59. return $debug;
  60. }
  61. return $debug && $request->attributes->getBoolean('showException', true);
  62. };
  63. }
  64. private function findTemplate(int $statusCode): ?string
  65. {
  66. $template = sprintf('@Twig/Exception/error%s.html.twig', $statusCode);
  67. if ($this->twig->getLoader()->exists($template)) {
  68. return $template;
  69. }
  70. $template = '@Twig/Exception/error.html.twig';
  71. if ($this->twig->getLoader()->exists($template)) {
  72. return $template;
  73. }
  74. return null;
  75. }
  76. }