vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Controller/ErrorController.php line 23

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\TemplateAttributeResolverInterface;
  12. use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
  13. use Sulu\Component\Webspace\Webspace;
  14. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Controller\ErrorController as SymfonyErrorController;
  18. use Twig\Environment;
  19. class ErrorController
  20. {
  21. /**
  22. * @var SymfonyErrorController
  23. */
  24. private $symfonyErrorController;
  25. /**
  26. * @var bool
  27. */
  28. private $debug;
  29. /**
  30. * @var TemplateAttributeResolverInterface
  31. */
  32. private $templateAttributeResolver;
  33. /**
  34. * @var Environment
  35. */
  36. private $twig;
  37. public function __construct(
  38. SymfonyErrorController $symfonyErrorController,
  39. TemplateAttributeResolverInterface $templateAttributeResolver,
  40. Environment $twig,
  41. bool $debug = false
  42. ) {
  43. $this->symfonyErrorController = $symfonyErrorController;
  44. $this->templateAttributeResolver = $templateAttributeResolver;
  45. $this->twig = $twig;
  46. $this->debug = $debug;
  47. }
  48. public function __invoke(Request $request, \Throwable $exception): Response
  49. {
  50. if ($this->debug && $request->attributes->getBoolean('showException', true)) {
  51. return $this->symfonyErrorController->__invoke($exception);
  52. }
  53. $flattenException = FlattenException::createFromThrowable($exception);
  54. $code = $flattenException->getStatusCode();
  55. $errorTemplate = $this->getErrorTemplate($request, $code);
  56. // render the default twig error template when no webspace template found
  57. if (!$errorTemplate) {
  58. return $this->symfonyErrorController->__invoke($exception);
  59. }
  60. return new Response(
  61. $this->twig->render(
  62. $errorTemplate,
  63. $this->templateAttributeResolver->resolve([
  64. 'exception' => $flattenException,
  65. 'status_code' => $flattenException->getStatusCode(),
  66. 'status_text' => $flattenException->getStatusText(),
  67. ])
  68. ),
  69. $code
  70. );
  71. }
  72. private function getErrorTemplate(Request $request, int $code): ?string
  73. {
  74. $suluAttributes = $request->attributes->get('_sulu');
  75. if (!$suluAttributes instanceof RequestAttributes) {
  76. return null;
  77. }
  78. $webspace = $suluAttributes->getAttribute('webspace');
  79. if (!$webspace instanceof Webspace) {
  80. return null;
  81. }
  82. // get the specified or the default error template
  83. $template = $webspace->getTemplate('error-' . $code, $request->getRequestFormat());
  84. if (null === $template) {
  85. $template = $webspace->getTemplate('error', $request->getRequestFormat());
  86. }
  87. if (false === $this->twig->getLoader()->exists($template)) {
  88. return null;
  89. }
  90. return $template;
  91. }
  92. public function preview(Request $request, int $code): Response
  93. {
  94. return $this->symfonyErrorController->preview($request, $code);
  95. }
  96. }