8000 [RFC][HttpFoundation] Introduce RequestMatcher as a namespace by ro0NL · Pull Request #20274 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpFoundation/RequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace Symfony\Component\HttpFoundation;

use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\RequestMatcher as BaseRequestMatcher;

@trigger_error('The '.RequestMatcher::class.' class is deprecated since version 3.2 and will be removed in 4.0. Use the '.BaseRequestMatcher::class.' class instead.', E_USER_DEPRECATED);

/**
* RequestMatcher compares a pre-defined set of checks against a Request instance.
*
Expand Down Expand Up @@ -96,6 +101,8 @@ public function matchHost($regexp)
*/
public function matchPath($regexp)
{
@trigger_error('The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0. Use the '.PathRequestMatcher::class.' class instead.', E_USER_DEPRECATED);

$this->path = $regexp;
}

Expand Down
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;
Copy link
Contributor

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 or addMatcher() which throws an invalid argument exception when it does not implement the RequestMatcherInterface.

Copy link
Contributor Author

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.

}

/**
* {@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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
        }

        // ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 ExpressionRequestMatcher, which extends the (current) generic request matcher and integrates the EL component additionally. Now we have 2 generic request matchers... 😕

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);
}
14 changes: 5 additions & 9 deletions src/Symfony/Component/HttpFoundation/RequestMatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@

namespace Symfony\Component\HttpFoundation;

use Symfony\Component\HttpFoundation\RequestMatcher\RequestMatcherInterface as BaseRequestMatcherInterface;

@trigger_error('The '.RequestMatcherInterface::class.' class is deprecated since version 3.2 and will be removed in 4.0. Use the '.BaseRequestMatcherInterface::class.' class instead.', E_USER_DEPRECATED);

/**
* RequestMatcherInterface is an interface for strategies to match a Request.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface RequestMatcherInterface
interface RequestMatcherInterface extends BaseRequestMatcherInterface
{
/**
* 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);
}
0