1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Aura for PHP. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Aura\Router; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* An individual route with a name, path, attributes, defaults, etc. |
14
|
|
|
* |
15
|
|
|
* @package Aura.Router |
16
|
|
|
* |
17
|
|
|
* @property-read string $name The route name. |
18
|
|
|
* |
19
|
|
|
* @property-read string $path The route path. |
20
|
|
|
* |
21
|
|
|
* @property-read string $namePrefix |
22
|
|
|
* |
23
|
|
|
* @property-read string $pathPrefix |
24
|
|
|
* |
25
|
|
|
* @property-read string $host |
26
|
|
|
* |
27
|
|
|
* @property-read array $defaults Default values for attributes. |
28
|
|
|
* |
29
|
|
|
* @property-read array $attributes Attribute values added by the rules. |
30
|
|
|
* |
31
|
|
|
* @property-read array $tokens Plceholder token names and regexes. |
32
|
|
|
* |
33
|
|
|
* @property-read string $wildcard The name of the wildcard token. |
34
|
|
|
* |
35
|
|
|
* @property-read array $accept |
36
|
|
|
* |
37
|
|
|
* @property-read array $extras |
38
|
|
|
* |
39
|
|
|
* @property-read bool $secure |
40
|
|
|
* |
41
|
|
|
* @property-read array $allows |
42
|
|
|
* |
43
|
|
|
* @property-read bool $routable |
44
|
|
|
* |
45
|
|
|
* @property-read string $failedRule |
46
|
|
|
* |
47
|
|
|
* @property-read mixed $handler |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
class Route |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* Accepts these content types. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
* |
58
|
|
|
*/ |
59
|
|
|
protected $accepts = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* Allows these HTTP methods. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
protected $allows = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* Attribute values added by the rules. |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
protected $attributes = []; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* Authentication/authorization values. |
82
|
|
|
* |
83
|
|
|
* @var mixed |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
|
|
protected $auth; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
* Default attribute values. |
91
|
|
|
* |
92
|
|
|
* @var array |
93
|
|
|
* |
94
|
|
|
*/ |
95
|
|
|
protected $defaults = []; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* Extra key-value pairs to attach to the route; intended for use by |
100
|
|
|
* custom matching rules. |
101
|
|
|
* |
102
|
|
|
* @var array |
103
|
|
|
* |
104
|
|
|
*/ |
105
|
|
|
protected $extras = []; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* The rule that failed, if any, during matching. |
110
|
|
|
* |
111
|
|
|
* @var string |
112
|
|
|
* |
113
|
|
|
*/ |
114
|
|
|
protected $failedRule; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
* The action, controller, callable, closure, etc. this route points to. |
119
|
|
|
* |
120
|
|
|
* @var mixed |
121
|
|
|
* |
122
|
|
|
*/ |
123
|
|
|
protected $handler; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* The host string this route responds to. |
128
|
|
|
* |
129
|
|
|
* @var string |
130
|
|
|
* |
131
|
|
|
*/ |
132
|
|
|
protected $host; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* |
136
|
|
|
* The name for this route. |
137
|
|
|
* |
138
|
|
|
* @var string |
139
|
|
|
* |
140
|
|
|
*/ |
141
|
|
|
protected $name; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* |
145
|
|
|
* Prefix the route name with this string. |
146
|
|
|
* |
147
|
|
|
* @var string |
148
|
|
|
* |
149
|
|
|
*/ |
150
|
|
|
protected $namePrefix; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* The path for this route. |
155
|
|
|
* |
156
|
|
|
* @var string |
157
|
|
|
* |
158
|
|
|
*/ |
159
|
|
|
protected $path; |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* |
163
|
|
|
* Prefix the route path with this string. |
164
|
|
|
* |
165
|
|
|
* @var string |
166
|
|
|
* |
167
|
|
|
*/ |
168
|
|
|
protected $pathPrefix; |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* |
172
|
|
|
* Should this route be used for matching? |
173
|
|
|
* |
174
|
|
|
* @var bool |
175
|
|
|
* |
176
|
|
|
*/ |
177
|
|
|
protected $isRoutable = true; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* |
181
|
|
|
* Should this route respond on a secure protocol? |
182
|
|
|
* |
183
|
|
|
* @var bool |
184
|
|
|
* |
185
|
|
|
*/ |
186
|
|
|
protected $secure = null; |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* |
190
|
|
|
* Placeholder token names and regexes. |
191
|
|
|
* |
192
|
|
|
* @var array |
193
|
|
|
* |
194
|
|
|
*/ |
195
|
|
|
protected $tokens = []; |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* |
199
|
|
|
* Wildcard token name, if any. |
200
|
|
|
* |
201
|
|
|
* @var string |
202
|
|
|
* |
203
|
|
|
*/ |
204
|
|
|
protected $wildcard = null; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* |
208
|
|
|
* When cloning the Route, reset the `$attributes` to an empty array, and |
209
|
|
|
* clear the `$failedRule`. |
210
|
|
|
* |
211
|
|
|
*/ |
212
|
30 |
|
public function __clone() |
213
|
|
|
{ |
214
|
|
|
// $this is the cloned instance, not the original |
215
|
30 |
|
$this->attributes = $this->defaults; |
216
|
30 |
|
$this->failedRule = null; |
217
|
30 |
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* |
221
|
|
|
* Magic read-only for all properties. |
222
|
|
|
* |
223
|
|
|
* @param string $key The property to read from. |
224
|
|
|
* |
225
|
|
|
* @return mixed |
226
|
|
|
* |
227
|
|
|
*/ |
228
|
39 |
|
public function __get($key) |
229
|
|
|
{ |
230
|
39 |
|
return $this->$key; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* |
235
|
|
|
* Merges with the existing content types. |
236
|
|
|
* |
237
|
|
|
* @param string|array $accepts The content types. |
238
|
|
|
* |
239
|
|
|
* @return $this |
240
|
|
|
* |
241
|
|
|
*/ |
242
|
1 |
|
public function accepts($accepts) |
243
|
|
|
{ |
244
|
1 |
|
$this->accepts = array_merge($this->accepts, (array) $accepts); |
245
|
1 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* |
250
|
|
|
* Merges with the existing allowed methods. |
251
|
|
|
* |
252
|
|
|
* @param string|array $allows The allowed HTTP methods. |
253
|
|
|
* |
254
|
|
|
* @return $this |
255
|
|
|
* |
256
|
|
|
*/ |
257
|
5 |
|
public function allows($allows) |
258
|
|
|
{ |
259
|
5 |
|
$this->allows = array_merge($this->allows, (array) $allows); |
260
|
5 |
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* |
265
|
|
|
* Merges with the existing attributes. |
266
|
|
|
* |
267
|
|
|
* @param array $attributes The attributes to add. |
268
|
|
|
* |
269
|
|
|
* @return $this |
270
|
|
|
* |
271
|
|
|
*/ |
272
|
17 |
|
public function attributes(array $attributes) |
273
|
|
|
{ |
274
|
17 |
|
$this->attributes = array_merge($this->attributes, $attributes); |
275
|
17 |
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* |
280
|
|
|
* Sets the auth value. |
281
|
|
|
* |
282
|
|
|
* @param mixed $auth The auth value to set. |
283
|
|
|
* |
284
|
|
|
* @return $this |
285
|
|
|
* |
286
|
|
|
*/ |
287
|
1 |
|
public function auth($auth) |
288
|
|
|
{ |
289
|
1 |
|
$this->auth = $auth; |
290
|
1 |
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* |
295
|
|
|
* Merges with the existing default values for attributes. |
296
|
|
|
* |
297
|
|
|
* @param array $defaults Default values for attributes. |
298
|
|
|
* |
299
|
|
|
* @return $this |
300
|
|
|
* |
301
|
|
|
*/ |
302
|
3 |
|
public function defaults(array $defaults) |
303
|
|
|
{ |
304
|
3 |
|
$this->defaults = array_merge($this->defaults, $defaults); |
305
|
3 |
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* |
310
|
|
|
* Merges with the existing extra key-value pairs; this merge is recursive, |
311
|
|
|
* so the values can be arbitrarily deep. |
312
|
|
|
* |
313
|
|
|
* @param array $extras The extra key-value pairs. |
314
|
|
|
* |
315
|
|
|
* @return $this |
316
|
|
|
* |
317
|
|
|
*/ |
318
|
1 |
|
public function extras(array $extras) |
319
|
|
|
{ |
320
|
1 |
|
$this->extras = array_merge_recursive($this->extras, $extras); |
321
|
1 |
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* |
326
|
|
|
* Sets the failed rule. |
327
|
|
|
* |
328
|
|
|
* @param mixed $failedRule The failed rule. |
329
|
|
|
* |
330
|
|
|
* @return $this |
331
|
|
|
* |
332
|
|
|
*/ |
333
|
4 |
|
public function failedRule($failedRule) |
334
|
|
|
{ |
335
|
4 |
|
$this->failedRule = $failedRule; |
336
|
4 |
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* |
341
|
|
|
* The route leads to this handler. |
342
|
|
|
* |
343
|
|
|
* @param mixed $handler The handler for this route; if null, uses the |
344
|
|
|
* route name. |
345
|
|
|
* |
346
|
|
|
* @return $this |
347
|
|
|
* |
348
|
|
|
*/ |
349
|
21 |
|
public function handler($handler) |
350
|
|
|
{ |
351
|
21 |
|
if ($handler === null) { |
352
|
19 |
|
$handler = $this->name; |
353
|
19 |
|
} |
354
|
21 |
|
$this->handler = $handler; |
355
|
21 |
|
return $this; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* |
360
|
|
|
* Sets the host. |
361
|
|
|
* |
362
|
|
|
* @param mixed $host The host. |
363
|
|
|
* |
364
|
|
|
* @return $this |
365
|
|
|
* |
366
|
|
|
*/ |
367
|
5 |
|
public function host($host) |
368
|
|
|
{ |
369
|
5 |
|
$this->host = $host; |
370
|
5 |
|
return $this; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* |
375
|
|
|
* Sets whether or not this route should be used for matching. |
376
|
|
|
* |
377
|
|
|
* @param bool $isRoutable If true, this route can be matched; if not, it |
378
|
|
|
* can be used only to generate a path. |
379
|
|
|
* |
380
|
|
|
* @return $this |
381
|
|
|
* |
382
|
|
|
*/ |
383
|
4 |
|
public function isRoutable($isRoutable = true) |
384
|
|
|
{ |
385
|
4 |
|
$this->isRoutable = (bool) $isRoutable; |
386
|
4 |
|
return $this; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* |
391
|
|
|
* Sets the route name; immutable once set. |
392
|
|
|
* |
393
|
|
|
* @param string $name The route name. |
394
|
|
|
* |
395
|
|
|
* @return $this |
396
|
|
|
* |
397
|
|
|
* @throws Exception\ImmutableProperty when the name has already been set. |
398
|
|
|
* |
399
|
|
|
*/ |
400
|
23 |
|
public function name($name) |
401
|
|
|
{ |
402
|
23 |
|
if ($this->name !== null) { |
403
|
1 |
|
$message = __CLASS__ . '::$name is immutable once set'; |
404
|
1 |
|
throw new Exception\ImmutableProperty($message); |
405
|
|
|
} |
406
|
23 |
|
$this->name = $this->namePrefix . $name; |
407
|
23 |
|
return $this; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* |
412
|
|
|
* Appends to the existing name prefix; immutable once $name is set. |
413
|
|
|
* |
414
|
|
|
* @param string $namePrefix The name prefix to append. |
415
|
|
|
* |
416
|
|
|
* @return $this |
417
|
|
|
* |
418
|
|
|
* @throws Exception\ImmutableProperty when the name has already been set. |
419
|
|
|
* |
420
|
|
|
*/ |
421
|
5 |
|
public function namePrefix($namePrefix) |
422
|
|
|
{ |
423
|
5 |
|
if ($this->name !== null) { |
424
|
1 |
|
$message = __CLASS__ . '::$namePrefix is immutable once $name is set'; |
425
|
1 |
|
throw new Exception\ImmutableProperty($message); |
426
|
|
|
} |
427
|
4 |
|
$this->namePrefix = $namePrefix; |
428
|
4 |
|
return $this; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* |
433
|
|
|
* Sets the route path; immutable once set. |
434
|
|
|
* |
435
|
|
|
* @param string $path The route path. |
436
|
|
|
* |
437
|
|
|
* @return $this |
438
|
|
|
* |
439
|
|
|
* @throws Exception\ImmutableProperty when the name has already been set. |
440
|
|
|
* |
441
|
|
|
*/ |
442
|
40 |
|
public function path($path) |
443
|
|
|
{ |
444
|
40 |
|
if ($this->path !== null) { |
445
|
1 |
|
$message = __CLASS__ . '::$path is immutable once set'; |
446
|
1 |
|
throw new Exception\ImmutableProperty($message); |
447
|
|
|
} |
448
|
40 |
|
$this->path = $this->pathPrefix . $path; |
449
|
40 |
|
return $this; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* |
454
|
|
|
* Appends to the existing path prefix; immutable once $path is set. |
455
|
|
|
* |
456
|
|
|
* @param string $pathPrefix The path prefix to append. |
457
|
|
|
* |
458
|
|
|
* @return $this |
459
|
|
|
* |
460
|
|
|
* @throws Exception\ImmutableProperty when the path has already been set. |
461
|
|
|
* |
462
|
|
|
*/ |
463
|
5 |
|
public function pathPrefix($pathPrefix) |
464
|
|
|
{ |
465
|
5 |
|
if ($this->path !== null) { |
466
|
1 |
|
$message = __CLASS__ . '::$pathPrefix is immutable once $path is set'; |
467
|
1 |
|
throw new Exception\ImmutableProperty($message); |
468
|
|
|
} |
469
|
4 |
|
$this->pathPrefix = $pathPrefix; |
470
|
4 |
|
return $this; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* |
475
|
|
|
* Sets whether or not the route must be secure. |
476
|
|
|
* |
477
|
|
|
* @param bool|null $secure If true, the server must indicate an HTTPS request; |
478
|
|
|
* if false, it must *not* be HTTPS; if null, it doesn't matter. |
479
|
|
|
* |
480
|
|
|
* @return $this |
481
|
|
|
* |
482
|
|
|
*/ |
483
|
5 |
|
public function secure($secure = true) |
484
|
|
|
{ |
485
|
5 |
|
$this->secure = ($secure === null) ? null : (bool) $secure; |
486
|
5 |
|
return $this; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* |
491
|
|
|
* Merges with the existing tokens. |
492
|
|
|
* |
493
|
|
|
* @param array $tokens The tokens. |
494
|
|
|
* |
495
|
|
|
* @return $this |
496
|
|
|
* |
497
|
|
|
*/ |
498
|
10 |
|
public function tokens(array $tokens) |
499
|
|
|
{ |
500
|
10 |
|
$this->tokens = array_merge($this->tokens, $tokens); |
501
|
10 |
|
return $this; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* |
506
|
|
|
* Sets the name of the wildcard token, if any. |
507
|
|
|
* |
508
|
|
|
* @param string $wildcard The name of the wildcard token, if any. |
509
|
|
|
* |
510
|
|
|
* @return $this |
511
|
|
|
* |
512
|
|
|
*/ |
513
|
3 |
|
public function wildcard($wildcard) |
514
|
|
|
{ |
515
|
3 |
|
$this->wildcard = $wildcard; |
516
|
3 |
|
return $this; |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
|