No Description

ErrorController.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\Component\HttpKernel\Controller;
  11. use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\HttpException;
  15. use Symfony\Component\HttpKernel\HttpKernelInterface;
  16. /**
  17. * Renders error or exception pages from a given FlattenException.
  18. *
  19. * @author Yonel Ceruto <yonelceruto@gmail.com>
  20. * @author Matthias Pigulla <mp@webfactory.de>
  21. */
  22. class ErrorController
  23. {
  24. private $kernel;
  25. private $controller;
  26. private $errorRenderer;
  27. public function __construct(HttpKernelInterface $kernel, $controller, ErrorRendererInterface $errorRenderer)
  28. {
  29. $this->kernel = $kernel;
  30. $this->controller = $controller;
  31. $this->errorRenderer = $errorRenderer;
  32. }
  33. public function __invoke(\Throwable $exception): Response
  34. {
  35. $exception = $this->errorRenderer->render($exception);
  36. return new Response($exception->getAsString(), $exception->getStatusCode(), $exception->getHeaders());
  37. }
  38. public function preview(Request $request, int $code): Response
  39. {
  40. /*
  41. * This Request mimics the parameters set by
  42. * \Symfony\Component\HttpKernel\EventListener\ErrorListener::duplicateRequest, with
  43. * the additional "showException" flag.
  44. */
  45. $subRequest = $request->duplicate(null, null, [
  46. '_controller' => $this->controller,
  47. 'exception' => new HttpException($code, 'This is a sample exception.'),
  48. 'logger' => null,
  49. 'showException' => false,
  50. ]);
  51. return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  52. }
  53. }