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 Closure; |
21
|
|
|
use Flight\Routing\Exceptions\InvalidControllerException; |
22
|
|
|
|
23
|
|
|
trait CastingTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @internal |
27
|
|
|
* |
28
|
|
|
* @return array<string,mixed> |
29
|
|
|
*/ |
30
|
1 |
|
public function __serialize(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
1 |
|
'name' => $this->name, |
34
|
1 |
|
'path' => $this->path, |
35
|
1 |
|
'host' => $this->domain, |
36
|
1 |
|
'schemes' => $this->schemes, |
37
|
1 |
|
'defaults' => $this->defaults, |
38
|
1 |
|
'patterns' => $this->patterns, |
39
|
1 |
|
'methods' => $this->methods, |
40
|
1 |
|
'middlewares' => $this->middlewares, |
41
|
1 |
|
'arguments' => $this->arguments, |
42
|
1 |
|
'handler' => $this->controller instanceof Closure ? [$this, 'getController'] : $this->controller, |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @internal |
48
|
|
|
* |
49
|
|
|
* @param array<string,mixed> $data |
50
|
|
|
*/ |
51
|
1 |
|
public function __unserialize(array $data): void |
52
|
|
|
{ |
53
|
1 |
|
$this->name = $data['name']; |
|
|
|
|
54
|
1 |
|
$this->path = $data['path']; |
|
|
|
|
55
|
1 |
|
$this->domain = $data['host']; |
|
|
|
|
56
|
1 |
|
$this->defaults = $data['defaults']; |
|
|
|
|
57
|
1 |
|
$this->schemes = $data['schemes']; |
|
|
|
|
58
|
1 |
|
$this->patterns = $data['patterns']; |
|
|
|
|
59
|
1 |
|
$this->methods = $data['methods']; |
|
|
|
|
60
|
1 |
|
$this->controller = $data['handler']; |
|
|
|
|
61
|
1 |
|
$this->middlewares = $data['middlewares']; |
|
|
|
|
62
|
1 |
|
$this->arguments = $data['arguments']; |
|
|
|
|
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
* |
68
|
|
|
* @internal |
69
|
|
|
*/ |
70
|
1 |
|
final public function serialize(): string |
71
|
|
|
{ |
72
|
1 |
|
return \serialize($this->__serialize()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
* |
78
|
|
|
* @internal |
79
|
|
|
*/ |
80
|
1 |
|
final public function unserialize($serialized): void |
81
|
|
|
{ |
82
|
1 |
|
$this->__unserialize(\unserialize($serialized)); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Locates appropriate route by name. Support dynamic route allocation using following pattern: |
87
|
|
|
* Pattern route: `pattern/*<controller@action>` |
88
|
|
|
* Default route: `*<controller@action>` |
89
|
|
|
* Only action: `pattern/*<action>`. |
90
|
|
|
* |
91
|
|
|
* @param string $route |
92
|
|
|
* |
93
|
|
|
* @throws InvalidControllerException |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
151 |
|
private function castRoute(string $route): string |
98
|
|
|
{ |
99
|
|
|
// Match domain + scheme from pattern... |
100
|
151 |
|
if (false !== \preg_match($regex = '@^(?:(https?):)?(//[^/]+)@i', $route)) { |
101
|
151 |
|
$route = $this->castDomain($route, $regex); |
102
|
|
|
} |
103
|
|
|
|
104
|
151 |
|
if (false !== \strpbrk($route, '*') && false !== \preg_match(self::RCA_PATTERN, $route, $matches)) { |
|
|
|
|
105
|
2 |
|
if (!isset($matches['route']) || empty($matches['route'])) { |
106
|
1 |
|
throw new InvalidControllerException("Unable to locate route candidate on `{$route}`"); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
if (isset($matches['controller'], $matches['action'])) { |
110
|
1 |
|
$this->controller = [$matches['controller'] ?: $this->controller, $matches['action']]; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
$route = $matches['route']; |
114
|
|
|
} |
115
|
|
|
|
116
|
150 |
|
return (empty($route) || '/' === $route) ? '/' : $route; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Match scheme and domain from route patterned path |
121
|
|
|
* |
122
|
|
|
* @param string $route |
123
|
|
|
* @param string $regex |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
151 |
|
private function castDomain(string $route, string $regex): string |
128
|
|
|
{ |
129
|
151 |
|
return (string) \preg_replace_callback($regex, function (array $matches): string { |
130
|
9 |
|
$this->setDomain(isset($matches[1]) ? $matches[0] : $matches[2]); |
|
|
|
|
131
|
|
|
|
132
|
9 |
|
return ''; |
133
|
151 |
|
}, $route); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Ensures that the right-most slash is trimmed for prefixes of more than |
138
|
|
|
* one character, and that the prefix begins with a slash. |
139
|
|
|
* |
140
|
|
|
* @param string $uri |
141
|
|
|
* @param string $prefix |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
16 |
|
private function castPrefix(string $uri, string $prefix): string |
146
|
|
|
{ |
147
|
|
|
// Allow homepage uri on prefix just like python django url style. |
148
|
16 |
|
if (\in_array($uri, ['', '/'], true)) { |
149
|
7 |
|
return \rtrim($prefix, '/') . $uri; |
150
|
|
|
} |
151
|
|
|
|
152
|
16 |
|
if (1 === \preg_match('/^([^\|\/|&|-|_|~|@]+)(&|-|_|~|@)/i', $prefix, $matches)) { |
153
|
1 |
|
$newPattern = \rtrim($prefix, $matches[2]) . $matches[2] . $uri; |
154
|
|
|
} |
155
|
|
|
|
156
|
16 |
|
return !empty($newPattern) ? $newPattern : \rtrim($prefix, '/') . '/' . \ltrim($uri, '/'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|