This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpFoundation/RequestMatcher.php

153 lines
3.4 KiB
PHP
Raw Normal View History

2010-09-01 12:56:06 +01:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-09-01 12:56:06 +01:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
2010-09-01 12:56:06 +01:00
/**
* RequestMatcher compares a pre-defined set of checks against a Request instance.
*
* @author Fabien Potencier <fabien@symfony.com>
2011-07-20 09:06:02 +01:00
*
* @api
2010-09-01 12:56:06 +01:00
*/
class RequestMatcher implements RequestMatcherInterface
{
2011-11-02 15:42:51 +00:00
/**
* @var string
*/
private $path;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
* @var string
*/
private $host;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
2012-06-25 17:21:41 +01:00
* @var array
2011-11-02 15:42:51 +00:00
*/
private $methods = array();
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
2011-12-18 13:33:54 +00:00
* @var string
2011-11-02 15:42:51 +00:00
*/
private $ip;
2011-12-18 13:33:54 +00:00
2011-11-02 15:42:51 +00:00
/**
2011-12-18 13:33:54 +00:00
* @var array
2011-11-02 15:42:51 +00:00
*/
private $attributes = array();
/**
* @param string|null $path
* @param string|null $host
* @param string|string[]|null $methods
* @param string|null $ip
* @param array $attributes
*/
public function __construct($path = null, $host = null, $methods = null, $ip = null, array $attributes = array())
{
$this->matchPath($path);
$this->matchHost($host);
$this->matchMethod($methods);
$this->matchIp($ip);
foreach ($attributes as $k => $v) {
$this->matchAttribute($k, $v);
}
}
2010-09-01 12:56:06 +01:00
/**
* Adds a check for the URL host name.
*
* @param string $regexp A Regexp
*/
public function matchHost($regexp)
{
$this->host = $regexp;
}
/**
* Adds a check for the URL path info.
*
* @param string $regexp A Regexp
*/
public function matchPath($regexp)
{
$this->path = $regexp;
}
/**
* Adds a check for the client IP.
*
* @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
*/
public function matchIp($ip)
{
$this->ip = $ip;
}
/**
* Adds a check for the HTTP method.
*
* @param string|string[]|null $method An HTTP method or an array of HTTP methods
2010-09-01 12:56:06 +01:00
*/
public function matchMethod($method)
{
$this->methods = array_map('strtoupper', (array) $method);
2010-09-01 12:56:06 +01:00
}
/**
* Adds a check for request attribute.
*
* @param string $key The request attribute name
* @param string $regexp A Regexp
*/
public function matchAttribute($key, $regexp)
{
$this->attributes[$key] = $regexp;
}
2010-09-01 12:56:06 +01:00
/**
* {@inheritdoc}
2011-07-20 09:06:02 +01:00
*
* @api
2010-09-01 12:56:06 +01:00
*/
public function matches(Request $request)
{
if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
2010-09-01 12:56:06 +01:00
return false;
}
foreach ($this->attributes as $key => $pattern) {
if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
return false;
}
}
if (null !== $this->path) {
2011-07-19 15:21:58 +01:00
$path = str_replace('#', '\\#', $this->path);
if (!preg_match('#'.$path.'#', rawurldecode($request->getPathInfo()))) {
return false;
}
2010-09-01 12:56:06 +01:00
}
if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#i', $request->getHost())) {
2010-09-01 12:56:06 +01:00
return false;
}
if (null !== $this->ip && !IpUtils::checkIp($request->getClientIp(), $this->ip)) {
2010-09-01 12:56:06 +01:00
return false;
}
return true;
}
}