base.php
4.55 KB
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
<?php
/**
* @package Joomla.Platform
* @subpackage Application
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* Basic Web application router class for the Joomla Platform.
*
* @since 3.0
* @deprecated 4.0 Use the `joomla/router` package via Composer instead
*/
class JApplicationWebRouterBase extends JApplicationWebRouter
{
/**
* @var array An array of rules, each rule being an associative array('regex'=> $regex, 'vars' => $vars, 'controller' => $controller)
* for routing the request.
* @since 3.0
*/
protected $maps = array();
/**
* 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 JApplicationWebRouter This object for method chaining.
*
* @since 3.0
*/
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)
{
// Match a splat with no variable.
if ($segment == '*')
{
$regex[] = '.*';
}
// Match a splat and capture the data to a named variable.
elseif ($segment[0] == '*')
{
$vars[] = substr($segment, 1);
$regex[] = '(.*)';
}
// Match an escaped splat segment.
elseif ($segment[0] == '\\' && $segment[1] == '*')
{
$regex[] = '\*' . preg_quote(substr($segment, 2));
}
// Match an unnamed variable without capture.
elseif ($segment == ':')
{
$regex[] = '[^/]*';
}
// Match a named variable and capture the data.
elseif ($segment[0] == ':')
{
$vars[] = substr($segment, 1);
$regex[] = '([^/]*)';
}
// Match a segment with an escaped variable character prefix.
elseif ($segment[0] == '\\' && $segment[1] == ':')
{
$regex[] = preg_quote(substr($segment, 1));
}
// Match the standard segment.
else
{
$regex[] = preg_quote($segment);
}
}
$this->maps[] = array(
'regex' => chr(1) . '^' . implode('/', $regex) . '$' . chr(1),
'vars' => $vars,
'controller' => (string) $controller,
);
return $this;
}
/**
* Add a route map 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 JApplicationWebRouter This object for method chaining.
*
* @since 3.0
*/
public function addMaps($maps)
{
foreach ($maps as $pattern => $controller)
{
$this->addMap($pattern, $controller);
}
return $this;
}
/**
* 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 3.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;
}
}