The ExpressionLanguage Component

The ExpressionLanguage component provides an engine that can compile andevaluate expressions. An expression is a one-liner that returns a value(mostly, but not limited to, Booleans).

Installation

  1. $ composer require symfony/expression-language

Note

If you install this component outside of a Symfony application, you mustrequire the vendor/autoload.php file in your code to enable the classautoloading mechanism provided by Composer. Readthis article for more details.

How can the Expression Engine Help Me?

The purpose of the component is to allow users to use expressions insideconfiguration for more complex logic. For some examples, the Symfony Frameworkuses expressions in security, for validation rules and in route matching.

Besides using the component in the framework itself, the ExpressionLanguagecomponent is a perfect candidate for the foundation of a business rule engine.The idea is to let the webmaster of a website configure things in a dynamicway without using PHP and without introducing security problems:

  1. # Get the special price if
  2. user.getGroup() in ['good_customers', 'collaborator']
  3.  
  4. # Promote article to the homepage when
  5. article.commentCount > 100 and article.category not in ["misc"]
  6.  
  7. # Send an alert when
  8. product.stock < 15

Expressions can be seen as a very restricted PHP sandbox and are immune toexternal injections as you must explicitly declare which variables are availablein an expression.

Usage

The ExpressionLanguage component can compile and evaluate expressions.Expressions are one-liners that often return a Boolean, which can be usedby the code executing the expression in an if statement. A simple exampleof an expression is 1 + 2. You can also use more complicated expressions,such as someArray[3].someMethod('bar').

The component provides 2 ways to work with expressions:

  • evaluation: the expression is evaluated without being compiled to PHP;
  • compile: the expression is compiled to PHP, so it can be cached andevaluated.The main class of the component isExpressionLanguage:
  1. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  2.  
  3. $expressionLanguage = new ExpressionLanguage();
  4.  
  5. var_dump($expressionLanguage->evaluate('1 + 2')); // displays 3
  6.  
  7. var_dump($expressionLanguage->compile('1 + 2')); // displays (1 + 2)

Expression Syntax

See The Expression Syntax to learn the syntax of theExpressionLanguage component.

Passing in Variables

You can also pass variables into the expression, which can be of any validPHP type (including objects):

  1. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  2.  
  3. $expressionLanguage = new ExpressionLanguage();
  4.  
  5. class Apple
  6. {
  7. public $variety;
  8. }
  9.  
  10. $apple = new Apple();
  11. $apple->variety = 'Honeycrisp';
  12.  
  13. var_dump($expressionLanguage->evaluate(
  14. 'fruit.variety',
  15. [
  16. 'fruit' => $apple,
  17. ]
  18. ));

This will print "Honeycrisp". For more information, see the The Expression Syntaxentry, especially Working with Objects and Working with Arrays.

Caching

The component provides some different caching strategies, read more about themin Caching Expressions Using Parser Caches.

AST Dumping and Editing

The AST (Abstract Syntax Tree) of expressions can be dumped and manipulatedas explained in Dumping and Manipulating the AST of Expressions.

Learn More