|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\Mvc\Router; |
|
4
|
|
|
use Bone\Regex; |
|
5
|
|
|
use Bone\Regex\Url; |
|
6
|
|
|
|
|
7
|
|
|
class Route |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* This be th' route from th' configgerashun |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
private $name; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Th' settin's for th' route. garr! |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This be the exploded path of th' array |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $parts; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* th' optional [/:var] part of th' uri as a regex |
|
29
|
|
|
* @var string; |
|
30
|
|
|
*/ |
|
31
|
|
|
private $optional; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* a bunch of voodoo regex strings |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $strings; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* fer matchin' patterns |
|
41
|
|
|
* @var \Bone\Regex |
|
42
|
|
|
*/ |
|
43
|
|
|
private $regex; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* th' exploded uri |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private $matched_uri_parts; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The key an' value from th' configgerashun |
|
62
|
|
|
* |
|
63
|
|
|
* @param $name |
|
64
|
|
|
* @param array $config |
|
65
|
|
|
*/ |
|
66
|
15 |
|
public function __construct($name, array $config) |
|
67
|
15 |
|
{ |
|
68
|
15 |
|
$this->name = $name; |
|
69
|
15 |
|
$this->config = $config; |
|
70
|
15 |
|
$this->strings = array( |
|
71
|
15 |
|
0 => '', |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
15 |
|
$name = $this->removeOptionalFromName($name); |
|
75
|
|
|
|
|
76
|
15 |
|
$this->setParts($name); |
|
77
|
|
|
|
|
78
|
15 |
|
$this->setStrings(); |
|
79
|
|
|
|
|
80
|
15 |
|
$this->setOptionalStrings(); |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
15 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Check fer an optional var [/:var] in the configgered route |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
15 |
|
private function removeOptionalFromName($name) |
|
92
|
15 |
|
{ |
|
93
|
15 |
|
$this->regex = new Regex(Url::SQUARE_BRACKETS); |
|
94
|
15 |
|
if($matches = $this->regex->getMatches($name)) |
|
95
|
15 |
|
{ |
|
96
|
|
|
/** |
|
97
|
|
|
* th' route has an optional field [/:var] at th' end garr |
|
98
|
|
|
* we'll add it after we've done the rest |
|
99
|
|
|
*/ |
|
100
|
5 |
|
$this->optional = str_replace('/','',$matches[1]); |
|
101
|
5 |
|
$name = str_replace('[/'.$this->optional.']','', $name); |
|
102
|
5 |
|
} |
|
103
|
15 |
|
return $name; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* checks t' see if th' uri matches the regex routes |
|
115
|
|
|
* |
|
116
|
|
|
* @param $uri |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
5 |
|
public function checkRoute($uri) |
|
120
|
5 |
|
{ |
|
121
|
5 |
|
foreach($this->strings as $expression) |
|
122
|
|
|
{ |
|
123
|
|
|
// check if it matches the pattern |
|
124
|
5 |
|
$this->regex->setPattern($expression); |
|
125
|
5 |
|
if($this->regex->getMatches($uri)) |
|
126
|
5 |
|
{ |
|
127
|
|
|
$this->matched_uri_parts = explode('/',$uri); |
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
5 |
|
} |
|
131
|
5 |
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param $name |
|
137
|
|
|
*/ |
|
138
|
15 |
|
private function setParts($name) |
|
139
|
15 |
|
{ |
|
140
|
|
|
/** |
|
141
|
|
|
* blow the feckin' route to smithereens |
|
142
|
|
|
*/ |
|
143
|
15 |
|
$this->parts = explode('/',$name); |
|
144
|
15 |
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* break the url t' smithereens! garrr! |
|
149
|
|
|
*/ |
|
150
|
15 |
|
private function setStrings() |
|
151
|
15 |
|
{ |
|
152
|
|
|
/* |
|
153
|
|
|
* Sift through the wreckage |
|
154
|
|
|
*/ |
|
155
|
15 |
|
foreach($this->parts as $part) |
|
156
|
|
|
{ |
|
157
|
15 |
|
$this->checkPart($part); |
|
158
|
15 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/* |
|
161
|
|
|
* if there's still nuthin', we must be on the feckin' home page |
|
162
|
|
|
*/ |
|
163
|
15 |
|
$this->strings[0] = ($this->strings[0] == '') ? '\/' : $this->strings[0]; |
|
164
|
15 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $part |
|
169
|
|
|
*/ |
|
170
|
15 |
|
private function checkPart($part) |
|
171
|
15 |
|
{ |
|
172
|
|
|
/* |
|
173
|
|
|
* look fer a colon |
|
174
|
|
|
*/ |
|
175
|
15 |
|
if($part && strstr($part,':')) |
|
176
|
15 |
|
{ |
|
177
|
|
|
/* |
|
178
|
|
|
* Make it look fer /something |
|
179
|
|
|
*/ |
|
180
|
5 |
|
$this->strings[0] .= Url::SLASH_WORD; |
|
181
|
5 |
|
} |
|
182
|
15 |
|
elseif($part) |
|
183
|
|
|
{ |
|
184
|
|
|
/* |
|
185
|
|
|
* make it look fer /part |
|
186
|
|
|
*/ |
|
187
|
5 |
|
$this->strings[0] .= '\/'.$part; |
|
188
|
5 |
|
} |
|
189
|
15 |
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* checks fer the optional stuff |
|
195
|
|
|
*/ |
|
196
|
15 |
|
private function setOptionalStrings() |
|
197
|
15 |
|
{ |
|
198
|
|
|
/* |
|
199
|
|
|
* Make another string t' check fer |
|
200
|
|
|
*/ |
|
201
|
15 |
|
if($this->optional) |
|
202
|
15 |
|
{ |
|
203
|
5 |
|
$this->strings[1] = $this->strings[0].Url::SLASH_WORD; |
|
204
|
|
|
//reverse the fecker, if the longer one matches first, good! |
|
205
|
5 |
|
$this->strings = array_reverse($this->strings); |
|
206
|
5 |
|
} |
|
207
|
15 |
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* th' patterns the route wants t' match |
|
214
|
|
|
* |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
15 |
|
public function getRegexStrings() |
|
218
|
15 |
|
{ |
|
219
|
15 |
|
return $this->strings; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
public function getControllerName() |
|
231
|
|
|
{ |
|
232
|
|
|
return $this->config['controller']; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
public function getActionName() |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->config['action']; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
public function getParams() |
|
254
|
|
|
{ |
|
255
|
|
|
$x = 0; |
|
256
|
|
|
foreach($this->parts as $part) |
|
257
|
|
|
{ |
|
258
|
|
|
if(strstr($part,':')) |
|
259
|
|
|
{ |
|
260
|
|
|
$this->config['params'][str_replace(':','',$part)] = $this->matched_uri_parts[$x]; |
|
261
|
|
|
} |
|
262
|
|
|
$x ++; |
|
263
|
|
|
} |
|
264
|
|
|
if($this->optional) |
|
265
|
|
|
{ |
|
266
|
|
|
$this->config['params'][str_replace(':','',$this->optional)] = isset($this->matched_uri_parts[$x]) ? $this->matched_uri_parts[$x] : null; |
|
267
|
|
|
} |
|
268
|
|
|
return $this->config['params']; |
|
269
|
|
|
} |
|
270
|
|
|
} |