ExceptionPanelController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpKernel\Profiler\Profiler;
  15. /**
  16. * Renders the exception panel.
  17. *
  18. * @author Yonel Ceruto <yonelceruto@gmail.com>
  19. *
  20. * @internal
  21. */
  22. class ExceptionPanelController
  23. {
  24. private $errorRenderer;
  25. private $profiler;
  26. public function __construct(HtmlErrorRenderer $errorRenderer, Profiler $profiler = null)
  27. {
  28. $this->errorRenderer = $errorRenderer;
  29. $this->profiler = $profiler;
  30. }
  31. /**
  32. * Renders the exception panel stacktrace for the given token.
  33. */
  34. public function body(string $token): Response
  35. {
  36. if (null === $this->profiler) {
  37. throw new NotFoundHttpException('The profiler must be enabled.');
  38. }
  39. $exception = $this->profiler->loadProfile($token)
  40. ->getCollector('exception')
  41. ->getException()
  42. ;
  43. return new Response($this->errorRenderer->getBody($exception), 200, ['Content-Type' => 'text/html']);
  44. }
  45. /**
  46. * Renders the exception panel stylesheet.
  47. */
  48. public function stylesheet(): Response
  49. {
  50. return new Response($this->errorRenderer->getStylesheet(), 200, ['Content-Type' => 'text/css']);
  51. }
  52. }