4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation;
15 * RequestMatcher compares a pre-defined set of checks against a Request instance.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class RequestMatcher implements RequestMatcherInterface
34 private $methods = array();
39 private $ips = array();
44 private $attributes = array();
49 private $schemes = array();
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
59 public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array(), $schemes = null)
61 $this->matchPath($path);
62 $this->matchHost($host);
63 $this->matchMethod($methods);
64 $this->matchIps($ips);
65 $this->matchScheme($schemes);
67 foreach ($attributes as $k => $v) {
68 $this->matchAttribute($k, $v);
73 * Adds a check for the HTTP scheme.
75 * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
77 public function matchScheme($scheme)
79 $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : array();
83 * Adds a check for the URL host name.
85 * @param string|null $regexp A Regexp
87 public function matchHost($regexp)
89 $this->host = $regexp;
93 * Adds a check for the URL path info.
95 * @param string|null $regexp A Regexp
97 public function matchPath($regexp)
99 $this->path = $regexp;
103 * Adds a check for the client IP.
105 * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
107 public function matchIp($ip)
109 $this->matchIps($ip);
113 * Adds a check for the client IP.
115 * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
117 public function matchIps($ips)
119 $this->ips = null !== $ips ? (array) $ips : array();
123 * Adds a check for the HTTP method.
125 * @param string|string[]|null $method An HTTP method or an array of HTTP methods
127 public function matchMethod($method)
129 $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : array();
133 * Adds a check for request attribute.
135 * @param string $key The request attribute name
136 * @param string $regexp A Regexp
138 public function matchAttribute($key, $regexp)
140 $this->attributes[$key] = $regexp;
146 public function matches(Request $request)
148 if ($this->schemes && !in_array($request->getScheme(), $this->schemes, true)) {
152 if ($this->methods && !in_array($request->getMethod(), $this->methods, true)) {
156 foreach ($this->attributes as $key => $pattern) {
157 if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
162 if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
166 if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
170 if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
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;