RouteMatch.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  4. use Symfony\Component\HttpFoundation\ParameterBag;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Route;
  7. /**
  8. * Default object representing the results of routing.
  9. */
  10. class RouteMatch implements RouteMatchInterface {
  11. /**
  12. * The route name.
  13. *
  14. * @var string
  15. */
  16. protected $routeName;
  17. /**
  18. * The route.
  19. *
  20. * @var \Symfony\Component\Routing\Route
  21. */
  22. protected $route;
  23. /**
  24. * A key|value store of parameters.
  25. *
  26. * @var \Symfony\Component\HttpFoundation\ParameterBag
  27. */
  28. protected $parameters;
  29. /**
  30. * A key|value store of raw parameters.
  31. *
  32. * @var \Symfony\Component\HttpFoundation\ParameterBag
  33. */
  34. protected $rawParameters;
  35. /**
  36. * Constructs a RouteMatch object.
  37. *
  38. * @param string $route_name
  39. * The name of the route.
  40. * @param \Symfony\Component\Routing\Route $route
  41. * The route.
  42. * @param array $parameters
  43. * The parameters array.
  44. * @param array $raw_parameters
  45. * The raw $parameters array.
  46. */
  47. public function __construct($route_name, Route $route, array $parameters = [], array $raw_parameters = []) {
  48. $this->routeName = $route_name;
  49. $this->route = $route;
  50. // Pre-filter parameters.
  51. $route_params = $this->getParameterNames();
  52. $parameters = array_intersect_key($parameters, $route_params);
  53. $raw_parameters = array_intersect_key($raw_parameters, $route_params);
  54. $this->parameters = new ParameterBag($parameters);
  55. $this->rawParameters = new ParameterBag($raw_parameters);
  56. }
  57. /**
  58. * Creates a RouteMatch from a request.
  59. *
  60. * @param \Symfony\Component\HttpFoundation\Request $request
  61. * A request object.
  62. *
  63. * @return \Drupal\Core\Routing\RouteMatchInterface
  64. * A new RouteMatch object if there's a matched route for the request.
  65. * A new NullRouteMatch object otherwise (e.g., on a 404 page or when
  66. * invoked prior to routing).
  67. */
  68. public static function createFromRequest(Request $request) {
  69. if ($request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
  70. $raw_variables = [];
  71. if ($raw = $request->attributes->get('_raw_variables')) {
  72. $raw_variables = $raw->all();
  73. }
  74. return new static(
  75. $request->attributes->get(RouteObjectInterface::ROUTE_NAME),
  76. $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT),
  77. $request->attributes->all(),
  78. $raw_variables);
  79. }
  80. else {
  81. return new NullRouteMatch();
  82. }
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getRouteName() {
  88. return $this->routeName;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getRouteObject() {
  94. return $this->route;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getParameter($parameter_name) {
  100. return $this->parameters->get($parameter_name);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getParameters() {
  106. return $this->parameters;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getRawParameter($parameter_name) {
  112. return $this->rawParameters->get($parameter_name);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getRawParameters() {
  118. return $this->rawParameters;
  119. }
  120. /**
  121. * Returns the names of all parameters for the currently matched route.
  122. *
  123. * @return array
  124. * Route parameter names as both the keys and values.
  125. */
  126. protected function getParameterNames() {
  127. $names = [];
  128. if ($route = $this->getRouteObject()) {
  129. // Variables defined in path and host patterns are route parameters.
  130. $variables = $route->compile()->getVariables();
  131. $names = array_combine($variables, $variables);
  132. // Route defaults that do not start with a leading "_" are also
  133. // parameters, even if they are not included in path or host patterns.
  134. foreach ($route->getDefaults() as $name => $value) {
  135. if (!isset($names[$name]) && substr($name, 0, 1) !== '_') {
  136. $names[$name] = $name;
  137. }
  138. }
  139. }
  140. return $names;
  141. }
  142. }