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