1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fwk |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2011-2014, Julien Ballestracci <[email protected]>. |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
23
|
|
|
* |
24
|
|
|
* PHP Version 5.3 |
25
|
|
|
* |
26
|
|
|
* @category Core |
27
|
|
|
* @package Fwk\Core |
28
|
|
|
* @subpackage Components |
29
|
|
|
* @author Julien Ballestracci <[email protected]> |
30
|
|
|
* @copyright 2011-2014 Julien Ballestracci <[email protected]> |
31
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
32
|
|
|
* @link http://www.fwk.pw |
33
|
|
|
*/ |
34
|
|
|
namespace Fwk\Core\Components\UrlRewriter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Route |
38
|
|
|
* |
39
|
|
|
* @category Utilities |
40
|
|
|
* @package Fwk\Core |
41
|
|
|
* @subpackage Components |
42
|
|
|
* @author Julien Ballestracci <[email protected]> |
43
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
44
|
|
|
* @link http://www.fwk.pw |
45
|
|
|
*/ |
46
|
|
|
class Route |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $actionName; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $parameters = array(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $uri; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $regex; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Cached reverses of this route |
74
|
|
|
* @var array<string> |
75
|
|
|
*/ |
76
|
|
|
protected $reverseCache = array(); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Constructor |
80
|
|
|
* |
81
|
|
|
* @param string $uri |
82
|
|
|
* |
83
|
|
|
* @return void |
|
|
|
|
84
|
|
|
*/ |
85
|
13 |
|
public function __construct($actionName, $uri, array $parameters = array()) |
86
|
|
|
{ |
87
|
13 |
|
$this->uri = $uri; |
88
|
13 |
|
$this->actionName = $actionName; |
89
|
13 |
|
foreach ($parameters as $param) { |
90
|
12 |
|
$this->addParameter($param); |
91
|
13 |
|
} |
92
|
13 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* @param RouteParameter $param |
97
|
|
|
* |
98
|
|
|
* @return Route |
99
|
|
|
*/ |
100
|
13 |
|
public function addParameter(RouteParameter $param) |
101
|
|
|
{ |
102
|
13 |
|
$this->parameters[$param->getName()] = $param; |
103
|
|
|
|
104
|
13 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
6 |
|
public function getActionName() |
112
|
|
|
{ |
113
|
6 |
|
return $this->actionName; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
1 |
|
public function getUri() |
121
|
|
|
{ |
122
|
1 |
|
return $this->uri; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* @param string $uri |
|
|
|
|
128
|
|
|
* |
129
|
|
|
* @return boolean |
130
|
|
|
*/ |
131
|
4 |
|
public function match($url) { |
|
|
|
|
132
|
4 |
|
$regex = $this->toRegularExpr(); |
133
|
|
|
|
134
|
4 |
|
return (preg_match($regex, $url) > 0); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
4 |
|
public function toRegularExpr() |
142
|
|
|
{ |
143
|
4 |
|
if (!isset($this->regex)) { |
144
|
4 |
|
$regex = sprintf("#^%s/?$#", rtrim($this->uri,'$')); |
145
|
|
|
|
146
|
4 |
|
if(preg_match_all('#:([a-zA-Z0-9_]+)#', $this->uri, $matches)) { |
147
|
4 |
|
foreach($matches[1] as $paramName) { |
148
|
|
|
try { |
149
|
4 |
|
$param = $this->getParameter($paramName); |
150
|
4 |
|
$required = $param->isRequired(); |
151
|
4 |
|
$reg = '(?P<'. $paramName .'>'. $param->getRegex() .')'; |
152
|
|
|
|
153
|
4 |
|
if (!$required) { |
154
|
|
|
$reg .= '?'; |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
$regex = str_replace(':'. $paramName, $reg, $regex); |
158
|
4 |
|
} catch(Exception $e) { |
|
|
|
|
159
|
|
|
} |
160
|
4 |
|
} |
161
|
4 |
|
} |
162
|
|
|
|
163
|
4 |
|
$this->regex = $regex; |
164
|
4 |
|
} |
165
|
|
|
|
166
|
4 |
|
return $this->regex; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
* @param string $name |
172
|
|
|
* |
173
|
|
|
* @return RouteParameter |
174
|
|
|
*/ |
175
|
5 |
|
public function getParameter($name) |
176
|
|
|
{ |
177
|
5 |
|
if(!isset($this->parameters[$name])) { |
178
|
1 |
|
throw new Exception( |
179
|
1 |
|
sprintf('Undefined route parameter "%s"', $name) |
180
|
1 |
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
5 |
|
return $this->parameters[$name]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
7 |
|
public function getParameters() |
191
|
|
|
{ |
192
|
7 |
|
return $this->parameters; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* |
198
|
|
|
* @param array $params |
199
|
|
|
* |
200
|
|
|
* @return string |
|
|
|
|
201
|
|
|
*/ |
202
|
3 |
|
public function getReverse(array $params = array(), $escapeAmp = false) |
203
|
|
|
{ |
204
|
3 |
|
$cacheKey = $this->getCacheKey($params, $escapeAmp); |
205
|
3 |
|
if (isset($this->reverseCache[$cacheKey])) { |
206
|
|
|
return $this->reverseCache[$cacheKey]; |
207
|
|
|
} |
208
|
|
|
|
209
|
3 |
|
$finalParams = array(); |
210
|
3 |
|
$regs = array(); |
211
|
|
|
|
212
|
3 |
|
foreach($this->getParameters() as $param) { |
213
|
3 |
|
$paramName = $param->getName(); |
214
|
3 |
|
$required = $param->isRequired(); |
215
|
3 |
|
$regex = $param->getRegex(); |
216
|
3 |
|
$value = $param->getValue(); |
217
|
|
|
|
218
|
3 |
|
if (isset($params[$paramName])) { |
219
|
3 |
|
$fValue = $params[$paramName]; |
220
|
3 |
|
unset($params[$paramName]); |
221
|
3 |
|
} else { |
222
|
2 |
|
$fValue = $param->getValueOrDefault(); |
223
|
|
|
} |
224
|
|
|
|
225
|
3 |
|
if (!empty($value) && $fValue !== $value) { |
226
|
1 |
|
$this->reverseCache[$cacheKey] = false; |
227
|
1 |
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
3 |
|
if(empty($fValue) && $required) { |
231
|
2 |
|
$this->reverseCache[$cacheKey] = false; |
232
|
2 |
|
return false; |
233
|
|
|
} |
234
|
|
|
|
235
|
3 |
|
if(!\preg_match(sprintf('#(%s)#', $regex), (string)$fValue)) { |
236
|
1 |
|
if ($required) { |
237
|
|
|
$this->reverseCache[$cacheKey] = false; |
238
|
|
|
return false; |
239
|
|
|
} else { |
240
|
1 |
|
$fValue = null; |
241
|
|
|
} |
242
|
1 |
|
} |
243
|
|
|
|
244
|
3 |
|
$finalParams[] = $fValue; |
245
|
3 |
|
$finds[] = ':'. $paramName; |
|
|
|
|
246
|
3 |
|
$regs[] = sprintf('#(:%s\??)#', $paramName); |
247
|
3 |
|
} |
248
|
|
|
|
249
|
3 |
|
$cleanUpUri = \ltrim(\rtrim($this->uri,'$'), '^'); |
250
|
3 |
|
$final = preg_replace($regs, $finalParams, $cleanUpUri); |
251
|
3 |
|
if (count($params)) { |
252
|
1 |
|
$final .= '?'. http_build_query($params, '', ($escapeAmp === false ? '' : '&')); |
253
|
1 |
|
} |
254
|
|
|
|
255
|
3 |
|
$this->reverseCache[$cacheKey] = $final; |
256
|
|
|
|
257
|
3 |
|
return $final; |
258
|
|
|
} |
259
|
|
|
|
260
|
3 |
|
public function getCacheKey(array $arguments, $escapeAmp) |
261
|
|
|
{ |
262
|
3 |
|
$final = ""; |
263
|
3 |
|
foreach ($arguments as $arg) { |
264
|
3 |
|
if (is_scalar($arg)) { |
265
|
3 |
|
$final .= (string)$arg; |
266
|
3 |
|
} else { |
267
|
1 |
|
$final .= serialize($arg); |
268
|
|
|
} |
269
|
3 |
|
} |
270
|
|
|
|
271
|
3 |
|
$final .= (string)$escapeAmp; |
272
|
|
|
|
273
|
3 |
|
return crc32($final); |
274
|
|
|
} |
275
|
|
|
|
276
|
3 |
|
public function __clone() |
277
|
|
|
{ |
278
|
3 |
|
foreach ($this->parameters as &$param) { |
279
|
2 |
|
$param = clone $param; |
280
|
3 |
|
}; |
281
|
|
|
} |
282
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.