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