|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package WPEmerge |
|
4
|
|
|
* @author Atanas Angelov <[email protected]> |
|
5
|
|
|
* @copyright 2018 Atanas Angelov |
|
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
|
7
|
|
|
* @link https://wpemerge.com/ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace WPEmerge\Routing; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use WPEmerge\Exceptions\ErrorHandlerInterface; |
|
15
|
|
|
use WPEmerge\Requests\RequestInterface; |
|
16
|
|
|
use WPEmerge\Routing\Conditions\ConditionFactory; |
|
17
|
|
|
use WPEmerge\Routing\Conditions\ConditionInterface; |
|
18
|
|
|
use WPEmerge\Routing\Conditions\HasUrlWhereInterface; |
|
19
|
|
|
use WPEmerge\Routing\Conditions\InvalidRouteConditionException; |
|
20
|
|
|
use WPEmerge\Support\Arr; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Provide routing for site requests (i.e. all non-api requests). |
|
24
|
|
|
*/ |
|
25
|
|
|
class Router implements HasRoutesInterface { |
|
26
|
|
|
use HasRoutesTrait { |
|
27
|
|
|
addRoute as traitAddRoute; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Condition factory. |
|
32
|
|
|
* |
|
33
|
|
|
* @var ConditionFactory |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $condition_factory = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Middleware available to the application. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $middleware = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Middleware groups. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array<string, array> |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $middleware_groups = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Global middleware that will be applied to all routes. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $global_middleware = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Middleware sorted in order of execution. |
|
60
|
|
|
* |
|
61
|
|
|
* @var array<string> |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $middleware_priority = []; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Exception handler. |
|
67
|
|
|
* |
|
68
|
|
|
* @var ErrorHandlerInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $error_handler = null; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Current active route. |
|
74
|
|
|
* |
|
75
|
|
|
* @var RouteInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $current_route = null; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Group stack. |
|
81
|
|
|
* |
|
82
|
|
|
* @var array<array<string, mixed>> |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $group_stack = []; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Constructor. |
|
88
|
|
|
* |
|
89
|
|
|
* @codeCoverageIgnore |
|
90
|
|
|
* @param ConditionFactory $condition_factory |
|
91
|
|
|
* @param ErrorHandlerInterface $error_handler |
|
92
|
|
|
*/ |
|
93
|
|
|
public function __construct( |
|
94
|
|
|
ConditionFactory $condition_factory, |
|
95
|
|
|
ErrorHandlerInterface $error_handler |
|
96
|
|
|
) { |
|
97
|
|
|
$this->condition_factory = $condition_factory; |
|
98
|
|
|
$this->error_handler = $error_handler; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Register middleware. |
|
103
|
|
|
* |
|
104
|
|
|
* @codeCoverageIgnore |
|
105
|
|
|
* @param array $middleware |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setMiddleware( $middleware ) { |
|
109
|
|
|
$this->middleware = $middleware; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Register middleware groups. |
|
114
|
|
|
* |
|
115
|
|
|
* @codeCoverageIgnore |
|
116
|
|
|
* @param array $middleware_groups |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setMiddlewareGroups( $middleware_groups ) { |
|
120
|
|
|
$this->middleware_groups = $middleware_groups; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Register global middleware. |
|
125
|
|
|
* |
|
126
|
|
|
* @codeCoverageIgnore |
|
127
|
|
|
* @param array $middleware |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setGlobalMiddleware( $middleware ) { |
|
131
|
|
|
$this->global_middleware = $middleware; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Register middleware execution priority. |
|
136
|
|
|
* |
|
137
|
|
|
* @codeCoverageIgnore |
|
138
|
|
|
* @param array $middleware_priority |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setMiddlewarePriority( $middleware_priority ) { |
|
142
|
|
|
$this->middleware_priority = $middleware_priority; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get middleware priority. |
|
147
|
|
|
* This is in reverse compared to definition order. |
|
148
|
|
|
* Middleware with unspecified priority will yield -1. |
|
149
|
|
|
* |
|
150
|
|
|
* @param mixed $middleware |
|
151
|
|
|
* @return integer |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function getMiddlewarePriority( $middleware ) { |
|
154
|
1 |
|
$increasing_priority = array_reverse( $this->middleware_priority ); |
|
155
|
1 |
|
$priority = array_search( $middleware, $increasing_priority ); |
|
156
|
1 |
|
return $priority !== false ? $priority : -1; |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Sort middleware by priority in ascending order. |
|
161
|
|
|
* |
|
162
|
|
|
* @param array $middleware |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function sortMiddleware( $middleware ) { |
|
166
|
1 |
|
$sorted = $middleware; |
|
167
|
|
|
|
|
168
|
1 |
|
usort( $sorted, function ( $a, $b ) use ( $middleware ) { |
|
169
|
1 |
|
$priority = $this->getMiddlewarePriority( $b ) - $this->getMiddlewarePriority( $a ); |
|
170
|
|
|
|
|
171
|
1 |
|
if ( $priority !== 0 ) { |
|
172
|
1 |
|
return $priority; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// Keep relative order from original array. |
|
176
|
1 |
|
return array_search( $a, $middleware ) - array_search( $b, $middleware ); |
|
177
|
1 |
|
} ); |
|
178
|
|
|
|
|
179
|
1 |
|
return array_values( $sorted ); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get the current route. |
|
184
|
|
|
* |
|
185
|
|
|
* @return RouteInterface |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function getCurrentRoute() { |
|
188
|
1 |
|
return $this->current_route; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Set the current route. |
|
193
|
|
|
* |
|
194
|
|
|
* @param RouteInterface |
|
195
|
|
|
* @return void |
|
196
|
|
|
*/ |
|
197
|
1 |
|
public function setCurrentRoute( RouteInterface $current_route ) { |
|
198
|
1 |
|
$this->current_route = $current_route; |
|
199
|
1 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Add a group to the group stack, merging all previous attributes. |
|
203
|
|
|
* |
|
204
|
|
|
* @param array<string, mixed> $attributes |
|
205
|
|
|
* @return void |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function addGroupToStack( $attributes ) { |
|
208
|
|
|
$previous = Arr::last( $this->group_stack, null, [] ); |
|
209
|
|
|
|
|
210
|
|
|
$condition = $this->condition_factory->merge( |
|
211
|
|
|
Arr::get( $previous, 'condition', '' ), |
|
212
|
|
|
Arr::get( $attributes, 'condition', '' ) |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
$attributes = array( |
|
216
|
|
|
'condition' => $condition !== null ? $condition : '', |
|
217
|
|
|
'where' => array_merge( |
|
218
|
|
|
Arr::get( $previous, 'where', [] ), |
|
219
|
|
|
Arr::get( $attributes, 'where', [] ) |
|
220
|
|
|
), |
|
221
|
|
|
'middleware' => array_merge( |
|
222
|
|
|
(array) Arr::get( $previous, 'middleware', [] ), |
|
223
|
|
|
(array) Arr::get( $attributes, 'middleware', [] ) |
|
224
|
|
|
), |
|
225
|
|
|
); |
|
226
|
|
|
|
|
227
|
|
|
$this->group_stack[] = $attributes; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Remove last group from the group stack. |
|
232
|
|
|
* |
|
233
|
|
|
* @return void |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function removeLastGroupFromStack() { |
|
236
|
|
|
array_pop( $this->group_stack ); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Create a new route group. |
|
241
|
|
|
* |
|
242
|
|
|
* @param array<string, mixed> $attributes |
|
243
|
|
|
* @param \Closure $routes |
|
244
|
|
|
* @return void |
|
245
|
|
|
*/ |
|
246
|
1 |
|
public function group( $attributes, $routes ) { |
|
247
|
1 |
|
$this->addGroupToStack( $attributes ); |
|
248
|
|
|
|
|
249
|
1 |
|
$routes(); |
|
250
|
|
|
|
|
251
|
1 |
|
$this->removeLastGroupFromStack(); |
|
252
|
1 |
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* {@inheritDoc} |
|
256
|
|
|
*/ |
|
257
|
|
|
public function makeRoute( $methods, $condition, $handler ) { |
|
258
|
|
|
if ( ! $condition instanceof ConditionInterface ) { |
|
259
|
|
|
try { |
|
260
|
|
|
$condition = $this->condition_factory->make( $condition ); |
|
261
|
|
|
} catch ( InvalidRouteConditionException $e ) { |
|
262
|
|
|
throw new InvalidRouteConditionException( 'Route condition is not a valid route string or condition.' ); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return new Route( $methods, $condition, $handler ); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* {@inheritDoc} |
|
271
|
|
|
*/ |
|
272
|
2 |
|
public function addRoute( $route ) { |
|
273
|
2 |
|
$group = Arr::last( $this->group_stack, null, [] ); |
|
274
|
2 |
|
$condition = $route->getCondition(); |
|
275
|
|
|
|
|
276
|
2 |
|
if ( $condition instanceof HasUrlWhereInterface ) { |
|
277
|
|
|
$condition->setUrlWhere( array_merge( |
|
278
|
|
|
Arr::get( $group, 'where', [] ), |
|
279
|
|
|
$condition->getUrlWhere() |
|
280
|
|
|
) ); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
2 |
|
$condition = $this->condition_factory->merge( |
|
284
|
2 |
|
Arr::get( $group, 'condition', '' ), |
|
285
|
2 |
|
$condition |
|
286
|
|
|
); |
|
287
|
|
|
|
|
288
|
2 |
|
$route->setCondition( $condition ); |
|
289
|
|
|
|
|
290
|
2 |
|
$route->setMiddleware( array_merge( |
|
291
|
2 |
|
$this->global_middleware, |
|
292
|
2 |
|
Arr::get( $group, 'middleware', [] ), |
|
293
|
2 |
|
$route->getMiddleware() |
|
294
|
|
|
) ); |
|
295
|
|
|
|
|
296
|
2 |
|
return $this->traitAddRoute( $route ); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Handle ALL requests. |
|
301
|
|
|
* |
|
302
|
|
|
* @param string|\Closure|null $handler |
|
303
|
|
|
* @return RouteInterface |
|
304
|
|
|
*/ |
|
305
|
1 |
|
public function handleAll( $handler = null ) { |
|
306
|
|
|
// Match ANY request method. |
|
307
|
|
|
// Match ANY url. |
|
308
|
|
|
// By default, use built-in WordPress controller. |
|
309
|
1 |
|
return $this->any( '*', $handler ); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Execute a route. |
|
314
|
|
|
* |
|
315
|
|
|
* @param RequestInterface $request |
|
316
|
|
|
* @param RouteInterface $route |
|
317
|
|
|
* @param string $view |
|
318
|
|
|
* @return ResponseInterface |
|
319
|
|
|
*/ |
|
320
|
2 |
|
protected function handle( RequestInterface $request, RouteInterface $route, $view ) { |
|
321
|
|
|
try { |
|
322
|
2 |
|
$this->error_handler->register(); |
|
323
|
|
|
|
|
324
|
2 |
|
$handler = function ( $request, $view ) use ( $route ) { |
|
325
|
2 |
|
return $route->handle( $request, $view ); |
|
326
|
2 |
|
}; |
|
327
|
|
|
|
|
328
|
2 |
|
$response = ( new Pipeline() ) |
|
329
|
2 |
|
->middleware( $this->global_middleware ) |
|
330
|
2 |
|
->middleware( $this->sortMiddleware( $route->getMiddleware() ) ) |
|
331
|
2 |
|
->to( $handler ) |
|
332
|
2 |
|
->run( $request, [$request, $view] ); |
|
333
|
|
|
|
|
334
|
1 |
|
$this->error_handler->unregister(); |
|
335
|
1 |
|
} catch ( Exception $exception ) { |
|
336
|
1 |
|
$response = $this->error_handler->getResponse( $exception ); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
1 |
|
return $response; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Execute the first satisfied route, if any. |
|
344
|
|
|
* |
|
345
|
|
|
* @param RequestInterface $request |
|
346
|
|
|
* @param string $view |
|
347
|
|
|
* @return ResponseInterface|null |
|
348
|
|
|
*/ |
|
349
|
3 |
|
public function execute( $request, $view ) { |
|
350
|
3 |
|
$routes = $this->getRoutes(); |
|
351
|
|
|
|
|
352
|
3 |
|
foreach ( $routes as $route ) { |
|
353
|
3 |
|
if ( $route->isSatisfied( $request ) ) { |
|
354
|
2 |
|
$this->setCurrentRoute( $route ); |
|
355
|
3 |
|
return $this->handle( $request, $route, $view ); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
1 |
|
return null; |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|