CurrentRouteMatch.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. /**
  6. * Default object for current_route_match service.
  7. */
  8. class CurrentRouteMatch implements ResettableStackedRouteMatchInterface {
  9. /**
  10. * The related request stack.
  11. *
  12. * @var \Symfony\Component\HttpFoundation\RequestStack
  13. */
  14. protected $requestStack;
  15. /**
  16. * Internal cache of RouteMatch objects.
  17. *
  18. * @var \SplObjectStorage
  19. */
  20. protected $routeMatches;
  21. /**
  22. * Constructs a CurrentRouteMatch object.
  23. *
  24. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  25. * The request stack.
  26. */
  27. public function __construct(RequestStack $request_stack) {
  28. $this->requestStack = $request_stack;
  29. $this->routeMatches = new \SplObjectStorage();
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getRouteName() {
  35. return $this->getCurrentRouteMatch()->getRouteName();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getRouteObject() {
  41. return $this->getCurrentRouteMatch()->getRouteObject();
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getParameter($parameter_name) {
  47. return $this->getCurrentRouteMatch()->getParameter($parameter_name);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getParameters() {
  53. return $this->getCurrentRouteMatch()->getParameters();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getRawParameter($parameter_name) {
  59. return $this->getCurrentRouteMatch()->getRawParameter($parameter_name);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getRawParameters() {
  65. return $this->getCurrentRouteMatch()->getRawParameters();
  66. }
  67. /**
  68. * Returns the route match for the current request.
  69. *
  70. * @return \Drupal\Core\Routing\RouteMatchInterface
  71. * The current route match object.
  72. */
  73. public function getCurrentRouteMatch() {
  74. return $this->getRouteMatch($this->requestStack->getCurrentRequest());
  75. }
  76. /**
  77. * Returns the route match for a passed in request.
  78. *
  79. * @param \Symfony\Component\HttpFoundation\Request $request
  80. * A request object.
  81. *
  82. * @return \Drupal\Core\Routing\RouteMatchInterface
  83. * A route match object created from the request.
  84. */
  85. protected function getRouteMatch(Request $request) {
  86. if (isset($this->routeMatches[$request])) {
  87. $route_match = $this->routeMatches[$request];
  88. }
  89. else {
  90. $route_match = RouteMatch::createFromRequest($request);
  91. // Since getRouteMatch() might be invoked both before and after routing
  92. // is completed, only statically cache the route match after there's a
  93. // matched route.
  94. if ($route_match->getRouteObject()) {
  95. $this->routeMatches[$request] = $route_match;
  96. }
  97. }
  98. return $route_match;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function resetRouteMatch() {
  104. $this->routeMatches = new \SplObjectStorage();
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getMasterRouteMatch() {
  110. return $this->getRouteMatch($this->requestStack->getMasterRequest());
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function getParentRouteMatch() {
  116. return $this->getRouteMatch($this->requestStack->getParentRequest());
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function getRouteMatchFromRequest(Request $request) {
  122. return $this->getRouteMatch($request);
  123. }
  124. }