ExpressionLanguageProvider.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. public function __construct(
  22. private ServiceProviderInterface $functions,
  23. ) {
  24. }
  25. public function getFunctions(): array
  26. {
  27. $functions = [];
  28. foreach ($this->functions->getProvidedServices() as $function => $type) {
  29. $functions[] = new ExpressionFunction(
  30. $function,
  31. static fn (...$args) => sprintf('($context->getParameter(\'_functions\')->get(%s)(%s))', var_export($function, true), implode(', ', $args)),
  32. fn ($values, ...$args) => $values['context']->getParameter('_functions')->get($function)(...$args)
  33. );
  34. }
  35. return $functions;
  36. }
  37. public function get(string $function): callable
  38. {
  39. return $this->functions->get($function);
  40. }
  41. }