1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Flight Routing. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Flight\Routing\Generator; |
19
|
|
|
|
20
|
|
|
use Flight\Routing\Interfaces\RouteGeneratorInterface; |
21
|
|
|
use Psr\Http\Message\UriInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The default compiled routes match for route compiler class. |
25
|
|
|
* |
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class GeneratedRoute implements RouteGeneratorInterface |
29
|
|
|
{ |
30
|
|
|
/** @var array<int,mixed> */ |
31
|
|
|
private array $compiledData; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array<string,array<int|string,array<int,int>|int>> $staticPaths |
35
|
|
|
* @param array<string,array<int,mixed[]>> $variables |
36
|
|
|
*/ |
37
|
12 |
|
public function __construct(array $staticPaths, ?string $dynamicRegex, array $variables) |
38
|
|
|
{ |
39
|
12 |
|
$this->compiledData = [$staticPaths, $dynamicRegex, $variables]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @internal |
44
|
|
|
*/ |
45
|
11 |
|
public function __serialize(): array |
46
|
|
|
{ |
47
|
11 |
|
return $this->compiledData; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @internal |
52
|
|
|
* |
53
|
|
|
* @param array<int,mixed> $data |
54
|
|
|
*/ |
55
|
11 |
|
public function __unserialize(array $data): void |
56
|
|
|
{ |
57
|
11 |
|
$this->compiledData = $data; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
7 |
|
public function getData(): array |
64
|
|
|
{ |
65
|
7 |
|
return $this->compiledData; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
10 |
|
public function match(string $method, UriInterface $uri, callable $routes) |
72
|
|
|
{ |
73
|
10 |
|
[$staticRoutes, $regexList, $variables] = $this->compiledData; |
74
|
10 |
|
$requestPath = $uri->getPath(); |
75
|
10 |
|
$matches = []; |
76
|
|
|
|
77
|
10 |
|
if (empty($matchedIds = $staticRoutes[$requestPath] ?? [])) { |
78
|
7 |
|
if (null === $regexList || !\preg_match($regexList, $requestPath, $matches, \PREG_UNMATCHED_AS_NULL)) { |
79
|
3 |
|
if (isset($staticRoutes['*'][$requestPath])) { |
80
|
2 |
|
$matchedIds = $staticRoutes['*'][$requestPath]; |
81
|
2 |
|
goto found_a_route_match; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
$matchedIds = [(int) $matches['MARK']]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
found_a_route_match: |
91
|
10 |
|
foreach ($matchedIds as $matchedId) { |
92
|
10 |
|
foreach ($variables[$method][$matchedId] ?? [] as $domain => $routeVar) { |
93
|
9 |
|
[$matchedRoute, $hostsVar] = $routes($matchedId, $domain ?: null, $uri); |
94
|
9 |
|
$requiredSchemes = $matchedRoute->getSchemes(); |
95
|
|
|
|
96
|
9 |
|
if (null === $hostsVar) { |
97
|
3 |
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
7 |
|
if ($requiredSchemes && !\in_array($uri->getScheme(), $requiredSchemes, true)) { |
101
|
1 |
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
7 |
|
if (!empty($routeVar)) { |
105
|
7 |
|
$matchInt = 0; |
106
|
|
|
|
107
|
7 |
|
foreach ($routeVar as $key => $value) { |
108
|
7 |
|
$matchedRoute->argument($key, $matches[++$matchInt] ?? $matches[$key] ?? $hostsVar[$key] ?? $value); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
7 |
|
return $matchedRoute; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
return $matchedIds; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|