comparison vendor/symfony/http-foundation/RequestMatcher.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpFoundation;
13
14 /**
15 * RequestMatcher compares a pre-defined set of checks against a Request instance.
16 *
17 * @author Fabien Potencier <fabien@symfony.com>
18 */
19 class RequestMatcher implements RequestMatcherInterface
20 {
21 /**
22 * @var string|null
23 */
24 private $path;
25
26 /**
27 * @var string|null
28 */
29 private $host;
30
31 /**
32 * @var string[]
33 */
34 private $methods = array();
35
36 /**
37 * @var string[]
38 */
39 private $ips = array();
40
41 /**
42 * @var array
43 */
44 private $attributes = array();
45
46 /**
47 * @var string[]
48 */
49 private $schemes = array();
50
51 /**
52 * @param string|null $path
53 * @param string|null $host
54 * @param string|string[]|null $methods
55 * @param string|string[]|null $ips
56 * @param array $attributes
57 * @param string|string[]|null $schemes
58 */
59 public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array(), $schemes = null)
60 {
61 $this->matchPath($path);
62 $this->matchHost($host);
63 $this->matchMethod($methods);
64 $this->matchIps($ips);
65 $this->matchScheme($schemes);
66
67 foreach ($attributes as $k => $v) {
68 $this->matchAttribute($k, $v);
69 }
70 }
71
72 /**
73 * Adds a check for the HTTP scheme.
74 *
75 * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
76 */
77 public function matchScheme($scheme)
78 {
79 $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : array();
80 }
81
82 /**
83 * Adds a check for the URL host name.
84 *
85 * @param string|null $regexp A Regexp
86 */
87 public function matchHost($regexp)
88 {
89 $this->host = $regexp;
90 }
91
92 /**
93 * Adds a check for the URL path info.
94 *
95 * @param string|null $regexp A Regexp
96 */
97 public function matchPath($regexp)
98 {
99 $this->path = $regexp;
100 }
101
102 /**
103 * Adds a check for the client IP.
104 *
105 * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
106 */
107 public function matchIp($ip)
108 {
109 $this->matchIps($ip);
110 }
111
112 /**
113 * Adds a check for the client IP.
114 *
115 * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
116 */
117 public function matchIps($ips)
118 {
119 $this->ips = null !== $ips ? (array) $ips : array();
120 }
121
122 /**
123 * Adds a check for the HTTP method.
124 *
125 * @param string|string[]|null $method An HTTP method or an array of HTTP methods
126 */
127 public function matchMethod($method)
128 {
129 $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : array();
130 }
131
132 /**
133 * Adds a check for request attribute.
134 *
135 * @param string $key The request attribute name
136 * @param string $regexp A Regexp
137 */
138 public function matchAttribute($key, $regexp)
139 {
140 $this->attributes[$key] = $regexp;
141 }
142
143 /**
144 * {@inheritdoc}
145 */
146 public function matches(Request $request)
147 {
148 if ($this->schemes && !in_array($request->getScheme(), $this->schemes, true)) {
149 return false;
150 }
151
152 if ($this->methods && !in_array($request->getMethod(), $this->methods, true)) {
153 return false;
154 }
155
156 foreach ($this->attributes as $key => $pattern) {
157 if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
158 return false;
159 }
160 }
161
162 if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
163 return false;
164 }
165
166 if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
167 return false;
168 }
169
170 if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
171 return true;
172 }
173
174 // Note to future implementors: add additional checks above the
175 // foreach above or else your check might not be run!
176 return count($this->ips) === 0;
177 }
178 }