-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteMatcher.php
More file actions
210 lines (172 loc) · 6.14 KB
/
RouteMatcher.php
File metadata and controls
210 lines (172 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/*
*
* (c) Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Yarhon\RouteGuardBundle\Security\Http;
use Symfony\Component\Routing\Route;
/**
* RouteMatcher checks if Route would always/possibly/never match a RequestConstraint.
*
* @author Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*/
class RouteMatcher
{
/**
* @var string
*/
private $defaultHost;
/**
* @var RegexParser
*/
private $regexParser;
/**
* @param string|null $defaultHost
* @param RegexParser|null $regexParser
*/
public function __construct($defaultHost = null, RegexParser $regexParser = null)
{
$this->defaultHost = $defaultHost;
$this->regexParser = $regexParser ?: new RegexParser();
}
/**
* @param Route $route
* @param RequestConstraint $constraint
*
* @return bool|RequestConstraint Boolean true/false if route would always/never match RequestConstraint
* A fresh RequestConstraint instance if route would possibly match RequestConstraint
*/
public function matches(Route $route, RequestConstraint $constraint)
{
// Note: order of parameters should be equal to the order of RequestConstraint constructor arguments.
$originalParameters = [
'pathPattern' => $constraint->getPathPattern(),
'hostPattern' => $constraint->getHostPattern(),
'methods' => $constraint->getMethods(),
'ips' => $constraint->getIps(),
];
// All parameters equal to false (like nulls, empty strings and arrays) would be filtered out.
$parameters = array_filter($originalParameters);
if (0 === count($parameters)) {
// If all parameters are empty, route would always match
return true;
}
$matchResults = [];
foreach ($parameters as $parameter => $value) {
$matcher = 'match'.ucfirst($parameter);
$matchResults[$parameter] = method_exists($this, $matcher) ? $this->$matcher($route, $value) : 0;
}
if (in_array(-1, $matchResults, true)) {
// One of the parameters would never match
return false;
}
if ([1] === array_unique(array_values($matchResults))) {
// All parameters would always match
return true;
}
$parameters = $originalParameters;
// Set always matching parameters to null to avoid theirs further unnecessary matching.
foreach (array_keys($matchResults, 1, true) as $parameter) {
$parameters[$parameter] = null;
}
return new RequestConstraint(...array_values($parameters));
}
/**
* @param Route $route
* @param string $pattern
*
* @return int
*/
private function matchPathPattern(Route $route, $pattern)
{
$compiledRoute = $route->compile();
$staticPrefix = $compiledRoute->getStaticPrefix();
// If route is static (no path variables), static prefix would be equal to the resulting url for the route.
if (!$compiledRoute->getPathVariables()) {
// Note: It's important to use the same regexp delimiters ("{}") used in \Symfony\Component\HttpFoundation\RequestMatcher::matches.
return preg_match('{'.$pattern.'}', $staticPrefix) ? 1 : -1;
}
if ('' === $staticPrefix) {
return 0;
}
return $this->matchStaticPrefixToPattern($staticPrefix, $pattern);
}
/**
* @param Route $route
* @param string $pattern
*
* @return int
*/
private function matchHostPattern(Route $route, $pattern)
{
$compiledRoute = $route->compile();
$host = $route->getHost() ?: $this->defaultHost;
if (!$host) {
return 0;
}
if (!$compiledRoute->getHostVariables()) {
// Note: It's important to use the same regexp delimiters ("{}") used in \Symfony\Component\HttpFoundation\RequestMatcher::matches.
return preg_match('{'.$pattern.'}i', $host) ? 1 : -1;
}
$staticPrefix = strstr($host, '{', true);
if ('' === $staticPrefix) {
return 0;
}
return $this->matchStaticPrefixToPattern($staticPrefix, $pattern, false);
}
/**
* @param Route $route
* @param array $methods
*
* @return int
*/
private function matchMethods(Route $route, array $methods)
{
if (!$route->getMethods()) {
return 0;
}
$matchingMethods = array_intersect($route->getMethods(), $methods);
if (!$matchingMethods) {
return -1;
}
if ($matchingMethods == $route->getMethods()) {
return 1;
}
return 0;
}
/**
* @param string $staticPrefix
* @param string $pattern
* @param bool $caseSensitive
*
* @return int
*/
private function matchStaticPrefixToPattern($staticPrefix, $pattern, $caseSensitive = true)
{
$parsedPattern = $this->regexParser->parse($pattern);
$patternStaticPrefix = $parsedPattern['staticPrefix'];
if ('' === $patternStaticPrefix) {
return 0;
}
if ($parsedPattern['hasStringStartAssert']) {
$compareLength = min(strlen($staticPrefix), strlen($patternStaticPrefix));
$compareFunction = $caseSensitive ? 'strncmp' : 'strncasecmp';
$compareResult = $compareFunction($staticPrefix, $patternStaticPrefix, $compareLength);
if (0 !== $compareResult) {
return -1;
}
if ($parsedPattern['dynamicPartIsWildcard'] && strlen($patternStaticPrefix) <= strlen($staticPrefix)) {
return 1;
}
} else {
$searchFunction = $caseSensitive ? 'strpos' : 'stripos';
if ($parsedPattern['dynamicPartIsWildcard'] && false !== $searchFunction($staticPrefix, $patternStaticPrefix)) {
return 1;
}
}
return 0;
}
}