|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.1 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\Traits; |
|
19
|
|
|
|
|
20
|
|
|
use Flight\Routing\Route; |
|
21
|
|
|
|
|
22
|
|
|
trait CastingTrait |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var null|string */ |
|
25
|
|
|
private $name; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $path; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array<string,bool> */ |
|
31
|
67 |
|
private $methods = []; |
|
32
|
|
|
|
|
33
|
67 |
|
/** @var array<string,bool> */ |
|
34
|
|
|
private $domain = []; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array<string,bool> */ |
|
37
|
|
|
private $schemes = []; |
|
38
|
|
|
|
|
39
|
|
|
/** @var array<string,mixed> */ |
|
40
|
|
|
private $defaults = []; |
|
41
|
28 |
|
|
|
42
|
|
|
/** @var array<string,string|string[]> */ |
|
43
|
28 |
|
private $patterns = []; |
|
44
|
28 |
|
|
|
45
|
|
|
/** @var array<int,mixed> */ |
|
46
|
|
|
private $middlewares = []; |
|
47
|
|
|
|
|
48
|
|
|
/** @var mixed */ |
|
49
|
|
|
private $controller; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Locates appropriate route by name. Support dynamic route allocation using following pattern: |
|
53
|
|
|
* Pattern route: `pattern/*<controller@action>` |
|
54
|
|
|
* Default route: `*<controller@action>` |
|
55
|
|
|
* Only action: `pattern/*<action>`. |
|
56
|
|
|
*/ |
|
57
|
|
|
private function castRoute(string $route): string |
|
58
|
151 |
|
{ |
|
59
|
|
|
$urlRegex = \strtr(Route::URL_PATTERN, ['/^' => '/^(?:', '$/u' => ')']); |
|
60
|
151 |
|
$urlRegex .= \str_replace('/^', '?', Route::RCA_PATTERN); |
|
61
|
|
|
|
|
62
|
|
|
// Match url + rca from pattern... |
|
63
|
151 |
|
\preg_match($urlRegex, $route, $matches); |
|
64
|
|
|
|
|
65
|
151 |
|
if (empty($matches)) { |
|
66
|
5 |
|
return $route; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
150 |
|
if (isset($matches['c'], $matches['a'])) { |
|
70
|
5 |
|
$handler = $matches['c'] ?: $this->controller; |
|
71
|
4 |
|
$this->controller = !$handler ? $matches['a'] : [$handler, $matches['a']]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
if (isset($matches['host'])) { |
|
75
|
|
|
$route = $this->castDomain($matches); |
|
76
|
|
|
} |
|
77
|
150 |
|
|
|
78
|
150 |
|
return $route ?: '/'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
149 |
|
/** |
|
82
|
|
|
* Match scheme and domain from route patterned path |
|
83
|
|
|
* |
|
84
|
|
|
* @param array<int|string,null|string> $matches |
|
85
|
|
|
*/ |
|
86
|
|
|
private function castDomain(array $matches): string |
|
87
|
|
|
{ |
|
88
|
|
|
$domain = $matches['host'] ?? ''; |
|
89
|
|
|
$scheme = $matches['scheme'] ?? ''; |
|
90
|
|
|
$route = $matches['route'] ?? ''; |
|
91
|
|
|
|
|
92
|
150 |
|
if ( |
|
93
|
|
|
(empty($route) || '/' === $route || 0 === preg_match('/.\w+$/', $domain)) && |
|
94
|
150 |
|
(!empty($domain) && empty($matches[2])) |
|
95
|
|
|
) { |
|
96
|
150 |
|
$route = $domain . $route; |
|
97
|
4 |
|
$domain = ''; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
150 |
|
if ('api' === $scheme && !empty($domain)) { |
|
101
|
139 |
|
$this->defaults['_api'] = \ucfirst($domain); |
|
102
|
139 |
|
|
|
103
|
|
|
return $route; |
|
104
|
|
|
} elseif (!empty($scheme) && 'api' !== $scheme) { |
|
105
|
150 |
|
$this->schemes[$scheme] = true; |
|
106
|
|
|
} |
|
107
|
150 |
|
|
|
108
|
1 |
|
if (!empty($domain) && 'api' !== $scheme) { |
|
109
|
|
|
$this->domain[$domain] = true; |
|
110
|
|
|
} |
|
111
|
149 |
|
|
|
112
|
|
|
return $route; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Ensures that the right-most slash is trimmed for prefixes of more than |
|
117
|
|
|
* one character, and that the prefix begins with a slash. |
|
118
|
|
|
*/ |
|
119
|
|
|
private function castPrefix(string $uri, string $prefix): string |
|
120
|
|
|
{ |
|
121
|
|
|
// Allow homepage uri on prefix just like python django url style. |
|
122
|
|
|
if (empty($uri) || '/' === $uri) { |
|
123
|
16 |
|
return \rtrim($prefix, '/') . $uri; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
16 |
|
if (1 === \preg_match('/^(.*)(\:|\-|\_|\~|\@)$/', $prefix, $matches)) { |
|
127
|
4 |
|
if ($matches[2] !== $uri[0]) { |
|
128
|
|
|
$uri = $matches[2] . $uri; |
|
129
|
|
|
} |
|
130
|
16 |
|
|
|
131
|
1 |
|
return \rtrim($prefix, $matches[2]) . $uri; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
15 |
|
return \rtrim($prefix, '/') . '/' . \ltrim($uri, '/'); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|