-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Extract request matchers for better reusability #47595
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -33,7 +33,13 @@ | |||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||||
use Symfony\Component\ExpressionLanguage\Expression; | ||||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||||
use Symfony\Component\HttpFoundation\RequestMatcher; | ||||
use Symfony\Component\HttpFoundation\ChainRequestMatcher; | ||||
use Symfony\Component\HttpFoundation\RequestMatcher\AttributesRequestMatcher; | ||||
use Symfony\Component\HttpFoundation\RequestMatcher\HostRequestMatcher; | ||||
use Symfony\Component\HttpFoundation\RequestMatcher\IpsRequestMatcher; | ||||
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher; | ||||
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher; | ||||
use Symfony\Component\HttpFoundation\RequestMatcher\PortRequestMatcher; | ||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||||
use Symfony\Component\HttpKernel\KernelEvents; | ||||
use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher; | ||||
|
@@ -893,7 +899,7 @@ private function createRequestMatcher(ContainerBuilder $container, string $path | |||
$methods = array_map('strtoupper', $methods); | ||||
} | ||||
|
||||
if (null !== $ips) { | ||||
if ($ips) { | ||||
foreach ($ips as $ip) { | ||||
$container->resolveEnvPlaceholders($ip, null, $usedEnvs); | ||||
|
||||
|
@@ -905,22 +911,58 @@ private function createRequestMatcher(ContainerBuilder $container, string $path | |||
} | ||||
} | ||||
|
||||
$id = '.security.request_matcher.'.ContainerBuilder::hash([$path, $host, $port, $methods, $ips, $attributes]); | ||||
$id = '.security.request_matcher.'.ContainerBuilder::hash([ChainRequestMatcher::class, $path, $host, $port, $methods, $ips, $attributes]); | ||||
|
||||
if (isset($this->requestMatchers[$id])) { | ||||
return $this->requestMatchers[$id]; | ||||
} | ||||
|
||||
// only add arguments that are necessary | ||||
$arguments = [$path, $host, $methods, $ips, $attributes, null, $port]; | ||||
while (\count($arguments) > 0 && !end($arguments)) { | ||||
array_pop($arguments); | ||||
$arguments = []; | ||||
if ($methods) { | ||||
if (!$container->hasDefinition($lid = '.security.request_matcher.'.ContainerBuilder::hash([MethodRequestMatcher::class, $methods]))) { | ||||
$container->register($lid, MethodRequestMatcher::class)->setArguments([$methods]); | ||||
} | ||||
$arguments[] = new Reference($lid); | ||||
} | ||||
|
||||
if ($path) { | ||||
if (!$container->hasDefinition($lid = '.security.request_matcher.'.ContainerBuilder::hash([PathRequestMatcher::class, $path]))) { | ||||
$container->register($lid, PathRequestMatcher::class)->setArguments([$path]); | ||||
} | ||||
$arguments[] = new Reference($lid); | ||||
} | ||||
|
||||
if ($host) { | ||||
if (!$container->hasDefinition($lid = '.security.request_matcher.'.ContainerBuilder::hash([HostRequestMatcher::class, $host]))) { | ||||
$container->register($lid, HostRequestMatcher::class)->setArguments([$host]); | ||||
} | ||||
$arguments[] = new Reference($lid); | ||||
} | ||||
|
||||
if ($ips) { | ||||
if (!$container->hasDefinition($lid = '.security.request_matcher.'.ContainerBuilder::hash([IpsRequestMatcher::class, $ips]))) { | ||||
$container->register($lid, IpsRequestMatcher::class)->setArguments([$ips]); | ||||
} | ||||
$arguments[] = new Reference($lid); | ||||
} | ||||
|
||||
if ($attributes) { | ||||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
if (!$container->hasDefinition($lid = '.security.request_matcher.'.ContainerBuilder::hash([AttributesRequestMatcher::class, $attributes]))) { | ||||
$container->register($lid, AttributesRequestMatcher::class)->setArguments([$attributes]); | ||||
} | ||||
$arguments[] = new Reference($lid); | ||||
} | ||||
|
||||
if ($port) { | ||||
if (!$container->hasDefinition($lid = '.security.request_matcher.'.ContainerBuilder::hash([PortRequestMatcher::class, $port]))) { | ||||
$container->register($lid, PortRequestMatcher::class)->setArguments([$port]); | ||||
} | ||||
$arguments[] = new Reference($lid); | ||||
} | ||||
|
||||
$container | ||||
->register($id, RequestMatcher::class) | ||||
->setPublic(false) | ||||
->setArguments($arguments) | ||||
->register($id, ChainRequestMatcher::class) | ||||
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.
Suggested change
? 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. I'd keep the current name: it doesn't have the same interface + needless BC break ahead when renaming. |
||||
->setArguments([$arguments]) | ||||
; | ||||
|
||||
return $this->requestMatchers[$id] = new Reference($id); | ||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Symfony/Component/HttpFoundation/ChainRequestMatcher.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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; | ||
|
||
/** | ||
* ChainRequestMatcher verifies that all checks match against a Request instance. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class ChainRequestMatcher implements RequestMatcherInterface | ||
{ | ||
/** | ||
* @param iterable<RequestMatcherInterface> $matchers | ||
*/ | ||
public function __construct(private iterable $matchers) | ||
{ | ||
} | ||
|
||
public function matches(Request $request): bool | ||
{ | ||
foreach ($this->matchers as $matcher) { | ||
if (!$matcher->matches($request)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Symfony/Component/HttpFoundation/RequestMatcher/AttributesRequestMatcher.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\HttpFoundation\RequestMatcherInterface; | ||
|
||
/** | ||
* Checks the Request attributes matches all regular expressions. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class AttributesRequestMatcher implements RequestMatcherInterface | ||
{ | ||
/** | ||
* @param array<string, string> $regexps | ||
*/ | ||
public function __construct(private array $regexps) | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
public function matches(Request $request): bool | ||
{ | ||
foreach ($this->regexps as $key => $regexp) { | ||
$attribute = $request->attributes->get($key); | ||
if (!\is_string($attribute)) { | ||
return false; | ||
} | ||
if (!preg_match('{'.$regexp.'}', $attribute)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/HttpFoundation/RequestMatcher/ExpressionRequestMatcher.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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\ExpressionLanguage\Expression; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestMatcherInterface; | ||
|
||
/** | ||
* ExpressionRequestMatcher uses an expression to match a Request. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class ExpressionRequestMatcher implements RequestMatcherInterface | ||
{ | ||
public function __construct( | ||
private ExpressionLanguage $language, | ||
private Expression|string $expression, | ||
) { | ||
} | ||
|
||
public function matches(Request $request): bool | ||
{ | ||
return $this->language->evaluate($this->expression, [ | ||
'request' => $request, | ||
'method' => $request->getMethod(), | ||
'path' => rawurldecode($request->getPathInfo()), | ||
'host' => $request->getHost(), | ||
'ip' => $request->getClientIp(), | ||
'attributes' => $request->attributes->all(), | ||
]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.