1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-server |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Apparat\Server\Ports |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Server\Ports\Route; |
38
|
|
|
|
39
|
|
|
use Apparat\Server\Ports\Action\ActionInterface; |
40
|
|
|
use Apparat\Server\Ports\Contract\RouteInterface; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Route |
44
|
|
|
* |
45
|
|
|
* @package Apparat\Server |
46
|
|
|
* @subpackage Apparat\Server\Ports |
47
|
|
|
*/ |
48
|
|
|
class Route implements RouteInterface |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* GET request |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
const GET = 'GET'; |
56
|
|
|
/** |
57
|
|
|
* POST request |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
const POST = 'POST'; |
62
|
|
|
/** |
63
|
|
|
* PATCH request |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
const PATCH = 'PATCH'; |
68
|
|
|
/** |
69
|
|
|
* DELETE request |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
const DELETE = 'DELETE'; |
74
|
|
|
/** |
75
|
|
|
* OPTIONS request |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
const OPTIONS = 'OPTIONS'; |
80
|
|
|
/** |
81
|
|
|
* HEAD request |
82
|
|
|
* |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
const HEAD = 'HEAD'; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Allowed HTTP verbs |
89
|
|
|
* |
90
|
|
|
* @var array |
91
|
|
|
*/ |
92
|
|
|
protected static $validVerbs = [ |
93
|
|
|
self::GET => true, |
94
|
|
|
self::POST => true, |
95
|
|
|
self::PATCH => true, |
96
|
|
|
self::DELETE => true, |
97
|
|
|
self::OPTIONS => true, |
98
|
|
|
self::HEAD => true |
99
|
|
|
]; |
100
|
|
|
/** |
101
|
|
|
* Route name |
102
|
|
|
* |
103
|
|
|
* @var string |
104
|
|
|
*/ |
105
|
|
|
protected $name; |
106
|
|
|
/** |
107
|
|
|
* Route path |
108
|
|
|
* |
109
|
|
|
* @var string |
110
|
|
|
*/ |
111
|
|
|
protected $path; |
112
|
|
|
/** |
113
|
|
|
* Route action |
114
|
|
|
* |
115
|
|
|
* @var string|callable |
116
|
|
|
*/ |
117
|
|
|
protected $action; |
118
|
|
|
/** |
119
|
|
|
* Route tokens |
120
|
|
|
* |
121
|
|
|
* @var array |
122
|
|
|
*/ |
123
|
|
|
protected $tokens = []; |
124
|
|
|
/** |
125
|
|
|
* Route default values |
126
|
|
|
* |
127
|
|
|
* @var array |
128
|
|
|
*/ |
129
|
|
|
protected $defaults = []; |
130
|
|
|
/** |
131
|
|
|
* Route wildcard name |
132
|
|
|
* |
133
|
|
|
* @var string|null |
134
|
|
|
*/ |
135
|
|
|
protected $wildcard = null; |
136
|
|
|
/** |
137
|
|
|
* Route host |
138
|
|
|
* |
139
|
|
|
* @var string|null |
140
|
|
|
*/ |
141
|
|
|
protected $host = null; |
142
|
|
|
/** |
143
|
|
|
* Allowed accept headers |
144
|
|
|
* |
145
|
|
|
* @var array |
146
|
|
|
*/ |
147
|
|
|
protected $accepts = []; |
148
|
|
|
/** |
149
|
|
|
* Allowed HTTP verbs |
150
|
|
|
* |
151
|
|
|
* @var array |
152
|
|
|
*/ |
153
|
|
|
protected $verbs = []; |
154
|
|
|
/** |
155
|
|
|
* Secure protocol behaviour |
156
|
|
|
* |
157
|
|
|
* @var boolean|null |
158
|
|
|
*/ |
159
|
|
|
protected $secure = false; |
160
|
|
|
/** |
161
|
|
|
* Authentication parameters |
162
|
|
|
* |
163
|
|
|
* @var mixed |
164
|
|
|
*/ |
165
|
|
|
protected $auth = null; |
166
|
|
|
/** |
167
|
|
|
* Custom extra parameters |
168
|
|
|
* |
169
|
|
|
* @var array |
170
|
|
|
*/ |
171
|
|
|
protected $extras = []; |
172
|
|
|
/** |
173
|
|
|
* Object route |
174
|
|
|
* |
175
|
|
|
* @var bool |
176
|
|
|
*/ |
177
|
|
|
protected $object = false; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Route constructor |
181
|
|
|
* |
182
|
|
|
* @param string|array $verbs Allowed HTTP verbs |
183
|
|
|
* @param string $name Route name |
184
|
|
|
* @param string $path Route path |
185
|
|
|
* @param string|callable|array $action Route action |
186
|
|
|
*/ |
187
|
1 |
|
public function __construct($verbs, $name, $path, $action) |
188
|
|
|
{ |
189
|
|
|
// Set whether this is an object route |
190
|
1 |
|
$this->object = is_array($action); |
191
|
|
|
|
192
|
|
|
// Set and validate the allowed HTTP verbs |
193
|
1 |
|
$this->setAndValidateVerbs($verbs); |
194
|
|
|
|
195
|
|
|
// Set and validate the route name |
196
|
1 |
|
$this->setAndValidateName($name); |
197
|
|
|
|
198
|
|
|
// Set and validate the route path |
199
|
1 |
|
$this->setAndValidatePath($path); |
200
|
|
|
|
201
|
|
|
// Set and validate the route action |
202
|
1 |
|
$this->setAndValidateActionList($action); |
|
|
|
|
203
|
1 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Validate the allowed HTTP verbs |
207
|
|
|
* |
208
|
|
|
* @param string|array $verbs Allowed HTTP verbs |
209
|
|
|
* @throws InvalidArgumentException If the HTTP verb list is empty |
210
|
|
|
* @throws InvalidArgumentException If the HTTP verb is invalid |
211
|
|
|
*/ |
212
|
1 |
|
protected function setAndValidateVerbs($verbs) |
213
|
|
|
{ |
214
|
1 |
|
$this->verbs = array_map('strtoupper', array_filter((array)$verbs)); |
215
|
|
|
|
216
|
|
|
// If the HTTP verb list is empty |
217
|
1 |
|
if (!count($this->verbs)) { |
218
|
|
|
throw new InvalidArgumentException( |
219
|
|
|
'Empty route HTTP verbs', |
220
|
|
|
InvalidArgumentException::EMPTY_ROUTE_HTTP_VERBS |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// Run through all registered HTTP verbs |
225
|
1 |
|
foreach ($this->verbs as $verb) { |
226
|
|
|
// If the HTTP verb is invalid |
227
|
1 |
|
if (!array_key_exists($verb, self::$validVerbs)) { |
228
|
|
|
throw new InvalidArgumentException( |
229
|
|
|
sprintf('Invalid route HTTP verb "%s"', $verb), |
230
|
1 |
|
InvalidArgumentException::INVALID_ROUTE_HTTP_VERB |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
} |
234
|
1 |
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Set and validate the route name |
238
|
|
|
* |
239
|
|
|
* @param string $name Route name |
240
|
|
|
* @throws InvalidArgumentException If the route name is empty |
241
|
|
|
*/ |
242
|
1 |
|
protected function setAndValidateName($name) |
243
|
|
|
{ |
244
|
1 |
|
$this->name = trim($name); |
245
|
|
|
// If the route name is empty |
246
|
1 |
|
if (!strlen($this->name)) { |
247
|
|
|
throw new InvalidArgumentException( |
248
|
|
|
'Route name must not be empty', |
249
|
|
|
InvalidArgumentException::EMPTY_ROUTE_NAME |
250
|
|
|
); |
251
|
|
|
} |
252
|
1 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set and validate the route path |
256
|
|
|
* |
257
|
|
|
* @param string $path Route path |
258
|
|
|
* @throws InvalidArgumentException If the route path is empty |
259
|
|
|
*/ |
260
|
1 |
|
protected function setAndValidatePath($path) |
261
|
|
|
{ |
262
|
1 |
|
$this->path = trim($path); |
263
|
|
|
// If the route path is empty |
264
|
1 |
|
if (!strlen($this->path)) { |
265
|
|
|
throw new InvalidArgumentException( |
266
|
|
|
'Route path must not be empty', |
267
|
|
|
InvalidArgumentException::EMPTY_ROUTE_NAME |
268
|
|
|
); |
269
|
|
|
} |
270
|
1 |
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Set and validate the route action |
274
|
|
|
* |
275
|
|
|
* @param string|array $actions Route actions |
276
|
|
|
*/ |
277
|
1 |
|
protected function setAndValidateActionList($actions) |
278
|
|
|
{ |
279
|
1 |
|
array_map([$this, 'setAndValidateAction'], (array)$actions); |
280
|
1 |
|
$this->action = $actions; |
281
|
1 |
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get the route name |
285
|
|
|
* |
286
|
|
|
* @return string Route name |
287
|
|
|
*/ |
288
|
1 |
|
public function getName() |
289
|
|
|
{ |
290
|
1 |
|
return $this->name; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get the route path |
295
|
|
|
* |
296
|
|
|
* @return string Route path |
297
|
|
|
*/ |
298
|
1 |
|
public function getPath() |
299
|
|
|
{ |
300
|
1 |
|
return $this->path; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Get the route action |
305
|
|
|
* |
306
|
|
|
* @return callable|string Route action |
307
|
|
|
*/ |
308
|
1 |
|
public function getAction() |
309
|
|
|
{ |
310
|
1 |
|
return $this->action; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Get the route tokens |
315
|
|
|
* |
316
|
|
|
* @return array Route tokens |
317
|
|
|
*/ |
318
|
1 |
|
public function getTokens() |
319
|
|
|
{ |
320
|
1 |
|
return $this->tokens; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Set the route tokens |
325
|
|
|
* |
326
|
|
|
* @param array $tokens Route tokens |
327
|
|
|
* @return Route Self reference |
328
|
|
|
*/ |
329
|
1 |
|
public function setTokens(array $tokens) |
330
|
|
|
{ |
331
|
1 |
|
$this->tokens = $tokens; |
332
|
1 |
|
return $this; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Get the route defaults |
337
|
|
|
* |
338
|
|
|
* @return array Route defaults |
339
|
|
|
*/ |
340
|
1 |
|
public function getDefaults() |
341
|
|
|
{ |
342
|
1 |
|
return $this->defaults; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Set the route defaults |
347
|
|
|
* |
348
|
|
|
* @param array $defaults Route defaults |
349
|
|
|
* @return Route Self reference |
350
|
|
|
*/ |
351
|
|
|
public function setDefaults(array $defaults) |
352
|
|
|
{ |
353
|
|
|
$this->defaults = $defaults; |
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Set the route wildcard name |
359
|
|
|
* |
360
|
|
|
* @return null|string |
361
|
|
|
*/ |
362
|
1 |
|
public function getWildcard() |
363
|
|
|
{ |
364
|
1 |
|
return $this->wildcard; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Get the route wildcard name |
369
|
|
|
* |
370
|
|
|
* @param null|string $wildcard Wildcard name |
371
|
|
|
* @return Route Self reference |
372
|
|
|
*/ |
373
|
|
|
public function setWildcard($wildcard) |
374
|
|
|
{ |
375
|
|
|
$this->wildcard = $wildcard; |
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Get the route host |
381
|
|
|
* |
382
|
|
|
* @return null|string Route host |
383
|
|
|
*/ |
384
|
1 |
|
public function getHost() |
385
|
|
|
{ |
386
|
1 |
|
return $this->host; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Set the route host |
391
|
|
|
* |
392
|
|
|
* @param null|string $host Route host |
393
|
|
|
* @return Route Self reference |
394
|
|
|
*/ |
395
|
|
|
public function setHost($host) |
396
|
|
|
{ |
397
|
|
|
$this->host = $host; |
398
|
|
|
return $this; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Get the allowed Accept headers |
403
|
|
|
* |
404
|
|
|
* @return array Allowed Accept headers |
405
|
|
|
*/ |
406
|
1 |
|
public function getAccepts() |
407
|
|
|
{ |
408
|
1 |
|
return $this->accepts; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Set the allowed Accept headers |
413
|
|
|
* |
414
|
|
|
* @param array $accepts Allowed Accept headers |
415
|
|
|
* @return Route Self reference |
416
|
|
|
*/ |
417
|
|
|
public function setAccepts(array $accepts) |
418
|
|
|
{ |
419
|
|
|
$this->accepts = $accepts; |
420
|
|
|
return $this; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Get the allowed HTTP verbs |
425
|
|
|
* |
426
|
|
|
* @return array Allowed HTTP verbs |
427
|
|
|
*/ |
428
|
1 |
|
public function getVerbs() |
429
|
|
|
{ |
430
|
1 |
|
return $this->verbs; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Set the allowed HTTP verbs |
435
|
|
|
* |
436
|
|
|
* @param array $verbs Allowed HTTP verbs |
437
|
|
|
* @return Route Self reference |
438
|
|
|
*/ |
439
|
|
|
public function setVerbs(array $verbs) |
440
|
|
|
{ |
441
|
|
|
$this->verbs = $verbs; |
442
|
|
|
return $this; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Get the secure protocol mode |
447
|
|
|
* |
448
|
|
|
* @return boolean|null Secure protocol mode |
449
|
|
|
*/ |
450
|
1 |
|
public function getSecure() |
451
|
|
|
{ |
452
|
1 |
|
return $this->secure; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Set the secure protocol mode |
457
|
|
|
* |
458
|
|
|
* @param boolean|null $secure Secure protocol mode |
459
|
|
|
* @return Route Self reference |
460
|
|
|
*/ |
461
|
|
|
public function setSecure($secure) |
462
|
|
|
{ |
463
|
|
|
$this->secure = $secure; |
464
|
|
|
return $this; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Get the authentication information |
469
|
|
|
* |
470
|
|
|
* @return mixed Authentication information |
471
|
|
|
*/ |
472
|
1 |
|
public function getAuth() |
473
|
|
|
{ |
474
|
1 |
|
return $this->auth; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Set the authentication information |
479
|
|
|
* |
480
|
|
|
* @param mixed $auth Authentication information |
481
|
|
|
* @return Route Self reference |
482
|
|
|
*/ |
483
|
|
|
public function setAuth($auth) |
484
|
|
|
{ |
485
|
|
|
$this->auth = $auth; |
486
|
|
|
return $this; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Get the custom extra information |
491
|
|
|
* |
492
|
|
|
* @return array Custom extra information |
493
|
|
|
*/ |
494
|
1 |
|
public function getExtras() |
495
|
|
|
{ |
496
|
1 |
|
return $this->extras; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Set the custom extra information |
501
|
|
|
* |
502
|
|
|
* @param array $extras Custom extra information |
503
|
|
|
* @return Route Self reference |
504
|
|
|
*/ |
505
|
|
|
public function setExtras(array $extras) |
506
|
|
|
{ |
507
|
|
|
$this->extras = $extras; |
508
|
|
|
return $this; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Return whether this is an object route |
513
|
|
|
* |
514
|
|
|
* @return boolean Object route |
515
|
|
|
*/ |
516
|
1 |
|
public function isObject() |
517
|
|
|
{ |
518
|
1 |
|
return $this->object; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Set and validate the route action |
523
|
|
|
* |
524
|
|
|
* @param string $action Route action |
525
|
|
|
* @throws InvalidArgumentException If the route action is empty |
526
|
|
|
* @throws InvalidArgumentException If the route action is not a class name |
527
|
|
|
* @throws InvalidArgumentException If the route action is neither a callable nor an ActionInterface |
528
|
|
|
*/ |
529
|
1 |
|
protected function setAndValidateAction($action) |
530
|
|
|
{ |
531
|
|
|
// If the action is given as string |
532
|
1 |
|
if (is_string($action)) { |
533
|
1 |
|
$this->action = trim($action); |
534
|
|
|
|
535
|
|
|
// If the route action is empty |
536
|
1 |
|
if (!strlen($this->action)) { |
537
|
|
|
throw new InvalidArgumentException( |
538
|
|
|
'Route action must not be empty', |
539
|
|
|
InvalidArgumentException::EMPTY_ROUTE_ACTION |
540
|
|
|
); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
// If the route action is not a class name |
544
|
1 |
|
if (!class_exists($this->action)) { |
545
|
|
|
throw new InvalidArgumentException( |
546
|
|
|
'Route action must be an existing class name', |
547
|
|
|
InvalidArgumentException::ROUTE_ACTION_MUST_BE_CLASSNAME |
548
|
|
|
); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
// If the route action doesn't implement the ActionInterface |
552
|
1 |
|
$actionReflection = new \ReflectionClass($this->action); |
553
|
1 |
|
if (!$actionReflection->implementsInterface(ActionInterface::class)) { |
554
|
|
|
throw new InvalidArgumentException( |
555
|
|
|
'Route action must implement '.ActionInterface::class, |
556
|
|
|
InvalidArgumentException::ROUTE_ACTION_MUST_IMPLEMENT_ACTION_INTERFACE |
557
|
|
|
); |
558
|
|
|
} |
559
|
|
|
|
560
|
1 |
|
return; |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
// If the action is given as callable |
564
|
|
|
if (is_callable($action)) { |
565
|
|
|
return; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
// If the route action is neither a callable nor an ActionInterface |
569
|
|
|
throw new InvalidArgumentException( |
570
|
|
|
'Route action must be a callable or '.ActionInterface::class, |
571
|
|
|
InvalidArgumentException::ROUTE_ACTION_NOT_CALLABLE_OR_ACTION_INTERFACE |
572
|
|
|
); |
573
|
|
|
} |
574
|
|
|
} |
575
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: