ExpressionRequestMatcher.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. use Symfony\Component\ExpressionLanguage\Expression;
  12. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  13. use Symfony\Component\HttpFoundation\RequestMatcher\ExpressionRequestMatcher as NewExpressionRequestMatcher;
  14. trigger_deprecation('symfony/http-foundation', '6.2', 'The "%s" class is deprecated, use "%s" instead.', ExpressionRequestMatcher::class, NewExpressionRequestMatcher::class);
  15. /**
  16. * ExpressionRequestMatcher uses an expression to match a Request.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @deprecated since Symfony 6.2, use "Symfony\Component\HttpFoundation\RequestMatcher\ExpressionRequestMatcher" instead
  21. */
  22. class ExpressionRequestMatcher extends RequestMatcher
  23. {
  24. private ExpressionLanguage $language;
  25. private Expression|string $expression;
  26. /**
  27. * @return void
  28. */
  29. public function setExpression(ExpressionLanguage $language, Expression|string $expression)
  30. {
  31. $this->language = $language;
  32. $this->expression = $expression;
  33. }
  34. public function matches(Request $request): bool
  35. {
  36. if (!isset($this->language)) {
  37. throw new \LogicException('Unable to match the request as the expression language is not available. Try running "composer require symfony/expression-language".');
  38. }
  39. return $this->language->evaluate($this->expression, [
  40. 'request' => $request,
  41. 'method' => $request->getMethod(),
  42. 'path' => rawurldecode($request->getPathInfo()),
  43. 'host' => $request->getHost(),
  44. 'ip' => $request->getClientIp(),
  45. 'attributes' => $request->attributes->all(),
  46. ]) && parent::matches($request);
  47. }
  48. }