-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequestCondition.php
More file actions
306 lines (273 loc) · 7.43 KB
/
RequestCondition.php
File metadata and controls
306 lines (273 loc) · 7.43 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
namespace Packaged\Routing;
use Exception;
use InvalidArgumentException;
use Packaged\Context\Context;
use Packaged\Helpers\Strings;
use function is_numeric;
use function preg_match;
use function preg_replace;
use function rtrim;
use function strpos;
class RequestCondition implements Condition, RouteCompleter
{
const SCHEME = 'scheme';
const PORT = 'port';
const PATH = 'path';
const DOMAIN = 'domain';
const SUBDOMAIN = 'subdomain';
const TLD = 'tld';
const METHOD = 'method';
const AJAX = 'xmlhttprequest';
const LANGUAGE = 'language';
const ROOT_DOMAIN = 'rootdomain';
const HOSTNAME = 'hostname';
const QUERY_KEY = 'querykey';
const QUERY_VALUE = 'queryvalue';
const TYPE_MATCH = 'match';
const TYPE_EXACT = 'exact';
const TYPE_START = 'start';
const TYPE_START_CASEI = 'start.casei';
const TYPE_REGEX = 'regex';
const META_ROUTED_PATH = '_routing_routed_path';
protected $_routedPath;
protected $_extractedData = [];
protected $_conditions = [];
public static function i()
{
return new static();
}
public function path($path, $type = self::TYPE_MATCH)
{
return $this->_add(self::PATH, $path, $type);
}
protected function _add($on, $with, $type = self::TYPE_MATCH)
{
$this->_conditions[] = [$on, $with, $type];
return $this;
}
public function scheme($protocol, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::SCHEME, $protocol, $matchType);
}
public function port($port, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::PORT, $port, $matchType);
}
public function method($method, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::METHOD, $method, $matchType);
}
public function ajax()
{
return $this->_add(self::AJAX, true);
}
public function domain($domain, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::DOMAIN, $domain, $matchType);
}
public function tld($tld, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::TLD, $tld, $matchType);
}
public function rootDomain($rootDomain, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::ROOT_DOMAIN, $rootDomain, $matchType);
}
public function subDomain($subdomain, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::SUBDOMAIN, $subdomain, $matchType);
}
public function hostname($hostname, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::HOSTNAME, $hostname, $matchType);
}
public function hasQueryKey($key, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::QUERY_KEY, $key, $matchType);
}
public function hasQueryValue($key, $value, $matchType = self::TYPE_MATCH)
{
return $this->_add(self::QUERY_VALUE, [$key, $value], $matchType);
}
public function match(Context $context): bool
{
$this->_routedPath = $context->meta()->get(self::META_ROUTED_PATH, '/');
foreach($this->_conditions as [$matchOn, $matchWith, $matchType])
{
if($matchOn === self::PATH)
{
if($matchWith === '/')
{
$matchType = self::TYPE_START;
}
else if(is_string($matchWith) && $matchType !== self::TYPE_REGEX)
{
$matchWith = $this->_convertPathToRegex($matchWith, $matchType);
$matchType = self::TYPE_REGEX;
}
}
if(!$this->_matchConstraint($context, $matchOn, $matchWith, $matchType))
{
return false;
}
}
return true;
}
protected function _convertPathToRegex($path, $type)
{
if(substr($path, -1) === '$')
{
$path = substr($path, 0, -1);
$type = self::TYPE_EXACT;
}
if(empty($path))
{
$path = $this->_routedPath;
}
if($path && $path[0] !== '/')
{
$path = rtrim($this->_routedPath, '/') . ($path ? '/' . $path : '');
}
if(strpos($path, '{') !== false)
{
$idPat = "([a-zA-Z_][a-zA-Z0-9_\-]*)";
$path = preg_replace(
[
"/{" . "$idPat\@alphanum}/",
"/{" . "$idPat\@alnum}/",
"/{" . "$idPat\@alpha}/",
"/{" . "$idPat\@all}/",
"/{" . "$idPat\@num}/",
"/{" . "$idPat}/",
],
[
"(?P<$1>\w+)",
"(?P<$1>\w+)",
"(?P<$1>[a-zA-Z]+)",
"(?P<$1>.+)",
"(?P<$1>\d+)",
"(?P<$1>[^\/]+)",
],
$path
);
}
$flags = 'u';
$path = '#^' . $path;
switch($type)
{
case self::TYPE_START:
break;
case self::TYPE_EXACT:
$path .= '$';
break;
case self::TYPE_MATCH:
// path should always match full parts, so check ends with slash or end of string
$path .= '(?=/|$)';
$flags .= 'i';
break;
case self::TYPE_START_CASEI:
default:
$flags .= 'i';
break;
}
return $path . '#' . $flags;
}
protected function _matchConstraint(Context $context, $matchOn, $matchWith, $matchType)
{
$value = $this->_matchValue($context, $matchOn, $matchWith);
if($matchOn == self::QUERY_VALUE)
{
$matchWith = $matchWith[1];
}
$matches = [];
switch($matchType)
{
case self::TYPE_REGEX:
try
{
if(!preg_match($matchWith, $value, $matches))
{
return false;
}
}
catch(Exception $e)
{
throw new InvalidArgumentException('Invalid regex passed to path ' . $matchWith, 400, $e);
}
break;
case self::TYPE_START:
case self::TYPE_START_CASEI:
if(!Strings::startsWith($value, $matchWith, $matchType == self::TYPE_START))
{
return false;
}
break;
case self::TYPE_MATCH:
if($value != $matchWith)
{
return false;
}
break;
case self::TYPE_EXACT:
default:
if($value !== $matchWith)
{
return false;
}
}
if($matchOn == self::PATH && !empty($matches[0]))
{
$this->_routedPath = $matches[0];
foreach($matches as $k => $v)
{
if(!is_numeric($k))
{
$this->_extractedData[$k] = $v;
}
}
}
return true;
}
protected function _matchValue(Context $context, $on, $matchWith)
{
switch($on)
{
case self::PATH;
return $context->request()->path();
case self::SUBDOMAIN;
return $context->request()->subDomain();
case self::ROOT_DOMAIN;
return $context->request()->urlSprintf('%d.%t');
case self::DOMAIN;
return $context->request()->domain();
case self::TLD;
return $context->request()->tld();
case self::SCHEME;
return $context->request()->getScheme();
case self::PORT;
return $context->request()->port();
case self::METHOD;
return $context->request()->getRealMethod();
case self::AJAX;
return $context->request()->isXmlHttpRequest();
case self::QUERY_KEY;
return $context->request()->query->has($matchWith) ? $matchWith : null;
case self::QUERY_VALUE;
return $context->request()->query->get($matchWith[0]);
case self::HOSTNAME;
return $context->request()->getHost();
}
// @codeCoverageIgnoreStart
return null;
// @codeCoverageIgnoreEnd
}
public function complete(Context $context)
{
$context->meta()->set(self::META_ROUTED_PATH, $this->_routedPath);
if(!empty($this->_extractedData))
{
$context->routeData()->add($this->_extractedData);
}
}
}