RouterController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  13. use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
  14. use Symfony\Component\Routing\RouteCollection;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Symfony\Component\HttpKernel\Profiler\Profiler;
  17. /**
  18. * RouterController.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class RouterController
  23. {
  24. private $profiler;
  25. private $twig;
  26. private $matcher;
  27. private $routes;
  28. public function __construct(Profiler $profiler = null, \Twig_Environment $twig, UrlMatcherInterface $matcher = null, RouteCollection $routes = null)
  29. {
  30. $this->profiler = $profiler;
  31. $this->twig = $twig;
  32. $this->matcher = $matcher;
  33. $this->routes = $routes;
  34. if (null === $this->routes && $this->matcher instanceof RouterInterface) {
  35. $this->routes = $matcher->getRouteCollection();
  36. }
  37. }
  38. /**
  39. * Renders the profiler panel for the given token.
  40. *
  41. * @param string $token The profiler token
  42. *
  43. * @return Response A Response instance
  44. */
  45. public function panelAction($token)
  46. {
  47. if (null === $this->profiler) {
  48. throw new NotFoundHttpException('The profiler must be enabled.');
  49. }
  50. $this->profiler->disable();
  51. if (null === $this->matcher || null === $this->routes) {
  52. return new Response('The Router is not enabled.', 200, array('Content-Type' => 'text/html'));
  53. }
  54. $profile = $this->profiler->loadProfile($token);
  55. $context = $this->matcher->getContext();
  56. $context->setMethod($profile->getMethod());
  57. $matcher = new TraceableUrlMatcher($this->routes, $context);
  58. $request = $profile->getCollector('request');
  59. return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', array(
  60. 'request' => $request,
  61. 'router' => $profile->getCollector('router'),
  62. 'traces' => $matcher->getTraces($request->getPathInfo()),
  63. )), 200, array('Content-Type' => 'text/html'));
  64. }
  65. }