|
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\MethodNotAllowedException; |
|
21
|
|
|
use Flight\Routing\Exceptions\UriHandlerException; |
|
22
|
|
|
use Flight\Routing\Route; |
|
23
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
24
|
|
|
use Psr\Http\Message\UriInterface; |
|
25
|
|
|
|
|
26
|
|
|
trait ValidationTrait |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Check if given request domain matches given route domain. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string[] $routeDomains |
|
32
|
|
|
* @param array<int|string,string> $parameters |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function compareDomain(array $routeDomains, string $requestDomain, array &$parameters): bool |
|
35
|
48 |
|
{ |
|
36
|
|
|
foreach ($routeDomains as $routeDomain) { |
|
37
|
48 |
|
if (1 === \preg_match($routeDomain, $requestDomain, $parameters)) { |
|
38
|
|
|
return true; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return empty($routeDomains); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Check if given request uri matches given uri method. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array<int|string,string> $parameters |
|
49
|
9 |
|
*/ |
|
50
|
|
|
protected function compareUri(string $routeUri, string $requestUri, array &$parameters): bool |
|
51
|
9 |
|
{ |
|
52
|
9 |
|
return 1 === \preg_match($routeUri, $requestUri, $parameters); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Asserts the Route's method and domain scheme. |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function assertRoute(Route $route, UriInterface $requestUri, string $method): void |
|
59
|
|
|
{ |
|
60
|
|
|
$methods = $route->getMethods(); |
|
61
|
|
|
$schemes = $route->getSchemes(); |
|
62
|
|
|
|
|
63
|
|
|
if (!$this->compareMethod($methods, $method)) { |
|
64
|
9 |
|
throw new MethodNotAllowedException(\array_keys($methods), $requestUri->getPath(), $method); |
|
65
|
|
|
} |
|
66
|
9 |
|
|
|
67
|
|
|
if (!$this->compareScheme($schemes, $requestUri->getScheme())) { |
|
68
|
|
|
throw new UriHandlerException( |
|
69
|
|
|
\sprintf( |
|
70
|
|
|
'Unfortunately current scheme "%s" is not allowed on requested uri [%s]', |
|
71
|
|
|
$requestUri->getScheme(), |
|
72
|
|
|
$requestUri->getPath() |
|
73
|
|
|
), |
|
74
|
|
|
400 |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
46 |
|
} |
|
78
|
|
|
|
|
79
|
46 |
|
/** |
|
80
|
|
|
* Asserts the Route's host and port |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function assertHost(string $hostAndPort, string $requestPath): UriHandlerException |
|
83
|
|
|
{ |
|
84
|
|
|
return new UriHandlerException( |
|
85
|
|
|
\sprintf( |
|
86
|
|
|
'Unfortunately current domain "%s" is not allowed on requested uri [%s]', |
|
87
|
|
|
$hostAndPort, |
|
88
|
|
|
$requestPath |
|
89
|
48 |
|
), |
|
90
|
|
|
400 |
|
91
|
48 |
|
); |
|
92
|
2 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
46 |
|
* Resolve request path to match sub-directory, server, and domain paths. |
|
96
|
1 |
|
*/ |
|
97
|
1 |
|
protected function resolvePath(ServerRequestInterface $request): string |
|
98
|
1 |
|
{ |
|
99
|
1 |
|
$requestPath = $request->getUri()->getPath(); |
|
100
|
1 |
|
$basePath = $request->getServerParams()['SCRIPT_NAME'] ?? ''; |
|
101
|
|
|
|
|
102
|
1 |
|
if ( |
|
103
|
|
|
$basePath !== $requestPath && |
|
104
|
|
|
\strlen($basePath = \dirname($basePath)) > 1 && |
|
105
|
45 |
|
$basePath !== '/index.php' |
|
106
|
|
|
) { |
|
107
|
|
|
$requestPath = \substr($requestPath, \strcmp($basePath, $requestPath)) ?: ''; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return \strlen($requestPath) > 1 ? \rtrim($requestPath, '/') : $requestPath; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Check if given request method matches given route method. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string[] $routeMethods |
|
117
|
|
|
*/ |
|
118
|
|
|
private function compareMethod(array $routeMethods, string $requestMethod): bool |
|
119
|
|
|
{ |
|
120
|
|
|
return isset($routeMethods[$requestMethod]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Check if given request uri scheme matches given route scheme. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string[] $routeSchemes |
|
127
|
|
|
*/ |
|
128
|
|
|
private function compareScheme(array $routeSchemes, string $requestScheme): bool |
|
129
|
|
|
{ |
|
130
|
|
|
return empty($routeSchemes) || isset($routeSchemes[$requestScheme]); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|