-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Route.php
643 lines (505 loc) · 15.3 KB
/
Route.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Routing;
use Nette;
use Nette\Utils\Strings;
use function array_key_exists, is_array, count, strlen;
/**
* The bidirectional route is responsible for mapping
* HTTP request to an array for dispatch and vice-versa.
*/
class Route implements Router
{
/** key used in metadata */
public const
Value = 'value',
Pattern = 'pattern',
FilterIn = 'filterIn',
FilterOut = 'filterOut',
FilterTable = 'filterTable',
FilterStrict = 'filterStrict';
/** @deprecated use Route::Value */
public const VALUE = self::Value;
/** @deprecated use Route::Pattern */
public const PATTERN = self::Pattern;
/** @deprecated use Route::FilterIn */
public const FILTER_IN = self::FilterIn;
/** @deprecated use Route::FilterOut */
public const FILTER_OUT = self::FilterOut;
/** @deprecated use Route::FilterTable */
public const FILTER_TABLE = self::FilterTable;
/** @deprecated use Route::FilterStrict */
public const FILTER_STRICT = self::FilterStrict;
/** key used in metadata */
private const
Default = 'defOut',
Fixity = 'fixity',
FilterTableOut = 'filterTO';
/** url type */
private const
Host = 1,
Path = 2,
Relative = 3;
/** fixity types - has default value and is: */
private const
InQuery = 0,
InPath = 1, // in brackets is default value = null
Constant = 2;
protected array $defaultMeta = [
'#' => [ // default style for path parameters
self::Pattern => '[^/]+',
self::FilterOut => [self::class, 'param2path'],
],
];
private string $mask;
private array $sequence;
/** regular expression pattern */
private string $re;
/** @var string[] parameter aliases in regular expression */
private array $aliases = [];
/** @var array of [value & fixity, filterIn, filterOut] */
private array $metadata = [];
private array $xlat = [];
/** Host, Path, Relative */
private int $type;
/** http | https */
private string $scheme = '';
/**
* @param string $mask e.g. '<presenter>/<action>/<id \d{1,3}>'
*/
public function __construct(string $mask, array $metadata = [])
{
$this->mask = $mask;
$this->metadata = $this->normalizeMetadata($metadata);
$this->parseMask($this->detectMaskType());
}
/**
* Returns mask.
*/
public function getMask(): string
{
return $this->mask;
}
/** @internal */
protected function getMetadata(): array
{
return $this->metadata;
}
/**
* Returns default values.
*/
public function getDefaults(): array
{
$defaults = [];
foreach ($this->metadata as $name => $meta) {
if (isset($meta[self::Fixity])) {
$defaults[$name] = $meta[self::Value];
}
}
return $defaults;
}
/** @internal */
public function getConstantParameters(): array
{
$res = [];
foreach ($this->metadata as $name => $meta) {
if (isset($meta[self::Fixity]) && $meta[self::Fixity] === self::Constant) {
$res[$name] = $meta[self::Value];
}
}
return $res;
}
/**
* Maps HTTP request to an array.
*/
public function match(Nette\Http\IRequest $httpRequest): ?array
{
// combine with precedence: mask (params in URL-path), fixity, query, (post,) defaults
// 1) URL MASK
$url = $httpRequest->getUrl();
$re = $this->re;
if ($this->type === self::Host) {
$host = $url->getHost();
$path = '//' . $host . $url->getPath();
$parts = ip2long($host)
? [$host]
: array_reverse(explode('.', $host));
$re = strtr($re, [
'/%basePath%/' => preg_quote($url->getBasePath(), '#'),
'%tld%' => preg_quote($parts[0], '#'),
'%domain%' => preg_quote(isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0], '#'),
'%sld%' => preg_quote($parts[1] ?? '', '#'),
'%host%' => preg_quote($host, '#'),
]);
} elseif ($this->type === self::Relative) {
$basePath = $url->getBasePath();
if (strncmp($url->getPath(), $basePath, strlen($basePath)) !== 0) {
return null;
}
$path = substr($url->getPath(), strlen($basePath));
} else {
$path = $url->getPath();
}
$path = rawurldecode($path);
if ($path !== '' && $path[-1] !== '/') {
$path .= '/';
}
if (!$matches = Strings::match($path, $re)) {
return null; // stop, not matched
}
// assigns matched values to parameters
$params = [];
foreach ($matches as $k => $v) {
if (is_string($k) && $v !== '') {
$params[$this->aliases[$k]] = $v;
}
}
// 2) CONSTANT FIXITY
foreach ($this->metadata as $name => $meta) {
if (!isset($params[$name]) && isset($meta[self::Fixity]) && $meta[self::Fixity] !== self::InQuery) {
$params[$name] = null; // cannot be overwriten in 3) and detected by isset() in 4)
}
}
// 3) QUERY
$params += self::renameKeys($httpRequest->getQuery(), array_flip($this->xlat));
// 4) APPLY FILTERS & FIXITY
foreach ($this->metadata as $name => $meta) {
if (isset($params[$name])) {
if (!is_scalar($params[$name])) {
// do nothing
} elseif (isset($meta[self::FilterTable][$params[$name]])) { // applies filterTable only to scalar parameters
$params[$name] = $meta[self::FilterTable][$params[$name]];
} elseif (isset($meta[self::FilterTable]) && !empty($meta[self::FilterStrict])) {
return null; // rejected by filterTable
} elseif (isset($meta[self::FilterIn])) { // applies filterIn only to scalar parameters
$params[$name] = $meta[self::FilterIn]((string) $params[$name]);
if ($params[$name] === null && !isset($meta[self::Fixity])) {
return null; // rejected by filter
}
}
} elseif (isset($meta[self::Fixity])) {
$params[$name] = $meta[self::Value];
}
}
if (isset($this->metadata[null][self::FilterIn])) {
$params = $this->metadata[null][self::FilterIn]($params);
if ($params === null) {
return null;
}
}
return $params;
}
/**
* Constructs absolute URL from array.
*/
public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string
{
if (!$this->preprocessParams($params)) {
return null;
}
$url = $this->compileUrl($params);
if ($url === null) {
return null;
}
// absolutize
if ($this->type === self::Relative) {
$url = (($tmp = $refUrl->getAuthority()) ? "//$tmp" : '') . $refUrl->getBasePath() . $url;
} elseif ($this->type === self::Path) {
$url = (($tmp = $refUrl->getAuthority()) ? "//$tmp" : '') . $url;
} else {
$host = $refUrl->getHost();
$parts = ip2long($host)
? [$host]
: array_reverse(explode('.', $host));
$port = $refUrl->getDefaultPort() === ($tmp = $refUrl->getPort()) ? '' : ':' . $tmp;
$url = strtr($url, [
'/%basePath%/' => $refUrl->getBasePath(),
'%tld%' => $parts[0] . $port,
'%domain%' => (isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0]) . $port,
'%sld%' => $parts[1] ?? '',
'%host%' => $host . $port,
]);
}
$url = ($this->scheme ?: $refUrl->getScheme()) . ':' . $url;
// build query string
$params = self::renameKeys($params, $this->xlat);
$sep = ini_get('arg_separator.input');
$query = http_build_query($params, '', $sep ? $sep[0] : '&');
if ($query !== '') {
$url .= '?' . $query;
}
return $url;
}
private function preprocessParams(array &$params): bool
{
$filter = $this->metadata[null][self::FilterOut] ?? null;
if ($filter) {
$params = $filter($params);
if ($params === null) {
return false; // rejected by global filter
}
}
foreach ($this->metadata as $name => $meta) {
$fixity = $meta[self::Fixity] ?? null;
if (!isset($params[$name])) {
if ($fixity === self::Constant) {
if ($meta[self::Value] === null) {
continue;
}
return false; // wrong parameter value
}
continue; // retains null values
}
if (is_scalar($params[$name])) {
$params[$name] = $params[$name] === false
? '0'
: (string) $params[$name];
}
if ($fixity !== null) {
if ($params[$name] == $meta[self::Value]) { // default value may be object, intentionally ==
// remove default values; null values are retain
unset($params[$name]);
continue;
} elseif ($fixity === self::Constant) {
return false; // wrong parameter value
}
}
if (is_scalar($params[$name]) && isset($meta[self::FilterTableOut][$params[$name]])) {
$params[$name] = $meta[self::FilterTableOut][$params[$name]];
} elseif (isset($meta[self::FilterTableOut]) && !empty($meta[self::FilterStrict])) {
return false;
} elseif (isset($meta[self::FilterOut])) {
$params[$name] = $meta[self::FilterOut]($params[$name]);
}
if (
isset($meta[self::Pattern])
&& !preg_match("#(?:{$meta[self::Pattern]})$#DA", rawurldecode((string) $params[$name]))
) {
return false; // pattern not match
}
}
return true;
}
private function compileUrl(array &$params): ?string
{
$brackets = [];
$required = null; // null for auto-optional
$path = '';
$i = count($this->sequence) - 1;
do {
$path = $this->sequence[$i] . $path;
if ($i === 0) {
return $path;
}
$i--;
$name = $this->sequence[$i--]; // parameter name
if ($name === ']') { // opening optional part
$brackets[] = $path;
} elseif ($name[0] === '[') { // closing optional part
$tmp = array_pop($brackets);
if ($required < count($brackets) + 1) { // is this level optional?
if ($name !== '[!') { // and not "required"-optional
$path = $tmp;
}
} else {
$required = count($brackets);
}
} elseif ($name[0] === '?') { // "foo" parameter
continue;
} elseif (isset($params[$name]) && $params[$name] !== '') {
$required = count($brackets); // make this level required
$path = $params[$name] . $path;
unset($params[$name]);
} elseif (isset($this->metadata[$name][self::Fixity])) { // has default value?
$path = $required === null && !$brackets // auto-optional
? ''
: $this->metadata[$name][self::Default] . $path;
} else {
return null; // missing parameter '$name'
}
} while (true);
}
private function detectMaskType(): string
{
// '//host/path' vs. '/abs. path' vs. 'relative path'
if (preg_match('#(?:(https?):)?(//.*)#A', $this->mask, $m)) {
$this->type = self::Host;
[, $this->scheme, $path] = $m;
return $path;
} elseif (str_starts_with($this->mask, '/')) {
$this->type = self::Path;
} else {
$this->type = self::Relative;
}
return $this->mask;
}
private function normalizeMetadata(array $metadata): array
{
foreach ($metadata as $name => $meta) {
if (!is_array($meta)) {
$metadata[$name] = $meta = [self::Value => $meta];
}
if (array_key_exists(self::Value, $meta)) {
if (is_scalar($meta[self::Value])) {
$metadata[$name][self::Value] = $meta[self::Value] === false
? '0'
: (string) $meta[self::Value];
}
$metadata[$name]['fixity'] = self::Constant;
}
}
return $metadata;
}
private function parseMask(string $path): void
{
// <parameter-name[=default] [pattern]> or [ or ] or ?...
$parts = Strings::split($path, '/<([^<>= ]+)(=[^<> ]*)? *([^<>]*)>|(\[!?|\]|\s*\?.*)/');
$i = count($parts) - 1;
if ($i === 0) {
$this->re = '#' . preg_quote($parts[0], '#') . '/?$#DA';
$this->sequence = [$parts[0]];
return;
}
if ($this->parseQuery($parts)) {
$i -= 5;
}
$brackets = 0; // optional level
$re = '';
$sequence = [];
$autoOptional = true;
do {
$part = $parts[$i]; // part of path
if (strpbrk($part, '<>') !== false) {
throw new Nette\InvalidArgumentException("Unexpected '$part' in mask '$this->mask'.");
}
array_unshift($sequence, $part);
$re = preg_quote($part, '#') . $re;
if ($i === 0) {
break;
}
$i--;
$part = $parts[$i]; // [ or ]
if ($part === '[' || $part === ']' || $part === '[!') {
$brackets += $part[0] === '[' ? -1 : 1;
if ($brackets < 0) {
throw new Nette\InvalidArgumentException("Unexpected '$part' in mask '$this->mask'.");
}
array_unshift($sequence, $part);
$re = ($part[0] === '[' ? '(?:' : ')?') . $re;
$i -= 4;
continue;
}
$pattern = trim($parts[$i--]); // validation condition (as regexp)
$default = $parts[$i--]; // default value
$name = $parts[$i--]; // parameter name
array_unshift($sequence, $name);
if ($name[0] === '?') { // "foo" parameter
$name = substr($name, 1);
$re = $pattern
? '(?:' . preg_quote($name, '#') . "|$pattern)$re"
: preg_quote($name, '#') . $re;
$sequence[1] = $name . $sequence[1];
continue;
}
// pattern, condition & metadata
$meta = ($this->metadata[$name] ?? []) + ($this->defaultMeta[$name] ?? $this->defaultMeta['#']);
if ($pattern === '' && isset($meta[self::Pattern])) {
$pattern = $meta[self::Pattern];
}
if ($default !== '') {
$meta[self::Value] = substr($default, 1);
$meta[self::Fixity] = self::InPath;
}
$meta[self::FilterTableOut] = empty($meta[self::FilterTable])
? null
: array_flip($meta[self::FilterTable]);
if (array_key_exists(self::Value, $meta)) {
if (isset($meta[self::FilterTableOut][$meta[self::Value]])) {
$meta[self::Default] = $meta[self::FilterTableOut][$meta[self::Value]];
} elseif (isset($meta[self::Value], $meta[self::FilterOut])) {
$meta[self::Default] = $meta[self::FilterOut]($meta[self::Value]);
} else {
$meta[self::Default] = $meta[self::Value];
}
}
$meta[self::Pattern] = $pattern;
// include in expression
$this->aliases['p' . $i] = $name;
$re = '(?P<p' . $i . '>(?U)' . $pattern . ')' . $re;
if ($brackets) { // is in brackets?
if (!isset($meta[self::Value])) {
$meta[self::Value] = $meta[self::Default] = null;
}
$meta[self::Fixity] = self::InPath;
} elseif (isset($meta[self::Fixity])) {
if ($autoOptional) {
$re = '(?:' . $re . ')?';
}
$meta[self::Fixity] = self::InPath;
} else {
$autoOptional = false;
}
$this->metadata[$name] = $meta;
} while (true);
if ($brackets) {
throw new Nette\InvalidArgumentException("Missing '[' in mask '$this->mask'.");
}
$this->re = '#' . $re . '/?$#DA';
$this->sequence = $sequence;
}
private function parseQuery(array $parts): bool
{
$query = $parts[count($parts) - 2] ?? '';
if (!str_starts_with(ltrim($query), '?')) {
return false;
}
// name=<parameter-name [pattern]>
$matches = Strings::matchAll($query, '/(?:([a-zA-Z0-9_.-]+)=)?<([^> ]+) *([^>]*)>/');
foreach ($matches as [, $param, $name, $pattern]) { // $pattern is not used
$meta = ($this->metadata[$name] ?? []) + ($this->defaultMeta['?' . $name] ?? []);
if (array_key_exists(self::Value, $meta)) {
$meta[self::Fixity] = self::InQuery;
}
unset($meta[self::Pattern]);
$meta[self::FilterTableOut] = empty($meta[self::FilterTable])
? null
: array_flip($meta[self::FilterTable]);
$this->metadata[$name] = $meta;
if ($param !== '') {
$this->xlat[$name] = $param;
}
}
return true;
}
/********************* Utilities ****************d*g**/
/**
* Rename keys in array.
*/
private static function renameKeys(array $arr, array $xlat): array
{
if (!$xlat) {
return $arr;
}
$res = [];
$occupied = array_flip($xlat);
foreach ($arr as $k => $v) {
if (isset($xlat[$k])) {
$res[$xlat[$k]] = $v;
} elseif (!isset($occupied[$k])) {
$res[$k] = $v;
}
}
return $res;
}
/**
* Url encode.
*/
public static function param2path(string $s): string
{
return str_replace('%2F', '/', rawurlencode($s));
}
}