ExpressionLanguageProvider.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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\Routing\Matcher;
  11. use Symfony\Component\ExpressionLanguage\ExpressionFunction;
  12. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  13. use Symfony\Contracts\Service\ServiceProviderInterface;
  14. /**
  15. * Exposes functions defined in the request context to route conditions.
  16. *
  17. * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
  18. */
  19. class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
  20. {
  21. private ServiceProviderInterface $functions;
  22. public function __construct(ServiceProviderInterface $functions)
  23. {
  24. $this->functions = $functions;
  25. }
  26. public function getFunctions(): array
  27. {
  28. $functions = [];
  29. foreach ($this->functions->getProvidedServices() as $function => $type) {
  30. $functions[] = new ExpressionFunction(
  31. $function,
  32. static fn (...$args) => \sprintf('($context->getParameter(\'_functions\')->get(%s)(%s))', var_export($function, true), implode(', ', $args)),
  33. fn ($values, ...$args) => $values['context']->getParameter('_functions')->get($function)(...$args)
  34. );
  35. }
  36. return $functions;
  37. }
  38. public function get(string $function): callable
  39. {
  40. return $this->functions->get($function);
  41. }
  42. }