1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2017-2019 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 Closure; |
13
|
|
|
use WPEmerge\Exceptions\ConfigurationException; |
14
|
|
|
use WPEmerge\Helpers\Handler; |
15
|
|
|
use WPEmerge\Helpers\HandlerFactory; |
16
|
|
|
use WPEmerge\Requests\RequestInterface; |
17
|
|
|
use WPEmerge\Routing\Conditions\ConditionFactory; |
18
|
|
|
use WPEmerge\Routing\Conditions\ConditionInterface; |
19
|
|
|
use WPEmerge\Routing\Conditions\UrlableInterface; |
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
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Condition factory. |
30
|
|
|
* |
31
|
|
|
* @var ConditionFactory |
32
|
|
|
*/ |
33
|
|
|
protected $condition_factory = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Handler factory. |
37
|
|
|
* |
38
|
|
|
* @var HandlerFactory |
39
|
|
|
*/ |
40
|
|
|
protected $handler_factory = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Group stack. |
44
|
|
|
* |
45
|
|
|
* @var array<array<string, mixed>> |
46
|
|
|
*/ |
47
|
|
|
protected $group_stack = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Current active route. |
51
|
|
|
* |
52
|
|
|
* @var RouteInterface|null |
53
|
|
|
*/ |
54
|
|
|
protected $current_route = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor. |
58
|
|
|
* |
59
|
|
|
* @codeCoverageIgnore |
60
|
|
|
* @param ConditionFactory $condition_factory |
61
|
|
|
* @param HandlerFactory $handler_factory |
62
|
|
|
*/ |
63
|
|
|
public function __construct( ConditionFactory $condition_factory, HandlerFactory $handler_factory ) { |
64
|
|
|
$this->condition_factory = $condition_factory; |
65
|
|
|
$this->handler_factory = $handler_factory; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the current route. |
70
|
|
|
* |
71
|
|
|
* @return RouteInterface |
72
|
|
|
*/ |
73
|
1 |
|
public function getCurrentRoute() { |
74
|
1 |
|
return $this->current_route; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the current route. |
79
|
|
|
* |
80
|
|
|
* @param RouteInterface $current_route |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
1 |
|
public function setCurrentRoute( RouteInterface $current_route ) { |
84
|
1 |
|
$this->current_route = $current_route; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Merge the methods attribute combining values. |
89
|
|
|
* |
90
|
|
|
* @param string[] $old |
91
|
|
|
* @param string[] $new |
92
|
|
|
* @return string[] |
93
|
|
|
*/ |
94
|
1 |
|
public function mergeMethodsAttribute( $old, $new ) { |
95
|
1 |
|
return array_merge( $old, $new ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Merge the condition attribute. |
100
|
|
|
* |
101
|
|
|
* @param string|array|Closure|ConditionInterface|null $old |
102
|
|
|
* @param string|array|Closure|ConditionInterface|null $new |
103
|
|
|
* @return ConditionInterface|string |
104
|
|
|
*/ |
105
|
3 |
|
public function mergeConditionAttribute( $old, $new ) { |
106
|
|
|
try { |
107
|
3 |
|
$condition = $this->condition_factory->merge( $old, $new ); |
108
|
3 |
|
} catch ( ConfigurationException $e ) { |
109
|
1 |
|
throw new ConfigurationException( 'Route condition is not a valid route string or condition.' ); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
return $condition; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Merge the middleware attribute combining values. |
117
|
|
|
* |
118
|
|
|
* @param string[] $old |
119
|
|
|
* @param string[] $new |
120
|
|
|
* @return string[] |
121
|
|
|
*/ |
122
|
1 |
|
public function mergeMiddlewareAttribute( $old, $new ) { |
123
|
1 |
|
return array_merge( $old, $new ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Merge the namespace attribute taking the latest value. |
128
|
|
|
* |
129
|
|
|
* @param string $old |
130
|
|
|
* @param string $new |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
1 |
|
public function mergeNamespaceAttribute( $old, $new ) { |
134
|
1 |
|
return ! empty( $new ) ? $new : $old; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Merge the handler attribute taking the latest value. |
139
|
|
|
* |
140
|
|
|
* @param string|Closure $old |
141
|
|
|
* @param string|Closure $new |
142
|
|
|
* @return string|Closure |
143
|
|
|
*/ |
144
|
1 |
|
public function mergeHandlerAttribute( $old, $new ) { |
145
|
1 |
|
return ! empty( $new ) ? $new : $old; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Merge the handler attribute taking the latest value. |
150
|
|
|
* |
151
|
|
|
* @param callable|null $old |
152
|
|
|
* @param callable|null $new |
153
|
|
|
* @return string|Closure |
154
|
|
|
*/ |
155
|
1 |
|
public function mergeQueryAttribute( $old, $new ) { |
156
|
1 |
|
if ( $new === null ) { |
157
|
1 |
|
return $old; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
if ( $old === null ) { |
161
|
1 |
|
return $new; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return function ( $query_vars ) use ( $old, $new ) { |
165
|
1 |
|
return call_user_func( $new, call_user_func( $old, $query_vars ) ); |
166
|
1 |
|
}; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Merge the name attribute combining values with a dot. |
171
|
|
|
* |
172
|
|
|
* @param string $old |
173
|
|
|
* @param string $new |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
1 |
|
public function mergeNameAttribute( $old, $new ) { |
177
|
1 |
|
$name = array_filter( [$old, $new] ); |
178
|
1 |
|
return preg_replace( '/\.{2,}/', '.', implode( '.', $name ) ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Merge attributes into route. |
183
|
|
|
* |
184
|
|
|
* @param array<string, mixed> $old |
185
|
|
|
* @param array<string, mixed> $new |
186
|
|
|
* @return array<string, mixed> |
187
|
|
|
*/ |
188
|
1 |
|
public function mergeAttributes( $old, $new ) { |
189
|
|
|
return [ |
190
|
1 |
|
'methods' => $this->mergeMethodsAttribute( |
191
|
1 |
|
(array) Arr::get( $old, 'methods', [] ), |
192
|
1 |
|
(array) Arr::get( $new, 'methods', [] ) |
193
|
1 |
|
), |
194
|
|
|
|
195
|
1 |
|
'condition' => $this->mergeConditionAttribute( |
196
|
1 |
|
Arr::get( $old, 'condition', null ), |
197
|
1 |
|
Arr::get( $new, 'condition', null ) |
198
|
1 |
|
), |
199
|
|
|
|
200
|
1 |
|
'middleware' => $this->mergeMiddlewareAttribute( |
201
|
1 |
|
(array) Arr::get( $old, 'middleware', [] ), |
202
|
1 |
|
(array) Arr::get( $new, 'middleware', [] ) |
203
|
1 |
|
), |
204
|
|
|
|
205
|
1 |
|
'namespace' => $this->mergeNamespaceAttribute( |
206
|
1 |
|
Arr::get( $old, 'namespace', '' ), |
207
|
1 |
|
Arr::get( $new, 'namespace', '' ) |
208
|
1 |
|
), |
209
|
|
|
|
210
|
1 |
|
'handler' => $this->mergeHandlerAttribute( |
211
|
1 |
|
Arr::get( $old, 'handler', '' ), |
212
|
1 |
|
Arr::get( $new, 'handler', '' ) |
213
|
1 |
|
), |
214
|
|
|
|
215
|
1 |
|
'query' => $this->mergeQueryAttribute( |
216
|
1 |
|
Arr::get( $old, 'query', null ), |
217
|
1 |
|
Arr::get( $new, 'query', null ) |
218
|
1 |
|
), |
219
|
|
|
|
220
|
1 |
|
'name' => $this->mergeNameAttribute( |
221
|
1 |
|
Arr::get( $old, 'name', null ), |
222
|
1 |
|
Arr::get( $new, 'name', null ) |
223
|
1 |
|
), |
224
|
1 |
|
]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the top group from the stack. |
229
|
|
|
* |
230
|
|
|
* @codeCoverageIgnore |
231
|
|
|
* @return array<string, mixed> |
232
|
|
|
*/ |
233
|
|
|
protected function getGroup() { |
234
|
|
|
return Arr::last( $this->group_stack, null, [] ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Add a group to the group stack, merging all previous attributes. |
239
|
|
|
* |
240
|
|
|
* @codeCoverageIgnore |
241
|
|
|
* @param array<string, mixed> $group |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
protected function pushGroup( $group ) { |
245
|
|
|
$this->group_stack[] = $this->mergeAttributes( $this->getGroup(), $group ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Remove last group from the group stack. |
250
|
|
|
* |
251
|
|
|
* @codeCoverageIgnore |
252
|
|
|
* @return void |
253
|
|
|
*/ |
254
|
|
|
protected function popGroup() { |
255
|
|
|
array_pop( $this->group_stack ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Create a route group. |
260
|
|
|
* |
261
|
|
|
* @codeCoverageIgnore |
262
|
|
|
* @param array<string, mixed> $attributes |
263
|
|
|
* @param Closure|string $routes Closure or path to file. |
264
|
|
|
* @return void |
265
|
|
|
*/ |
266
|
|
|
public function group( $attributes, $routes ) { |
267
|
|
|
$this->pushGroup( $attributes ); |
268
|
|
|
|
269
|
|
|
if ( is_string( $routes ) ) { |
270
|
|
|
/** @noinspection PhpIncludeInspection */ |
271
|
|
|
/** @codeCoverageIgnore */ |
272
|
|
|
require_once $routes; |
273
|
|
|
} else { |
274
|
|
|
$routes(); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$this->popGroup(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Make a route condition. |
282
|
|
|
* |
283
|
|
|
* @param mixed $condition |
284
|
|
|
* @return ConditionInterface |
285
|
|
|
*/ |
286
|
3 |
|
protected function routeCondition( $condition ) { |
287
|
3 |
|
if ( $condition === null ) { |
288
|
1 |
|
throw new ConfigurationException( 'No route condition specified. Did you miss to call url() or where()?' ); |
289
|
|
|
} |
290
|
|
|
|
291
|
2 |
|
if ( ! $condition instanceof ConditionInterface ) { |
292
|
1 |
|
$condition = $this->condition_factory->make( $condition ); |
293
|
1 |
|
} |
294
|
|
|
|
295
|
2 |
|
return $condition; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Make a route handler. |
300
|
|
|
* |
301
|
|
|
* @codeCoverageIgnore |
302
|
|
|
* @param string|Closure|null $handler |
303
|
|
|
* @param string $namespace |
304
|
|
|
* @return Handler |
305
|
|
|
*/ |
306
|
|
|
protected function routeHandler( $handler, $namespace ) { |
307
|
|
|
if ( $handler === null ) { |
308
|
|
|
throw new ConfigurationException( 'No route handler specified. Did you miss to call handle()?' ); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return $this->handler_factory->make( $handler, '', $namespace ); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Make a route. |
316
|
|
|
* |
317
|
|
|
* @param array<string, mixed> $attributes |
318
|
|
|
* @return RouteInterface |
319
|
|
|
*/ |
320
|
2 |
|
public function route( $attributes ) { |
321
|
2 |
|
$attributes = $this->mergeAttributes( $this->getGroup(), $attributes ); |
322
|
2 |
|
$attributes = array_merge( |
323
|
2 |
|
$attributes, |
324
|
|
|
[ |
325
|
2 |
|
'condition' => $this->routeCondition( $attributes['condition'] ), |
326
|
2 |
|
'handler' => $this->routeHandler( $attributes['handler'], $attributes['namespace'] ), |
327
|
|
|
] |
328
|
2 |
|
); |
329
|
|
|
|
330
|
2 |
|
if ( empty( $attributes['methods'] ) ) { |
331
|
1 |
|
throw new ConfigurationException( |
332
|
|
|
'Route does not have any assigned request methods. ' . |
333
|
|
|
'Did you miss to call get() or post() on your route definition, for example?' |
334
|
1 |
|
); |
335
|
|
|
} |
336
|
|
|
|
337
|
1 |
|
return (new Route())->attributes( $attributes ); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Assign and return the first satisfied route (if any) as the current one for the given request. |
342
|
|
|
* |
343
|
|
|
* @param RequestInterface $request |
344
|
|
|
* @return RouteInterface|null |
345
|
|
|
*/ |
346
|
2 |
|
public function execute( $request ) { |
347
|
2 |
|
$routes = $this->getRoutes(); |
348
|
|
|
|
349
|
2 |
|
foreach ( $routes as $route ) { |
350
|
2 |
|
if ( $route->isSatisfied( $request ) ) { |
351
|
1 |
|
$this->setCurrentRoute( $route ); |
352
|
1 |
|
return $route; |
353
|
|
|
} |
354
|
1 |
|
} |
355
|
|
|
|
356
|
1 |
|
return null; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Get the url for a named route. |
361
|
|
|
* |
362
|
|
|
* @param string $name |
363
|
|
|
* @param array $arguments |
364
|
|
|
* @return string |
365
|
|
|
*/ |
366
|
3 |
|
public function getRouteUrl( $name, $arguments = [] ) { |
367
|
3 |
|
$routes = $this->getRoutes(); |
368
|
|
|
|
369
|
3 |
|
foreach ( $routes as $route ) { |
370
|
2 |
|
if ( $route->getAttribute( 'name' ) !== $name ) { |
371
|
|
|
continue; |
372
|
|
|
} |
373
|
|
|
|
374
|
2 |
|
$condition = $route->getAttribute( 'condition' ); |
375
|
|
|
|
376
|
2 |
|
if ( ! $condition instanceof UrlableInterface ) { |
377
|
1 |
|
throw new ConfigurationException( |
378
|
|
|
'Route condition is not resolvable to a URL.' |
379
|
1 |
|
); |
380
|
|
|
} |
381
|
|
|
|
382
|
1 |
|
return $condition->toUrl( $arguments ); |
383
|
1 |
|
} |
384
|
|
|
|
385
|
1 |
|
throw new ConfigurationException( "No route registered with the name \"$name\"." ); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|