ExpressionRequestMatcher.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. /**
  14. * ExpressionRequestMatcher uses an expression to match a Request.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class ExpressionRequestMatcher extends RequestMatcher
  19. {
  20. private $language;
  21. private $expression;
  22. public function setExpression(ExpressionLanguage $language, Expression|string $expression)
  23. {
  24. $this->language = $language;
  25. $this->expression = $expression;
  26. }
  27. public function matches(Request $request): bool
  28. {
  29. if (!isset($this->language)) {
  30. throw new \LogicException('Unable to match the request as the expression language is not available.');
  31. }
  32. return $this->language->evaluate($this->expression, [
  33. 'request' => $request,
  34. 'method' => $request->getMethod(),
  35. 'path' => rawurldecode($request->getPathInfo()),
  36. 'host' => $request->getHost(),
  37. 'ip' => $request->getClientIp(),
  38. 'attributes' => $request->attributes->all(),
  39. ]) && parent::matches($request);
  40. }
  41. }