ExpressionLanguageProvider.php 1.6 KB

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