-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RFC][HttpFoundation] Introduce RequestMatcher as a namespace #20274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\RequestMatcher; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @author Roland Franssen <franssen.roland@gmail.com> | ||
*/ | ||
class ChainRequestMatcher implements RequestMatcherInterface | ||
{ | ||
private $matchers; | ||
|
||
/** | ||
* @param RequestMatcherInterface[] $matchers | ||
*/ | ||
public function __construct(array $matchers) | ||
{ | ||
$this->matchers = $matchers; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function matches(Request $request) | ||
{ | ||
foreach ($this->matchers as $matcher) { | ||
if (!$matcher->matches($request)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\RequestMatcher; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
||
/** | ||
* ExpressionRequestMatcher uses an expression to match a Request. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Roland Franssen <franssen.roland@gmail.com> | ||
*/ | ||
class ExpressionRequestMatcher implements RequestMatcherInterface | ||
{ | ||
private $language; | ||
private $expression; | ||
|
||
public function __construct(ExpressionLanguage $language, $expression) | ||
{ | ||
$this->language = $language; | ||
$this->expression = $expression; | ||
} | ||
|
||
public function matches(Request $request) | ||
{ | ||
return (bool) $this->language->evaluate($this->expression, array( | ||
'request' => $request, | ||
'method' => $request->getMethod(), | ||
'path' => rawurldecode($request->getPathInfo()), | ||
'host' => $request->getHost(), | ||
'ip' => $request->getClientIp(), | ||
'attributes' => $request->attributes->all(), | ||
)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\RequestMatcher; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Roland Franssen <franssen.roland@gmail.com> | ||
*/ | ||
class PathRequestMatcher implements RequestMatcherInterface | ||
{ | ||
private $path; | ||
private $isRegex; | ||
|
||
/** | ||
* @param string $path | ||
* @param bool $isRegex | ||
*/ | ||
public function __construct($path, $isRegex = false) | ||
{ | ||
$this->path = $path; | ||
$this->isRegex = $isRegex; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function matches(Request $request) | ||
{ | ||
return $this->isRegex | ||
? (bool) preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo())) | ||
: $this->path === rawurldecode($request->getPathInfo()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\RequestMatcher; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* RequestMatcher compares a pre-defined set of checks against a Request instance. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Roland Franssen <franssen.roland@gmail.com> | ||
*/ | ||
class RequestMatcher extends ChainRequestMatcher | ||
{ | ||
private $host; | ||
private $methods; | ||
private $ips; | ||
private $attributes; | ||
private $schemes; | ||
|
||
/** | ||
* @param string|null $path | ||
* @param string|null $host | ||
* @param string[] $methods | ||
* @param string[] $ips | ||
* @param array $attributes | ||
* @param string[] $schemes | ||
*/ | ||
public function __construct($path = null, $host = null, array $methods = array(), array $ips = array(), array $attributes = array(), array $schemes = array()) | ||
{ | ||
$matchers = array(); | ||
if (null !== $path) { | ||
$matchers[] = new PathRequestMatcher($path, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to make it even more reusable it would be nice to be able to inject custom matchers: /**
* @param RequestMatcherInferace|string|null $path
* @param RequestMatcherInferace|string|null $host
* @param RequestMatcherInferace|string[] $methods
* @param RequestMatcherInferace|string[] $ips
* @param RequestMatcherInferace|array $attributes
* @param RequestMatcherInferace|string[] $schemes
*/
public function __construct($path = null, $host = null, array $methods = array(), array $ips, array(), array $attributes = array(), array $schemes = array())
{
$matchers = array();
if (null !== $path) {
$matchers[] = $path instanceof RequestMatcherInterface ? $path : new PathRequestMatcher($path, true);
}
// ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure.. to me this class just factorizes a chain matcher (from scalars). If you have ready-to-use matchers, just chain them yourself 😕 And eventually the entire class may not be needed, if we built a chain in the security extension from config. Same for the current With this PR i propse a standalone expression request matcher, which you can combine with other matchers (using a chain). |
||
} | ||
|
||
parent::__construct($matchers); | ||
|
||
// @todo | ||
$this->host = $host; | ||
$this->methods = $methods; | ||
$this->ips = $ips; | ||
$this->attributes = $attributes; | ||
$this->schemes = $schemes; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
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; | ||
} | ||
|
||
foreach ($this->attributes as $key => $pattern) { | ||
if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) { | ||
return false; | ||
} | ||
} | ||
|
||
if (!parent::matches($request)) { | ||
return false; | ||
} | ||
|
||
if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) { | ||
return false; | ||
} | ||
|
||
if (IpUtils::checkIp($request->getClientIp(), $this->ips)) { | ||
return true; | ||
} | ||
|
||
// Note to future implementors: add additional checks above the | ||
// foreach above or else your check might not be run! | ||
return count($this->ips) === 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\RequestMatcher; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* RequestMatcherInterface is an interface for strategies to match a Request. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
interface RequestMatcherInterface | ||
{ | ||
/** | ||
* Decides whether the rule(s) implemented by the strategy matches the supplied request. | ||
* | ||
* @param Request $request The request to check for a match | ||
* | ||
* @return bool true if the request matches, false otherwise | ||
*/ | ||
public function matches(Request $request); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should loop through the matchers and maybe use a public
add
oraddMatcher()
which throws an invalid argument exception when it does not implement theRequestMatcherInterface
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 not sure about a
add
method though, making the class mutable. I tend to like having immutable matchers by default.