4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ExpressionLanguage;
14 use Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache;
15 use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface;
18 * Allows to compile and evaluate expressions written in your own DSL.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class ExpressionLanguage
25 * @var ParserCacheInterface
32 protected $functions = array();
35 * @param ParserCacheInterface $cache
36 * @param ExpressionFunctionProviderInterface[] $providers
38 public function __construct(ParserCacheInterface $cache = null, array $providers = array())
40 $this->cache = $cache ?: new ArrayParserCache();
41 $this->registerFunctions();
42 foreach ($providers as $provider) {
43 $this->registerProvider($provider);
48 * Compiles an expression source code.
50 * @param Expression|string $expression The expression to compile
51 * @param array $names An array of valid names
53 * @return string The compiled PHP source code
55 public function compile($expression, $names = array())
57 return $this->getCompiler()->compile($this->parse($expression, $names)->getNodes())->getSource();
61 * Evaluate an expression.
63 * @param Expression|string $expression The expression to compile
64 * @param array $values An array of values
66 * @return string The result of the evaluation of the expression
68 public function evaluate($expression, $values = array())
70 return $this->parse($expression, array_keys($values))->getNodes()->evaluate($this->functions, $values);
74 * Parses an expression.
76 * @param Expression|string $expression The expression to parse
77 * @param array $names An array of valid names
79 * @return ParsedExpression A ParsedExpression instance
81 public function parse($expression, $names)
83 if ($expression instanceof ParsedExpression) {
88 $cacheKeyItems = array();
90 foreach ($names as $nameKey => $name) {
91 $cacheKeyItems[] = is_int($nameKey) ? $name : $nameKey.':'.$name;
94 $key = $expression.'//'.implode('|', $cacheKeyItems);
96 if (null === $parsedExpression = $this->cache->fetch($key)) {
97 $nodes = $this->getParser()->parse($this->getLexer()->tokenize((string) $expression), $names);
98 $parsedExpression = new ParsedExpression((string) $expression, $nodes);
100 $this->cache->save($key, $parsedExpression);
103 return $parsedExpression;
107 * Registers a function.
109 * @param string $name The function name
110 * @param callable $compiler A callable able to compile the function
111 * @param callable $evaluator A callable able to evaluate the function
113 * @throws \LogicException when registering a function after calling evaluate(), compile() or parse()
115 * @see ExpressionFunction
117 public function register($name, $compiler, $evaluator)
119 if (null !== $this->parser) {
120 throw new \LogicException('Registering functions after calling evaluate(), compile() or parse() is not supported.');
123 $this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
126 public function addFunction(ExpressionFunction $function)
128 $this->register($function->getName(), $function->getCompiler(), $function->getEvaluator());
131 public function registerProvider(ExpressionFunctionProviderInterface $provider)
133 foreach ($provider->getFunctions() as $function) {
134 $this->addFunction($function);
138 protected function registerFunctions()
140 $this->register('constant', function ($constant) {
141 return sprintf('constant(%s)', $constant);
142 }, function (array $values, $constant) {
143 return constant($constant);
147 private function getLexer()
149 if (null === $this->lexer) {
150 $this->lexer = new Lexer();
156 private function getParser()
158 if (null === $this->parser) {
159 $this->parser = new Parser($this->functions);
162 return $this->parser;
165 private function getCompiler()
167 if (null === $this->compiler) {
168 $this->compiler = new Compiler($this->functions);
171 return $this->compiler->reset();