1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Message\RequestMatcher; |
4
|
|
|
|
5
|
|
|
use Http\Message\RequestMatcher; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A port of the Symfony RequestMatcher for PSR-7. |
10
|
|
|
* |
11
|
|
|
* @author Fabien Potencier <[email protected]> |
12
|
|
|
* @author Joel Wurtz <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class RegexRequestMatcher implements RequestMatcher |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $path; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $host; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $methods = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $ips = array(); |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $attributes = array(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string[] |
43
|
|
|
*/ |
44
|
|
|
private $schemes = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The regular expressions used for path or host must be specified without delimiter. |
48
|
|
|
* You do not need to escape the forward slash / to match it. |
49
|
|
|
* |
50
|
|
|
* @param string|null $path Regular expression for the path |
51
|
|
|
* @param string|null $host Regular expression for the hostname |
52
|
|
|
* @param string|string[]|null $methods Method or list of methods to match |
53
|
|
|
* @param string|string[]|null $schemes Scheme or list of schemes to match (e.g. http or https) |
54
|
|
|
*/ |
55
|
10 |
|
public function __construct($path = null, $host = null, $methods = [], $schemes = []) |
56
|
|
|
{ |
57
|
10 |
|
$this->path = $path; |
58
|
10 |
|
$this->host = $host; |
59
|
10 |
|
$this->methods = array_map('strtoupper', (array) $methods); |
60
|
10 |
|
$this->schemes = array_map('strtolower', (array) $schemes); |
61
|
10 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
* |
66
|
|
|
* @api |
67
|
|
|
*/ |
68
|
8 |
|
public function matches(RequestInterface $request) |
69
|
|
|
{ |
70
|
8 |
|
if ($this->schemes && !in_array($request->getUri()->getScheme(), $this->schemes)) { |
|
|
|
|
71
|
1 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
7 |
|
if ($this->methods && !in_array($request->getMethod(), $this->methods)) { |
|
|
|
|
75
|
1 |
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
View Code Duplication |
if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getUri()->getPath()))) { |
|
|
|
|
79
|
|
|
|
80
|
1 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
View Code Duplication |
if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getUri()->getHost())) { |
|
|
|
|
84
|
1 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.