feature #10667 [HttpFoundation] Add a way to match a specific scheme in RequestMatcher #10556 (ProPheT777)
This PR was squashed before being merged into the 2.6-dev branch (closes #10667).
Discussion
----------
[HttpFoundation] Add a way to match a specific scheme in RequestMatcher #10556
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #10556
| License | MIT
| Doc PR | no
Commits
-------
787ae07
[HttpFoundation] Add a way to match a specific scheme in RequestMatcher #10556
This commit is contained in:
commit
e8146810e7
@ -45,24 +45,42 @@ class RequestMatcher implements RequestMatcherInterface
|
||||
*/
|
||||
private $attributes = array();
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $schemes = array();
|
||||
|
||||
/**
|
||||
* @param string|null $path
|
||||
* @param string|null $host
|
||||
* @param string|string[]|null $methods
|
||||
* @param string|string[]|null $ips
|
||||
* @param array $attributes
|
||||
* @param string|string[]|null $schemes
|
||||
*/
|
||||
public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array())
|
||||
public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array(), $schemes = null)
|
||||
{
|
||||
$this->matchPath($path);
|
||||
$this->matchHost($host);
|
||||
$this->matchMethod($methods);
|
||||
$this->matchIps($ips);
|
||||
$this->matchScheme($schemes);
|
||||
|
||||
foreach ($attributes as $k => $v) {
|
||||
$this->matchAttribute($k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a check for the HTTP scheme.
|
||||
*
|
||||
* @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
|
||||
*/
|
||||
public function matchScheme($scheme)
|
||||
{
|
||||
$this->schemes = array_map('strtolower', (array) $scheme);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a check for the URL host name.
|
||||
*
|
||||
@ -106,7 +124,7 @@ class RequestMatcher implements RequestMatcherInterface
|
||||
/**
|
||||
* Adds a check for the HTTP method.
|
||||
*
|
||||
* @param string|string[]|null $method An HTTP method or an array of HTTP methods
|
||||
* @param string|string[] $method An HTTP method or an array of HTTP methods
|
||||
*/
|
||||
public function matchMethod($method)
|
||||
{
|
||||
@ -131,6 +149,10 @@ class RequestMatcher implements RequestMatcherInterface
|
||||
*/
|
||||
public function matches(Request $request)
|
||||
{
|
||||
if ($this->schemes && !in_array($request->getScheme(), $this->schemes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -43,6 +43,25 @@ class RequestMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testScheme()
|
||||
{
|
||||
$httpRequest = $request = $request = Request::create('');
|
||||
$httpsRequest = $request = $request = Request::create('', 'get', array(), array(), array(), array('HTTPS' => 'on'));
|
||||
|
||||
$matcher = new RequestMatcher();
|
||||
$matcher->matchScheme('https');
|
||||
$this->assertFalse($matcher->matches($httpRequest));
|
||||
$this->assertTrue($matcher->matches($httpsRequest));
|
||||
|
||||
$matcher->matchScheme('http');
|
||||
$this->assertFalse($matcher->matches($httpsRequest));
|
||||
$this->assertTrue($matcher->matches($httpRequest));
|
||||
|
||||
$matcher = new RequestMatcher();
|
||||
$this->assertTrue($matcher->matches($httpsRequest));
|
||||
$this->assertTrue($matcher->matches($httpRequest));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider testHostFixture
|
||||
*/
|
||||
@ -68,7 +87,8 @@ class RequestMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
array('.*\.example\.COM', true),
|
||||
array('\.example\.COM$', true),
|
||||
array('^.*\.example\.COM$', true),
|
||||
array('.*\.sensio\.COM', false), );
|
||||
array('.*\.sensio\.COM', false),
|
||||
);
|
||||
}
|
||||
|
||||
public function testPath()
|
||||
|
Reference in New Issue
Block a user