1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Router; |
4
|
|
|
|
5
|
|
|
use Zend\Diactoros\ServerRequest; |
6
|
|
|
use Zend\Diactoros\Response; |
7
|
|
|
use DrMVC\Exceptions\ArrayException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Router |
11
|
|
|
* @package DrMVC |
12
|
|
|
* @method Router get(string $pattern, callable $callable): Interfaces\Router |
13
|
|
|
* @method Router post(string $pattern, callable $callable): Interfaces\Router |
14
|
|
|
* @method Router put(string $pattern, callable $callable): Interfaces\Router |
15
|
|
|
* @method Router delete(string $pattern, callable $callable): Interfaces\Router |
16
|
|
|
* @method Router option(string $pattern, callable $callable): Interfaces\Router |
17
|
|
|
*/ |
18
|
|
|
class Router implements Interfaces\Router |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Array with all available routes |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $_routes = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class with error inside |
28
|
|
|
* @var callable|string |
29
|
|
|
*/ |
30
|
|
|
private $_error = 'DrMVC\Router\Error'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Zend\Diactoros\ServerRequest |
34
|
|
|
*/ |
35
|
|
|
private $_request; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Zend\Diactoros\Response |
39
|
|
|
*/ |
40
|
|
|
private $_response; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Router constructor. |
44
|
|
|
* |
45
|
|
|
* @param ServerRequest $request |
46
|
|
|
* @param Response $response |
47
|
|
|
*/ |
48
|
|
|
public function __construct(ServerRequest $request, Response $response) |
49
|
|
|
{ |
50
|
|
|
$this |
51
|
|
|
->setRequest($request) |
52
|
|
|
->setResponse($response); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param mixed $request |
57
|
|
|
* @return Interfaces\Router |
58
|
|
|
*/ |
59
|
|
|
public function setRequest($request): Interfaces\Router |
60
|
|
|
{ |
61
|
|
|
$this->_request = $request; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return ServerRequest |
67
|
|
|
*/ |
68
|
|
|
public function getRequest(): ServerRequest |
69
|
|
|
{ |
70
|
|
|
return $this->_request; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $response |
75
|
|
|
* @return Interfaces\Router |
76
|
|
|
*/ |
77
|
|
|
public function setResponse($response): Interfaces\Router |
78
|
|
|
{ |
79
|
|
|
$this->_response = $response; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Response |
85
|
|
|
*/ |
86
|
|
|
public function getResponse(): Response |
87
|
|
|
{ |
88
|
|
|
return $this->_response; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Overwrite default error class |
93
|
|
|
* |
94
|
|
|
* @param callable|string $error |
95
|
|
|
* @return Interfaces\Router |
96
|
|
|
*/ |
97
|
|
|
public function setError($error): Interfaces\Router |
98
|
|
|
{ |
99
|
|
|
$this->_error = $error; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return callable|string |
105
|
|
|
*/ |
106
|
|
|
public function getError() |
107
|
|
|
{ |
108
|
|
|
$error = $this->_error; |
109
|
|
|
return new $error(); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Parse URI by Regexp from routes and return single route |
114
|
|
|
* |
115
|
|
|
* @return Interfaces\Route |
116
|
|
|
*/ |
117
|
|
|
public function getRoute() |
118
|
|
|
{ |
119
|
|
|
// Find route by regexp and URI |
120
|
|
|
$matches = array_map( |
121
|
|
|
// Foreach emulation |
122
|
|
|
function($regexp, $route) { |
123
|
|
|
$uri = $this->getRequest()->getUri()->getPath(); |
124
|
|
|
$match = preg_match_all($regexp, $uri, $matches); |
125
|
|
|
|
126
|
|
|
// If something found |
127
|
|
|
if ($match) { |
128
|
|
|
// Set array of variables |
129
|
|
|
$route->setVariables($matches); |
130
|
|
|
return $route; |
131
|
|
|
} else { |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
}, |
135
|
|
|
// Array with keys |
136
|
|
|
$this->getRoutes(true), |
137
|
|
|
// Array with values |
138
|
|
|
$this->getRoutes() |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
// Cleanup the array of matches, then reindex array |
142
|
|
|
$matches = array_values(array_filter($matches)); |
143
|
|
|
|
144
|
|
|
// If we have some classes in result of regexp |
145
|
|
|
if (!empty($matches)) { |
146
|
|
|
// Take first from matches |
147
|
|
|
$result = $matches[0]; // Here the Route() object |
148
|
|
|
} else { |
149
|
|
|
// Create new object with error inside |
150
|
|
|
$result = $this->getError(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$result->setRequest($this->getRequest()); |
154
|
|
|
$result->setResponse($this->getResponse()); |
155
|
|
|
|
156
|
|
|
return $result; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Abstraction of setter |
161
|
|
|
* |
162
|
|
|
* @param string $method |
163
|
|
|
* @param array $args |
164
|
|
|
* @return Interfaces\Router |
165
|
|
|
*/ |
166
|
|
|
private function set(string $method, array $args): Interfaces\Router |
167
|
|
|
{ |
168
|
|
|
$pattern = $args[0]; |
169
|
|
|
$callable = $args[1]; |
170
|
|
|
$route = new Route($method, $pattern, $callable, $this->getRequest(), $this->getResponse()); |
171
|
|
|
$this->setRoute($route); |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Some kind of magic ;) details in header of this class, in @methods |
177
|
|
|
* |
178
|
|
|
* @param string $method |
179
|
|
|
* @param $args |
180
|
|
|
* @return Interfaces\Router |
181
|
|
|
*/ |
182
|
|
|
public function __call(string $method, $args) |
183
|
|
|
{ |
184
|
|
|
if (in_array($method, Router::METHODS)) { |
185
|
|
|
$this->set($method, $args); |
186
|
|
|
} |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add route into the array of routes |
192
|
|
|
* |
193
|
|
|
* @param Interfaces\Route $route |
194
|
|
|
*/ |
195
|
|
|
private function setRoute(Interfaces\Route $route) |
196
|
|
|
{ |
197
|
|
|
$regexp = $route->getRegexp(); |
198
|
|
|
$this->_routes[$regexp] = $route; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get all available routes |
203
|
|
|
* |
204
|
|
|
* @param bool $keys - Return only keys |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function getRoutes(bool $keys = false): array |
208
|
|
|
{ |
209
|
|
|
return $keys |
210
|
|
|
? array_keys($this->_routes) |
211
|
|
|
: $this->_routes; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Callable must be only selected methods |
216
|
|
|
* |
217
|
|
|
* @param array $methods |
218
|
|
|
* @param string $pattern |
219
|
|
|
* @param callable $callable |
220
|
|
|
* @return Interfaces\Router |
221
|
|
|
*/ |
222
|
|
|
public function map(array $methods, string $pattern, callable $callable): Interfaces\Router |
223
|
|
|
{ |
224
|
|
|
array_map( |
225
|
|
|
function($method) use ($pattern, $callable) { |
226
|
|
|
$method = strtolower($method); |
227
|
|
|
|
228
|
|
|
try { |
229
|
|
|
ArrayException::inArray($method, Router::METHODS); |
230
|
|
|
} catch (ArrayException $e) { |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$this->$method($pattern, $callable); |
234
|
|
|
}, |
235
|
|
|
$methods |
236
|
|
|
); |
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Any method should be callable |
242
|
|
|
* |
243
|
|
|
* @param string $pattern |
244
|
|
|
* @param callable $callable |
245
|
|
|
* @return Interfaces\Router |
246
|
|
|
*/ |
247
|
|
|
public function any(string $pattern, callable $callable): Interfaces\Router |
248
|
|
|
{ |
249
|
|
|
return $this->map(Router::METHODS, $pattern, $callable); |
250
|
|
|
} |
251
|
|
|
} |