vendor/symfony/dependency-injection/ExpressionLanguageProvider.php line 33

Open in your IDE?
  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\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Exception\LogicException;
  12. use Symfony\Component\ExpressionLanguage\ExpressionFunction;
  13. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  14. /**
  15.  * Define some ExpressionLanguage functions.
  16.  *
  17.  * To get a service, use service('request').
  18.  * To get a parameter, use parameter('kernel.debug').
  19.  * To get an env variable, use env('SOME_VARIABLE').
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  */
  23. class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
  24. {
  25.     private ?\Closure $serviceCompiler;
  26.     private ?\Closure $getEnv;
  27.     public function __construct(callable $serviceCompiler null\Closure $getEnv null)
  28.     {
  29.         $this->serviceCompiler null === $serviceCompiler null $serviceCompiler(...);
  30.         $this->getEnv $getEnv;
  31.     }
  32.     public function getFunctions(): array
  33.     {
  34.         return [
  35.             new ExpressionFunction('service'$this->serviceCompiler ?? function ($arg) {
  36.                 return sprintf('$this->get(%s)'$arg);
  37.             }, function (array $variables$value) {
  38.                 return $variables['container']->get($value);
  39.             }),
  40.             new ExpressionFunction('parameter', function ($arg) {
  41.                 return sprintf('$this->getParameter(%s)'$arg);
  42.             }, function (array $variables$value) {
  43.                 return $variables['container']->getParameter($value);
  44.             }),
  45.             new ExpressionFunction('env', function ($arg) {
  46.                 return sprintf('$this->getEnv(%s)'$arg);
  47.             }, function (array $variables$value) {
  48.                 if (!$this->getEnv) {
  49.                     throw new LogicException('You need to pass a getEnv closure to the expression langage provider to use the "env" function.');
  50.                 }
  51.                 return ($this->getEnv)($value);
  52.             }),
  53.             new ExpressionFunction('arg', function ($arg) {
  54.                 return sprintf('$args?->get(%s)'$arg);
  55.             }, function (array $variables$value) {
  56.                 return $variables['args']?->get($value);
  57.             }),
  58.         ];
  59.     }
  60. }