RouterController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\ExpressionLanguage\ExpressionFunctionProviderInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\HttpKernel\Profiler\Profiler;
  17. use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
  18. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  19. use Symfony\Component\Routing\RouteCollection;
  20. use Symfony\Component\Routing\RouterInterface;
  21. use Twig\Environment;
  22. /**
  23. * RouterController.
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. *
  27. * @internal
  28. */
  29. class RouterController
  30. {
  31. private $profiler;
  32. private $twig;
  33. private $matcher;
  34. private $routes;
  35. /**
  36. * @var ExpressionFunctionProviderInterface[]
  37. */
  38. private $expressionLanguageProviders = [];
  39. public function __construct(Profiler $profiler = null, Environment $twig, UrlMatcherInterface $matcher = null, RouteCollection $routes = null, iterable $expressionLanguageProviders = [])
  40. {
  41. $this->profiler = $profiler;
  42. $this->twig = $twig;
  43. $this->matcher = $matcher;
  44. $this->routes = (null === $routes && $matcher instanceof RouterInterface) ? $matcher->getRouteCollection() : $routes;
  45. $this->expressionLanguageProviders = $expressionLanguageProviders;
  46. }
  47. /**
  48. * Renders the profiler panel for the given token.
  49. *
  50. * @return Response A Response instance
  51. *
  52. * @throws NotFoundHttpException
  53. */
  54. public function panelAction(string $token)
  55. {
  56. if (null === $this->profiler) {
  57. throw new NotFoundHttpException('The profiler must be enabled.');
  58. }
  59. $this->profiler->disable();
  60. if (null === $this->matcher || null === $this->routes) {
  61. return new Response('The Router is not enabled.', 200, ['Content-Type' => 'text/html']);
  62. }
  63. $profile = $this->profiler->loadProfile($token);
  64. /** @var RequestDataCollector $request */
  65. $request = $profile->getCollector('request');
  66. return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', [
  67. 'request' => $request,
  68. 'router' => $profile->getCollector('router'),
  69. 'traces' => $this->getTraces($request, $profile->getMethod()),
  70. ]), 200, ['Content-Type' => 'text/html']);
  71. }
  72. /**
  73. * Returns the routing traces associated to the given request.
  74. */
  75. private function getTraces(RequestDataCollector $request, string $method): array
  76. {
  77. $traceRequest = Request::create(
  78. $request->getPathInfo(),
  79. $request->getRequestServer(true)->get('REQUEST_METHOD'),
  80. \in_array($request->getMethod(), ['DELETE', 'PATCH', 'POST', 'PUT'], true) ? $request->getRequestRequest()->all() : $request->getRequestQuery()->all(),
  81. $request->getRequestCookies(true)->all(),
  82. [],
  83. $request->getRequestServer(true)->all()
  84. );
  85. $context = $this->matcher->getContext();
  86. $context->setMethod($method);
  87. $matcher = new TraceableUrlMatcher($this->routes, $context);
  88. foreach ($this->expressionLanguageProviders as $provider) {
  89. $matcher->addExpressionLanguageProvider($provider);
  90. }
  91. return $matcher->getTracesForRequest($traceRequest);
  92. }
  93. }