|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Routlt\ActionMapping |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2014 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link http://github.com/appserver-io/routlt |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Routlt; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Tokenize implementation using a regex to parse a route. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2014 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link http://github.com/appserver-io/routlt |
|
30
|
|
|
* @link http://www.appserver.io |
|
31
|
|
|
*/ |
|
32
|
|
|
class ActionMapping implements ActionMappingInterface |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The template for to create the compiled regex from. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $regexTemplate = '/^%s$/'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The path with the controller/method name. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $path; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The uncompiled expression. |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $expression; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The compiled regex, used to extract the variables from the route. |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $compiledRegex; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The controller name, extracted from the route. |
|
65
|
|
|
* |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $controllerName; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The method name, extracted from the route. |
|
72
|
|
|
* |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $methodName; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The array containing the declared placeholders. |
|
79
|
|
|
* |
|
80
|
|
|
* @var array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $vars = array(); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* The array with the default values for the placeholders. |
|
86
|
|
|
* |
|
87
|
|
|
* @var array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $defaults = array(); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* The request parameters, extracted from the route. |
|
93
|
|
|
* |
|
94
|
|
|
* @var array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $requestParameters = array(); |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Compiles the passed route expression into a valid regex. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $expression The expression to use |
|
102
|
|
|
* @param array $requirements The requirements for the expression |
|
103
|
|
|
* @param array $defaults The default values for the found variables |
|
104
|
|
|
* |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
11 |
|
public function compile($expression, array $requirements = array(), array $defaults = array()) |
|
108
|
|
|
{ |
|
109
|
|
|
|
|
110
|
|
|
// intialize the expression |
|
111
|
11 |
|
$this->expression = $expression; |
|
112
|
11 |
|
$this->defaults = $defaults; |
|
113
|
|
|
|
|
114
|
|
|
// the element number of the FIRST variable |
|
115
|
11 |
|
$pathLength = null; |
|
116
|
|
|
|
|
117
|
|
|
// extract the variables from the expression, also store their position |
|
118
|
11 |
|
$elements = explode('/', ltrim($expression, '/')); |
|
119
|
11 |
|
foreach ($elements as $key => $element) { |
|
120
|
|
|
// query whether or not we found a variable |
|
121
|
11 |
|
if (strpos($element, ':') === 0) { |
|
122
|
|
|
// add the variable and store with the position as key |
|
123
|
8 |
|
$this->vars[$key] = ltrim($element, ':'); |
|
124
|
|
|
// we store the position of the FIRST variable |
|
125
|
8 |
|
if ($pathLength == null) { |
|
126
|
8 |
|
$pathLength = $key; |
|
127
|
8 |
|
} |
|
128
|
8 |
|
} |
|
129
|
11 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
// query whether or not we found variables |
|
132
|
11 |
|
if ($pathLength) { |
|
133
|
|
|
// if yes, cut of the array at that position |
|
134
|
8 |
|
$elements = array_slice($elements, 0, $pathLength); |
|
135
|
8 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
// the path ends where the variables begin or |
|
138
|
11 |
|
$this->path = sprintf('/%s', implode('/', $elements)); |
|
139
|
|
|
|
|
140
|
|
|
// initialize the string that has to be compiled |
|
141
|
11 |
|
$toCompile = $this->expression; |
|
142
|
|
|
|
|
143
|
|
|
// try to compile the regex group expression for each variable |
|
144
|
11 |
|
foreach ($this->vars as $var) { |
|
145
|
|
|
// by default the sequence to use is .* |
|
146
|
8 |
|
$sequence = '.*'; |
|
147
|
|
|
// check if a sequence for the variable has been passed |
|
148
|
8 |
|
if (isset($requirements[$var])) { |
|
149
|
4 |
|
$sequence = $requirements[$var]; |
|
150
|
4 |
|
} |
|
151
|
|
|
// replace the placeholder with the regex group expression |
|
152
|
8 |
|
$toCompile = str_replace(sprintf(':%s', $var), sprintf('(?<%s>%s)', $var, $sequence), $toCompile); |
|
153
|
11 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
// initialize the member with the compiled regex |
|
156
|
11 |
|
$this->compiledRegex = sprintf($this->regexTemplate, addcslashes($toCompile, '/')); |
|
157
|
11 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Tokenizes the passed route by using the tokenizers expression. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $route The route to be parsed |
|
163
|
|
|
* |
|
164
|
|
|
* @return boolean TRUE if the passed route matches the expression, else FALSE |
|
165
|
|
|
*/ |
|
166
|
10 |
|
public function match($route) |
|
167
|
|
|
{ |
|
168
|
|
|
|
|
169
|
|
|
// initialize the array for the variable matches |
|
170
|
10 |
|
$matches = array(); |
|
171
|
|
|
|
|
172
|
|
|
// execute the regex and extract the variables |
|
173
|
10 |
|
$result = preg_match($this->getCompiledRegex(), $this->applyDefaults($route), $matches); |
|
174
|
|
|
|
|
175
|
|
|
// append the variable values to the request parameters |
|
176
|
10 |
|
foreach ($this->vars as $var) { |
|
177
|
8 |
|
if (isset($matches[$var])) { |
|
178
|
8 |
|
$this->requestParameters[$var] = $matches[$var]; |
|
179
|
8 |
|
} |
|
180
|
10 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
// return TRUE if the compiled regex matched |
|
183
|
10 |
|
if ($result === 1) { |
|
184
|
9 |
|
return true; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
// else we return FALSE |
|
188
|
1 |
|
return false; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Return's the path with the controller/method name. |
|
193
|
|
|
* |
|
194
|
|
|
* @return string The path with the controller/method name |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function getPath() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->path; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* If necessary, this method applies the default values to the passed route. |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $route The route to apply the default values to |
|
205
|
|
|
* |
|
206
|
|
|
* @return string The route with the default values applied |
|
207
|
|
|
*/ |
|
208
|
10 |
|
protected function applyDefaults($route) |
|
209
|
|
|
{ |
|
210
|
|
|
|
|
211
|
|
|
// query whether or not the route matches |
|
212
|
10 |
|
if (preg_match(sprintf('/%s/', addcslashes($this->path, '/')), $route)) { |
|
213
|
|
|
// initialize the array for the appended variables |
|
214
|
9 |
|
$vars = array(); |
|
215
|
|
|
|
|
216
|
|
|
// try to extract the appended variables |
|
217
|
9 |
|
$varString = ltrim(str_replace($this->path, '', $route), '/'); |
|
218
|
9 |
|
if (empty($varString) === false) { |
|
219
|
6 |
|
$vars = explode('/', $varString); |
|
220
|
6 |
|
} |
|
221
|
|
|
|
|
222
|
|
|
// iterate over all variables |
|
223
|
9 |
|
foreach ($this->vars as $key => $var) { |
|
224
|
|
|
// query whether or not, we've a default value and the variable has NOT been set |
|
225
|
8 |
|
if (isset($this->defaults[$var]) && !isset($vars[$key])) { |
|
226
|
3 |
|
$vars[$key] = $this->defaults[$var]; |
|
227
|
3 |
|
} |
|
228
|
9 |
|
} |
|
229
|
|
|
|
|
230
|
|
|
// if we've found vars, append them to the path |
|
231
|
9 |
|
if (sizeof($vars) > 0) { |
|
232
|
8 |
|
$route = sprintf('%s/%s', $this->path, implode('/', $vars)); |
|
233
|
8 |
|
} |
|
234
|
9 |
|
} |
|
235
|
|
|
|
|
236
|
|
|
// return the route with the defaults applied |
|
237
|
10 |
|
return $route; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Return's the regex build from the expression. |
|
242
|
|
|
* |
|
243
|
|
|
* @return string The regex |
|
244
|
|
|
*/ |
|
245
|
10 |
|
public function getCompiledRegex() |
|
246
|
|
|
{ |
|
247
|
10 |
|
return $this->compiledRegex; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Initializes the action mapping with the passed controller name. |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $controllerName The controller name found in the route |
|
254
|
|
|
* |
|
255
|
|
|
* @return void |
|
256
|
|
|
*/ |
|
257
|
2 |
|
public function setControllerName($controllerName) |
|
258
|
|
|
{ |
|
259
|
2 |
|
$this->controllerName = $controllerName; |
|
260
|
2 |
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Return's the controller name found in the route. |
|
264
|
|
|
* |
|
265
|
|
|
* @return string The controller name |
|
266
|
|
|
*/ |
|
267
|
1 |
|
public function getControllerName() |
|
268
|
|
|
{ |
|
269
|
1 |
|
return $this->controllerName; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Initializes the action mapping with the passed method name. |
|
274
|
|
|
* |
|
275
|
|
|
* @param string $methodName The method name found in the route |
|
276
|
|
|
* |
|
277
|
|
|
* @return void |
|
278
|
|
|
*/ |
|
279
|
2 |
|
public function setMethodName($methodName) |
|
280
|
|
|
{ |
|
281
|
2 |
|
$this->methodName = $methodName; |
|
282
|
2 |
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Return's the method name found in the route. |
|
286
|
|
|
* |
|
287
|
|
|
* @return string The method name |
|
288
|
|
|
*/ |
|
289
|
1 |
|
public function getMethodName() |
|
290
|
|
|
{ |
|
291
|
1 |
|
return $this->methodName; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Return's the extracted request parameters found in the route. |
|
296
|
|
|
* |
|
297
|
|
|
* @return array The extracted request parameters |
|
298
|
|
|
*/ |
|
299
|
10 |
|
public function getRequestParameters() |
|
300
|
|
|
{ |
|
301
|
10 |
|
return $this->requestParameters; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|