Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #47595 [HttpFoundation] Extract request matchers for better r…
…eusability (fabpot) This PR was merged into the 6.2 branch. Discussion ---------- [HttpFoundation] Extract request matchers for better reusability | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | The `RequestMatcher` class hardcodes its matchers. This PR extracts those into their own classes so that we can compose a `RequestMatcher` and allow better reusability. Commits ------- 6cfd3b7 [HttpFoundation] Extract request matchers for better reusability
- Loading branch information
Showing
29 changed files
with
931 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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) | ||
{ | ||
} | ||
|
||
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 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.