PreviewErrorController.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Bundle\TwigBundle\Controller;
  11. use Symfony\Component\Debug\Exception\FlattenException;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. /**
  15. * PreviewErrorController can be used to test error pages.
  16. *
  17. * It will create a test exception and forward it to another controller.
  18. *
  19. * @author Matthias Pigulla <mp@webfactory.de>
  20. */
  21. class PreviewErrorController
  22. {
  23. protected $kernel;
  24. protected $controller;
  25. public function __construct(HttpKernelInterface $kernel, $controller)
  26. {
  27. $this->kernel = $kernel;
  28. $this->controller = $controller;
  29. }
  30. public function previewErrorPageAction(Request $request, $code)
  31. {
  32. $exception = FlattenException::create(new \Exception('Something has intentionally gone wrong.'), $code);
  33. /*
  34. * This Request mimics the parameters set by
  35. * \Symfony\Component\HttpKernel\EventListener\ExceptionListener::duplicateRequest, with
  36. * the additional "showException" flag.
  37. */
  38. $subRequest = $request->duplicate(null, null, array(
  39. '_controller' => $this->controller,
  40. 'exception' => $exception,
  41. 'logger' => null,
  42. 'format' => $request->getRequestFormat(),
  43. 'showException' => false,
  44. ));
  45. return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  46. }
  47. }