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