|
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
|
|
|
* Locates appropriate route by name. Support dynamic route allocation using following pattern: |
|
26
|
|
|
* Pattern route: `pattern/*<controller@action>` |
|
27
|
|
|
* Default route: `*<controller@action>` |
|
28
|
|
|
* Only action: `pattern/*<action>`. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $route |
|
31
|
|
|
* |
|
32
|
|
|
* @throws InvalidControllerException |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
150 |
|
private function castRoute(string $route): string |
|
37
|
|
|
{ |
|
38
|
|
|
// Match domain + scheme from pattern... |
|
39
|
150 |
|
if (false !== \preg_match($regex = '@^(?:(https?):)?(//[^/]+)@i', $route)) { |
|
40
|
150 |
|
$route = $this->castDomain($route, $regex); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
150 |
|
if (false !== \strpbrk($route, '*') && false !== \preg_match(self::RCA_PATTERN, $route, $matches)) { |
|
|
|
|
|
|
44
|
5 |
|
if (!isset($matches['route']) || empty($matches['route'])) { |
|
45
|
1 |
|
throw new InvalidControllerException("Unable to locate route candidate on `{$route}`"); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
if (isset($matches['controller'], $matches['action'])) { |
|
49
|
4 |
|
$this->controller = [$matches['controller'] ?: $this->controller, $matches['action']]; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
4 |
|
$route = $matches['route']; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
149 |
|
return (empty($route) || '/' === $route) ? '/' : $route; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Match scheme and domain from route patterned path |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $route |
|
62
|
|
|
* @param string $regex |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
150 |
|
private function castDomain(string $route, string $regex): string |
|
67
|
|
|
{ |
|
68
|
150 |
|
return (string) \preg_replace_callback($regex, function (array $matches): string { |
|
69
|
9 |
|
$this->setDomain(isset($matches[1]) ? $matches[0] : $matches[2]); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
9 |
|
return ''; |
|
72
|
150 |
|
}, $route); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Ensures that the right-most slash is trimmed for prefixes of more than |
|
77
|
|
|
* one character, and that the prefix begins with a slash. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $uri |
|
80
|
|
|
* @param string $prefix |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
15 |
|
private function castPrefix(string $uri, string $prefix): string |
|
85
|
|
|
{ |
|
86
|
|
|
// Allow homepage uri on prefix just like python django url style. |
|
87
|
15 |
|
if (\in_array($uri, ['', '/'], true)) { |
|
88
|
7 |
|
return \rtrim($prefix, '/') . $uri; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
15 |
|
if (1 === \preg_match('/^([^\|\/|&|-|_|~|@]+)(&|-|_|~|@)/i', $prefix, $matches)) { |
|
92
|
1 |
|
$newPattern = \rtrim($prefix, $matches[2]) . $matches[2] . $uri; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
15 |
|
return !empty($newPattern) ? $newPattern : \rtrim($prefix, '/') . '/' . \ltrim($uri, '/'); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|