ExpressionFunction example

public function __construct(ServiceProviderInterface $functions)
    {
        $this->functions = $functions;
    }

    public function getFunctions(): array
    {
        $functions = [];

        foreach ($this->functions->getProvidedServices() as $function => $type) {
            $functions[] = new ExpressionFunction(
                $function,
                static fn (...$args) => sprintf('($context->getParameter(\'_functions\')->get(%s)(%s))', var_export($function, true)implode(', ', $args)),
                fn ($values, ...$args) => $values['context']->getParameter('_functions')->get($function)(...$args)
            );
        }

        return $functions;
    }

    public function get(string $function): callable
    {
        
private ?\Closure $getEnv;

    public function __construct(callable $serviceCompiler = null, \Closure $getEnv = null)
    {
        $this->serviceCompiler = null === $serviceCompiler ? null : $serviceCompiler(...);
        $this->getEnv = $getEnv;
    }

    public function getFunctions(): array
    {
        return [
            new ExpressionFunction('service', $this->serviceCompiler ?? fn ($arg) => sprintf('$container->get(%s)', $arg)fn (array $variables$value) => $variables['container']->get($value)),

            new ExpressionFunction('parameter', fn ($arg) => sprintf('$container->getParameter(%s)', $arg)fn (array $variables$value) => $variables['container']->getParameter($value)),

            new ExpressionFunction('env', fn ($arg) => sprintf('$container->getEnv(%s)', $arg)function Darray $variables$value) {
                if (!$this->getEnv) {
                    throw new LogicException('You need to pass a getEnv closure to the expression langage provider to use the "env" function.');
                }

                return ($this->getEnv)($value);
            }),

            
Home | Imprint | This part of the site doesn't use cookies.