|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\Mvc; |
|
4
|
|
|
|
|
5
|
|
|
use Bone\Mvc\Router\Route; |
|
6
|
|
|
use Bone\Regex; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Router |
|
10
|
|
|
{ |
|
11
|
|
|
private $request; |
|
12
|
|
|
private $uri; |
|
13
|
|
|
private $controller; |
|
14
|
|
|
private $action; |
|
15
|
|
|
private $params; |
|
16
|
|
|
private $routes; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* We be needin' t' look at th' map |
|
21
|
|
|
* @param ServerRequestInterface $request |
|
22
|
|
|
*/ |
|
23
|
23 |
|
public function __construct(ServerRequestInterface $request) |
|
24
|
|
|
{ |
|
25
|
23 |
|
$this->request = $request; |
|
26
|
23 |
|
$this->uri = $request->getURI(); |
|
27
|
23 |
|
$this->controller = 'index'; |
|
28
|
23 |
|
$this->action = 'index'; |
|
29
|
23 |
|
$this->params = []; |
|
30
|
23 |
|
$this->routes = []; |
|
31
|
|
|
|
|
32
|
|
|
// get th' path 'n' query string from url |
|
33
|
23 |
|
$parse = parse_url($this->uri); |
|
34
|
23 |
|
$this->uri = $parse['path']; |
|
35
|
23 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
21 |
|
private function matchCustomRoute() |
|
42
|
|
|
{ |
|
43
|
|
|
/** @var \Bone\Mvc\Router\Route $route */ |
|
44
|
21 |
|
foreach ($this->routes as $route) { |
|
45
|
|
|
// if the regex ain't for the home page an' it matches our route |
|
46
|
21 |
|
$strings = $route->getRegexStrings(); |
|
47
|
21 |
|
if ($strings[0] != '\/' && $matches = $route->checkRoute($this->uri)) { |
|
48
|
|
|
// Garrr me hearties! It be a custom route from th' configgeration! |
|
49
|
2 |
|
$this->controller = $route->getControllerName(); |
|
50
|
2 |
|
$this->action = $route->getActionName(); |
|
51
|
2 |
|
$this->params = $route->getParams(); |
|
52
|
21 |
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
19 |
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
19 |
|
private function regexMatch($regex_string) |
|
60
|
|
|
{ |
|
61
|
19 |
|
$regex = new Regex($regex_string); |
|
62
|
19 |
|
return $regex->getMatches($this->uri); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $matches |
|
68
|
|
|
*/ |
|
69
|
3 |
|
private function setController(array $matches) |
|
70
|
|
|
{ |
|
71
|
3 |
|
$this->controller = $matches['controller']; |
|
72
|
3 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $matches |
|
77
|
|
|
*/ |
|
78
|
2 |
|
private function setAction(array $matches) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->action = $matches['action']; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $matches |
|
86
|
|
|
*/ |
|
87
|
1 |
|
private function setVarValPairs(array $matches) |
|
88
|
|
|
{ |
|
89
|
1 |
|
$ex = explode('/', $matches['varvalpairs']); |
|
90
|
1 |
|
for ($x = 0; $x <= count($ex) - 1; $x += 2) { |
|
91
|
1 |
|
if (isset($ex[$x + 1])) { |
|
92
|
1 |
|
$this->params[$ex[$x]] = $ex[$x + 1]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
19 |
|
private function matchControllerActionParamsRoute() |
|
102
|
|
|
{ |
|
103
|
19 |
|
return $this->regexMatch(Regex\Url::CONTROLLER_ACTION_VARS); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array|null |
|
109
|
|
|
*/ |
|
110
|
18 |
|
private function matchControllerActionRoute() |
|
111
|
|
|
{ |
|
112
|
18 |
|
return $this->regexMatch(Regex\Url::CONTROLLER_ACTION); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
17 |
|
private function matchControllerRoute() |
|
120
|
|
|
{ |
|
121
|
17 |
|
return $this->regexMatch(Regex\Url::CONTROLLER); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* gets custom routes from config |
|
127
|
|
|
*/ |
|
128
|
21 |
|
private function setCustomRoutesFromConfig() |
|
129
|
|
|
{ |
|
130
|
|
|
// we be checkin' our instruction fer configgered routes |
|
131
|
21 |
|
$configgeration = Registry::ahoy()->get('routes'); |
|
132
|
|
|
|
|
133
|
|
|
// stick some voodoo pins in the map |
|
134
|
21 |
|
foreach ($configgeration as $route => $options) { |
|
135
|
|
|
// add the route t' the map |
|
136
|
21 |
|
$this->routes[] = new Route($route, $options); |
|
137
|
|
|
} |
|
138
|
21 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Merges params from config |
|
143
|
|
|
*/ |
|
144
|
22 |
|
private function setParams() |
|
145
|
|
|
{ |
|
146
|
|
|
// be addin' the $_GET an' $_POST t' th' params! |
|
147
|
22 |
|
$method = $this->request->getMethod(); |
|
148
|
22 |
|
$getParams = $this->request->getQueryParams(); |
|
149
|
22 |
|
$postParams = $this->request->getParsedBody(); |
|
150
|
22 |
|
if ($method == "POST" && is_array($postParams)) { |
|
151
|
|
|
$this->params = array_merge($this->params, $postParams); |
|
152
|
|
|
} |
|
153
|
22 |
|
$this->params = array_merge($this->params, $getParams); |
|
154
|
22 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Tells the Navigator to go to the / route |
|
159
|
|
|
*/ |
|
160
|
21 |
|
private function matchRoute() |
|
161
|
|
|
{ |
|
162
|
|
|
// we be startin' off assumin' th' voyage will be a disaster |
|
163
|
21 |
|
$this->controller = 'error'; |
|
164
|
21 |
|
$this->action = 'not-found'; |
|
165
|
|
|
|
|
166
|
|
|
// Get th' navigator! Do we know where we be sailin'? Do we have a plan? |
|
167
|
21 |
|
if (!$this->matchCustomRoute()) { |
|
168
|
19 |
|
$this->matchDefaultRoutes(); |
|
169
|
|
|
} |
|
170
|
21 |
|
} |
|
171
|
|
|
|
|
172
|
19 |
|
public function matchDefaultRoutes() |
|
173
|
|
|
{ |
|
174
|
19 |
|
if ( $matches = $this->matchControllerActionParamsRoute()) { |
|
175
|
|
|
// we have a controller action var val match Cap'n! |
|
176
|
1 |
|
$this->setController($matches); |
|
177
|
1 |
|
$this->setAction($matches); |
|
178
|
1 |
|
$this->setVarValPairs($matches); |
|
179
|
18 |
|
} elseif ($matches = $this->matchControllerActionRoute()) { |
|
180
|
|
|
// we have a controller action match Cap'n! |
|
181
|
1 |
|
$this->setController($matches); |
|
182
|
1 |
|
$this->setAction($matches); |
|
183
|
17 |
|
} elseif ($matches = $this->matchControllerRoute()) { |
|
184
|
|
|
// we have a controller action match Cap'n! |
|
185
|
|
|
// settin' the destination controller and action and params |
|
186
|
1 |
|
$this->setController($matches); |
|
187
|
1 |
|
$this->action = 'index'; |
|
188
|
|
|
} |
|
189
|
19 |
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Tells the Navigator to go to the / route |
|
194
|
|
|
*/ |
|
195
|
22 |
|
private function sailHome() |
|
196
|
|
|
{ |
|
197
|
22 |
|
$routes = Registry::ahoy()->get('routes'); |
|
198
|
22 |
|
$home_page = $routes['/']; |
|
199
|
22 |
|
$this->controller = $home_page['controller']; |
|
200
|
22 |
|
$this->action = $home_page['action']; |
|
201
|
22 |
|
$this->params = $home_page['params']; |
|
202
|
22 |
|
} |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Figger out where we be goin' |
|
207
|
|
|
*/ |
|
208
|
22 |
|
public function parseRoute() |
|
209
|
|
|
{ |
|
210
|
|
|
// start at the home page |
|
211
|
22 |
|
$this->sailHome(); |
|
212
|
|
|
|
|
213
|
|
|
// which way be we goin' ? |
|
214
|
22 |
|
$path = $this->uri; |
|
215
|
|
|
|
|
216
|
|
|
// Has th' route been set? |
|
217
|
22 |
|
if ($path != '/') { |
|
218
|
|
|
// Set the routes configgerd in th' config.php |
|
219
|
21 |
|
$this->setCustomRoutesFromConfig(); |
|
220
|
|
|
|
|
221
|
|
|
// Match the feckin' route ya blubberin' seadog! |
|
222
|
21 |
|
$this->matchRoute(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// Merge th' GET POST and config params |
|
226
|
22 |
|
$this->setParams(); |
|
227
|
22 |
|
} |
|
228
|
|
|
|
|
229
|
23 |
|
public function getController() |
|
230
|
|
|
{ |
|
231
|
23 |
|
return $this->controller; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
20 |
|
public function getAction() |
|
235
|
|
|
{ |
|
236
|
20 |
|
return $this->action; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
18 |
|
public function getParams() |
|
240
|
|
|
{ |
|
241
|
18 |
|
return $this->params; |
|
242
|
|
|
} |
|
243
|
|
|
} |