ExpressionRequestMatcher.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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\RequestMatcher;
  11. use Symfony\Component\ExpressionLanguage\Expression;
  12. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  15. /**
  16. * ExpressionRequestMatcher uses an expression to match a Request.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class ExpressionRequestMatcher implements RequestMatcherInterface
  21. {
  22. public function __construct(
  23. private ExpressionLanguage $language,
  24. private Expression|string $expression,
  25. ) {
  26. }
  27. public function matches(Request $request): bool
  28. {
  29. return $this->language->evaluate($this->expression, [
  30. 'request' => $request,
  31. 'method' => $request->getMethod(),
  32. 'path' => rawurldecode($request->getPathInfo()),
  33. 'host' => $request->getHost(),
  34. 'ip' => $request->getClientIp(),
  35. 'attributes' => $request->attributes->all(),
  36. ]);
  37. }
  38. }