|
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\Exceptions\InvalidControllerException; |
|
21
|
|
|
|
|
22
|
|
|
trait CastingTrait |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
* |
|
27
|
|
|
* @internal |
|
28
|
|
|
*/ |
|
29
|
1 |
|
final public function serialize(): string |
|
30
|
|
|
{ |
|
31
|
1 |
|
return \serialize($this->__serialize()); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
* |
|
37
|
|
|
* @internal |
|
38
|
|
|
*/ |
|
39
|
1 |
|
final public function unserialize($serialized): void |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->__unserialize(\unserialize($serialized)); |
|
|
|
|
|
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Locates appropriate route by name. Support dynamic route allocation using following pattern: |
|
46
|
|
|
* Pattern route: `pattern/*<controller@action>` |
|
47
|
|
|
* Default route: `*<controller@action>` |
|
48
|
|
|
* Only action: `pattern/*<action>`. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $route |
|
51
|
|
|
* |
|
52
|
|
|
* @throws InvalidControllerException |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
128 |
|
private function castRoute(string $route): string |
|
57
|
|
|
{ |
|
58
|
|
|
// Match domain + scheme from pattern... |
|
59
|
128 |
|
if (false !== \preg_match($regex = '@^(?:(https?):)?(//[^/]+)@i', $route)) { |
|
60
|
128 |
|
$route = $this->castDomain($route, $regex); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
128 |
|
if (false !== \strpbrk($route, '*') && false !== \preg_match(self::RCA_PATTERN, $route, $matches)) { |
|
|
|
|
|
|
64
|
2 |
|
if (!isset($matches['route']) || empty($matches['route'])) { |
|
65
|
1 |
|
throw new InvalidControllerException("Unable to locate route candidate on `{$route}`"); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
if (isset($matches['controller'], $matches['action'])) { |
|
69
|
1 |
|
$this->controller = [$matches['controller'] ?: $this->controller, $matches['action']]; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
$route = $matches['route']; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
127 |
|
return (empty($route) || '/' === $route) ? '/' : $route; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Match scheme and domain from route patterned path |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $route |
|
82
|
|
|
* @param string $regex |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
128 |
|
private function castDomain(string $route, string $regex): string |
|
87
|
|
|
{ |
|
88
|
128 |
|
return (string) \preg_replace_callback($regex, function (array $matches): string { |
|
89
|
9 |
|
$this->setDomain(isset($matches[1]) ? $matches[0] : $matches[2]); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
9 |
|
return ''; |
|
92
|
128 |
|
}, $route); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Ensures that the right-most slash is trimmed for prefixes of more than |
|
97
|
|
|
* one character, and that the prefix begins with a slash. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $uri |
|
100
|
|
|
* @param string $prefix |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
5 |
|
private function castPrefix(string $uri, string $prefix): string |
|
105
|
|
|
{ |
|
106
|
|
|
// Allow homepage uri on prefix just like python django url style. |
|
107
|
5 |
|
if (\in_array($uri, ['', '/'], true)) { |
|
108
|
1 |
|
return \rtrim($prefix, '/') . $uri; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
5 |
|
if (1 === \preg_match('/^([^\|\/|&|-|_|~|@]+)(&|-|_|~|@)/i', $prefix, $matches)) { |
|
112
|
1 |
|
$newPattern = \rtrim($prefix, $matches[2]) . $matches[2] . $uri; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
5 |
|
return !empty($newPattern) ? $newPattern : \rtrim($prefix, '/') . '/' . \ltrim($uri, '/'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.