ExceptionController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\WebProfilerBundle\Controller;
  11. use Symfony\Component\HttpKernel\Profiler\Profiler;
  12. use Symfony\Component\HttpKernel\Debug\ExceptionHandler;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpFoundation\Response;
  15. /**
  16. * ExceptionController.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class ExceptionController
  21. {
  22. protected $twig;
  23. protected $debug;
  24. protected $profiler;
  25. public function __construct(Profiler $profiler = null, \Twig_Environment $twig, $debug)
  26. {
  27. $this->profiler = $profiler;
  28. $this->twig = $twig;
  29. $this->debug = $debug;
  30. }
  31. /**
  32. * Renders the exception panel for the given token.
  33. *
  34. * @param string $token The profiler token
  35. *
  36. * @return Response A Response instance
  37. */
  38. public function showAction($token)
  39. {
  40. if (null === $this->profiler) {
  41. throw new NotFoundHttpException('The profiler must be enabled.');
  42. }
  43. $this->profiler->disable();
  44. $exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
  45. $template = $this->getTemplate();
  46. if (!$this->twig->getLoader()->exists($template)) {
  47. $handler = new ExceptionHandler();
  48. return new Response($handler->getContent($exception), 200, array('Content-Type' => 'text/html'));
  49. }
  50. $code = $exception->getStatusCode();
  51. return new Response($this->twig->render(
  52. $template,
  53. array(
  54. 'status_code' => $code,
  55. 'status_text' => Response::$statusTexts[$code],
  56. 'exception' => $exception,
  57. 'logger' => null,
  58. 'currentContent' => '',
  59. )
  60. ), 200, array('Content-Type' => 'text/html'));
  61. }
  62. /**
  63. * Renders the exception panel stylesheet for the given token.
  64. *
  65. * @param string $token The profiler token
  66. *
  67. * @return Response A Response instance
  68. */
  69. public function cssAction($token)
  70. {
  71. if (null === $this->profiler) {
  72. throw new NotFoundHttpException('The profiler must be enabled.');
  73. }
  74. $this->profiler->disable();
  75. $exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
  76. $template = $this->getTemplate();
  77. if (!$this->templateExists($template)) {
  78. $handler = new ExceptionHandler();
  79. return new Response($handler->getStylesheet($exception), 200, array('Content-Type' => 'text/css'));
  80. }
  81. return new Response($this->twig->render('@WebProfiler/Collector/exception.css.twig'), 200, array('Content-Type' => 'text/css'));
  82. }
  83. protected function getTemplate()
  84. {
  85. return '@Twig/Exception/'.($this->debug ? 'exception' : 'error').'.html.twig';
  86. }
  87. // to be removed when the minimum required version of Twig is >= 2.0
  88. protected function templateExists($template)
  89. {
  90. $loader = $this->twig->getLoader();
  91. if ($loader instanceof \Twig_ExistsLoaderInterface) {
  92. return $loader->exists($template);
  93. }
  94. try {
  95. $loader->getSource($template);
  96. return true;
  97. } catch (\Twig_Error_Loader $e) {
  98. }
  99. return false;
  100. }
  101. }