-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Router.php
316 lines (279 loc) · 7.72 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* Part of the Joomla Framework Router Package
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Router;
use Joomla\Controller\ControllerInterface;
use Joomla\Input\Input;
/**
* A path router.
*
* @since 1.0
*/
class Router
{
/**
* Controller class name prefix for creating controller objects by name.
*
* @var string
* @since 1.0
* @deprecated 2.0 Deprecated without replacement
*/
protected $controllerPrefix;
/**
* The default page controller name for an empty route.
*
* @var string
* @since 1.0
* @deprecated 2.0 Deprecated without replacement
*/
protected $default;
/**
* An input object from which to derive the route.
*
* @var Input
* @since 1.0
* @deprecated 2.0 Deprecated without replacement
*/
protected $input;
/**
* An array of rules, each rule being an associative array('regex'=> $regex, 'vars' => $vars, 'controller' => $controller)
* for routing the request.
*
* @var array
* @since 1.0
* @deprecated 2.0 Deprecated without replacement
*/
protected $maps = array();
/**
* Constructor.
*
* @param Input $input An optional input object from which to derive the route.
*
* @since 1.0
*/
public function __construct(Input $input = null)
{
$this->input = $input ?: new Input;
}
/**
* Add a route map to the router. If the pattern already exists it will be overwritten.
*
* @param string $pattern The route pattern to use for matching.
* @param string $controller The controller name to map to the given pattern.
*
* @return Router Returns itself to support chaining.
*
* @since 1.0
* @deprecated 2.0 Deprecated without replacement
*/
public function addMap($pattern, $controller)
{
// Sanitize and explode the pattern.
$pattern = explode('/', trim(parse_url((string) $pattern, PHP_URL_PATH), ' /'));
// Prepare the route variables
$vars = array();
// Initialize regular expression
$regex = array();
// Loop on each segment
foreach ($pattern as $segment)
{
if ($segment == '*')
{
// Match a splat with no variable.
$regex[] = '.*';
}
elseif ($segment[0] == '*')
{
// Match a splat and capture the data to a named variable.
$vars[] = substr($segment, 1);
$regex[] = '(.*)';
}
elseif ($segment[0] == '\\' && $segment[1] == '*')
{
// Match an escaped splat segment.
$regex[] = '\*' . preg_quote(substr($segment, 2));
}
elseif ($segment == ':')
{
// Match an unnamed variable without capture.
$regex[] = '[^/]*';
}
elseif ($segment[0] == ':')
{
// Match a named variable and capture the data.
$vars[] = substr($segment, 1);
$regex[] = '([^/]*)';
}
elseif ($segment[0] == '\\' && $segment[1] == ':')
{
// Match a segment with an escaped variable character prefix.
$regex[] = preg_quote(substr($segment, 1));
}
else
{
// Match the standard segment.
$regex[] = preg_quote($segment);
}
}
$this->maps[] = array(
'regex' => \chr(1) . '^' . implode('/', $regex) . '$' . \chr(1),
'vars' => $vars,
'controller' => (string) $controller,
);
return $this;
}
/**
* Add an array of route maps to the router. If the pattern already exists it will be overwritten.
*
* @param array $maps A list of route maps to add to the router as $pattern => $controller.
*
* @return Router Returns itself to support chaining.
*
* @since 1.0
* @deprecated 2.0 Deprecated without replacement
*/
public function addMaps($maps)
{
foreach ($maps as $pattern => $controller)
{
$this->addMap($pattern, $controller);
}
return $this;
}
/**
* Find and execute the appropriate controller based on a given route.
*
* @param string $route The route string for which to find and execute a controller.
*
* @return ControllerInterface
*
* @since 1.0
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @deprecated 2.0 Deprecated without replacement
*/
public function getController($route)
{
// Get the controller name based on the route patterns and requested route.
$name = $this->parseRoute($route);
// Get the controller object by name.
return $this->fetchController($name);
}
/**
* Set the controller name prefix.
*
* @param string $prefix Controller class name prefix for creating controller objects by name.
*
* @return Router Returns itself to support chaining.
*
* @since 1.0
* @deprecated 2.0 Deprecated without replacement
*/
public function setControllerPrefix($prefix)
{
$this->controllerPrefix = (string) $prefix;
return $this;
}
/**
* Set the default controller name.
*
* @param string $name The default page controller name for an empty route.
*
* @return Router Returns itself to support chaining.
*
* @since 1.0
* @deprecated 2.0 Deprecated without replacement
*/
public function setDefaultController($name)
{
$this->default = (string) $name;
return $this;
}
/**
* Get a Controller object for a given name.
*
* @param string $name The controller name (excluding prefix) for which to fetch and instance.
*
* @return ControllerInterface
*
* @since 1.0
* @throws \RuntimeException
* @deprecated 2.0 Deprecated without replacement
*/
protected function fetchController($name)
{
// Derive the controller class name.
$class = $this->controllerPrefix . ucfirst($name);
// If the controller class does not exist panic.
if (!class_exists($class))
{
throw new \RuntimeException(sprintf('Unable to locate controller `%s`.', $class), 404);
}
// If the controller does not follows the implementation.
if (!is_subclass_of($class, 'Joomla\\Controller\\ControllerInterface'))
{
throw new \RuntimeException(
sprintf(
'Invalid Controller. Controllers must implement Joomla\Controller\ControllerInterface. `%s`.',
$class
),
500
);
}
// Instantiate the controller.
$controller = new $class($this->input);
return $controller;
}
/**
* Parse the given route and return the name of a controller mapped to the given route.
*
* @param string $route The route string for which to find and execute a controller.
*
* @return string The controller name for the given route excluding prefix.
*
* @since 1.0
* @throws \InvalidArgumentException
*/
protected function parseRoute($route)
{
$controller = false;
// Trim the query string off.
$route = preg_replace('/([^?]*).*/u', '\1', $route);
// Sanitize and explode the route.
$route = trim(parse_url($route, PHP_URL_PATH), ' /');
// If the route is empty then simply return the default route. No parsing necessary.
if ($route == '')
{
return $this->default;
}
// Iterate through all of the known route maps looking for a match.
foreach ($this->maps as $rule)
{
if (preg_match($rule['regex'], $route, $matches))
{
// If we have gotten this far then we have a positive match.
$controller = $rule['controller'];
// Time to set the input variables.
// We are only going to set them if they don't already exist to avoid overwriting things.
foreach ($rule['vars'] as $i => $var)
{
$this->input->def($var, $matches[$i + 1]);
// Don't forget to do an explicit set on the GET superglobal.
$this->input->get->def($var, $matches[$i + 1]);
}
$this->input->def('_rawRoute', $route);
break;
}
}
// We were unable to find a route match for the request. Panic.
if (!$controller)
{
throw new \InvalidArgumentException(sprintf('Unable to handle request for route `%s`.', $route), 404);
}
return $controller;
}
}