/
Route.php
executable file
·291 lines (243 loc) · 7.78 KB
/
Route.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
<?php
/*
* This file is part of the phpalchemy package.
*
* (c) Erik Amaru Ortiz <aortiz.erik@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Component\Routing;
use Alchemy\Component\Http\Request;
/**
* Class Route
*
* This class match http url strings for a pattern given
*
* @version 1.0
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
* @link https://github.com/eriknyk/phpalchemy
* @copyright Copyright 2012 Erik Amaru Ortiz
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Alchemy/Component/Routing
*/
class Route
{
/**
* @var string
*/
protected $pattern = '';
/**
* @var string
*/
protected $realPattern = '';
/**
* @var array
*/
protected $vars = array();
/**
* @var array
*/
protected $defaults = array();
/**
* @var array
*/
protected $requirements = array();
/**
* @var array|bool
*/
protected $mapping = false;
/**
* @var string
*/
protected $urlString = '';
/**
* @var string
*/
protected $type = '';
/**
* @var string
*/
protected $resourcePath = '';
/**
* @var array
*/
public $parameters = array();
/**
* Request object to try match with the request information
* @var \Alchemy\Component\Http\Request
*/
protected $request;
public function __construct(
$pattern = '',
array $defaults = array(),
array $requirements = array(),
$type = '',
$resourcePath = ''
) {
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
$this->setPattern($pattern);
$this->setDefaults($defaults);
$this->setRequirements($requirements);
$this->type = $type;
$this->resourcePath = $resourcePath;
}
public function setPattern($pattern)
{
$this->pattern = $pattern;
}
public function getPattern()
{
return $this->pattern;
}
public function setDefaults($defaults)
{
$this->defaults = $defaults;
}
public function getDefaults()
{
return $this->defaults;
}
public function setRequirements($requirements)
{
if (array_key_exists('_method', $requirements)) {
if (! is_array($requirements['_method']) && ! is_string($requirements['_method'])) {
throw new \InvalidArgumentException(
"Invalid Argument Error: Param '_method' only accepts string or array definition."
);
}
$methods = is_array($requirements['_method']) ? $requirements['_method'] : array($requirements['_method']);
// convert all methods names to lowercase
foreach ($methods as $i => $method) {
$methods[$i] = strtolower($method);
}
$requirements['_method'] = $methods;
}
$this->requirements = $requirements;
}
public function getRequirements()
{
return $this->requirements;
}
public function setMapping($mapping)
{
$this->mapping = $mapping;
}
public function getMapping()
{
return $this->requirements;
}
public function getType()
{
return $this->type;
}
public function getVars()
{
return $this->vars[0];
}
public function prepare()
{
$this->pattern = addcslashes($this->pattern, '.\/');
preg_match_all('/\{([\w]+)\}/', $this->pattern, $this->vars);
$patterns = $replacements = array();
foreach ($this->vars[1] as $var) {
$patterns[] = "/\{$var\}/";
if (isset($this->requirements[$var])) {
array_push($replacements, "({$this->requirements[$var]})");
} else {
array_push($replacements, '([\w\-]+)');
}
}
$this->realPattern = preg_replace($patterns, $replacements, $this->pattern);
}
/**
* @param \Alchemy\Component\Http\Request|string $mixed
* @return array|bool
* @throws \Exception
*/
public function match($mixed)
{
$this->prepare();
$urlString = $mixed;
$requestMethod = '';
if ($urlString instanceof Request) {
$urlString = $mixed->getPathInfo();
$requestMethod = strtolower($mixed->getMethod());
// HEAD and GET are equivalent as per RFC
if ('head' === ($method = strtolower($mixed->getMethod()))) {
$requestMethod = 'get';
}
}
$this->urlString = urldecode($urlString);
if (!preg_match('/^'.$this->realPattern.'$/', $this->urlString, $compiledMatches)) {
return false;
}
// to verify _method requirement was defined $requestMethod ahould defined from Request object
if (array_key_exists('_method', $this->requirements) && ! empty($requestMethod)) {
// filter method that by requirement
if (! in_array($requestMethod, $this->requirements['_method'])) {
return false;
}
}
if (!(isset($this->vars[1]) && count($compiledMatches) >= count($this->vars[1]))) {
throw new \Exception("Error while matching parameters, url string given: '$urlString'");
}
$varValues = array_slice($compiledMatches, 1);
foreach ($this->vars[1] as $i => $varName) {
$this->parameters[$varName] = $varValues[$i];
unset($varValues[$i]);
}
foreach ($varValues as $varValue) {
if (substr($varValue, 0, 1) != '?') {
$this->parameters[] = $varValue;
}
}
$this->parameters = array_merge($this->defaults, $this->parameters);
if (! empty($this->mapping)) {
$this->parameters = array(
"params" => $this->parameters,
"mapped" => $this->map($this->parameters)
);
}
return $this->parameters;
}
public function map($parameters)
{
foreach ($this->mapping as $mapKey => $mapValue) {
if (is_array($mapValue)) {
if (isset($mapValue["transform"]) && isset($parameters[$mapKey])) {
$transform = $mapValue["transform"];
$transformList = strpos($transform, ",") !== false ? explode(",", $transform) : array($transform);
foreach($transformList as $trans) {
switch (trim($trans)) {
case "ucfirst": $parameters[$mapKey] = ucfirst($parameters[$mapKey]); break;
case "lcfirst": $parameters[$mapKey] = lcfirst($parameters[$mapKey]); break;
case "camelcase": $parameters[$mapKey] = self::camelcase($parameters[$mapKey]); break;
}
}
}
}
}
foreach ($this->mapping as $mapKey => $mapValue) {
if (! is_string($mapValue) && ! is_array($mapValue)) {
continue;
}
if (is_array($mapValue)) {
$mapValue = isset($mapValue["to"]) ? $mapValue["to"] : "";
}
if (! empty($mapValue)) {
foreach ($parameters as $key => $value) {
if (strpos($mapValue, "{".$key."}") !== false) {
$mapValue = str_replace("{".$key."}", $value, $mapValue);
}
}
$parameters[$mapKey] = $mapValue;
}
}
return $parameters;
}
public static function camelcase($str)
{
return str_replace(" ", "", ucwords(str_replace("_", " ", $str)));
}
}