1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Routing\Matcher;
13
14use Symfony\Component\ExpressionLanguage\ExpressionFunction;
15use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
16use Symfony\Contracts\Service\ServiceProviderInterface;
17
18/**
19 * Exposes functions defined in the request context to route conditions.
20 *
21 * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
22 */
23class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
24{
25    private $functions;
26
27    public function __construct(ServiceProviderInterface $functions)
28    {
29        $this->functions = $functions;
30    }
31
32    /**
33     * {@inheritdoc}
34     */
35    public function getFunctions()
36    {
37        foreach ($this->functions->getProvidedServices() as $function => $type) {
38            yield new ExpressionFunction(
39                $function,
40                static function (...$args) use ($function) {
41                    return sprintf('($context->getParameter(\'_functions\')->get(%s)(%s))', var_export($function, true), implode(', ', $args));
42                },
43                function ($values, ...$args) use ($function) {
44                    return $values['context']->getParameter('_functions')->get($function)(...$args);
45                }
46            );
47        }
48    }
49
50    public function get(string $function): callable
51    {
52        return $this->functions->get($function);
53    }
54}
55