New in Symfony 7.2: Expression Language Improvements
The ExpressionLanguage component provides an engine that can compile and
evaluate expressions. The main purpose of the component is to allow users to use
expressions inside configuration for more complex logic. In Symfony 7.2, we’ve
improved it with new features.
New ExpressionLanguage Operators
The ExpressionLanguage component already supports some binary operators: & (and),
| (or) and ^ (xor). In Symfony 7.2, we’re adding support for the rest of
the bitwise operators: << (left shift), >> (right shift) and ~ (not).
These operators are not the most popular ones, but they come in handy in scenarios
like dealing with bitfields inside expressions, such as the Yaml component flags:
use SymfonyComponentExpressionLanguageExpressionLanguage;
$el = new ExpressionLanguage();
$el->evaluate(‚1 << 4‘); // result: 16
$el->evaluate(’32 >> 4′); // result: 2
$el->evaluate(‚~5‘); // result: -6
In addition, we’re adding ^ (xor) support as a logical operator (this operator
was already available as a binary operator):
$el->evaluate(‚$a xor $b‘); // true if either $a or $b is true, but not both
Custom Provider Iterables
The ExpressionLanguage component allows to extend its features with new functions
thanks to custom expression providers. You can pass your providers via the
second argument of the ExpressionLanguage service constructor.
This argument was a PHP array, so you couldn’t use iterable variables (e.g. a
tagged iterator). In Symfony 7.2, we’re turning this array argument into
an iterable argument, so you can use it as follows:
app.my_expression_language:
class: SymfonyComponentExpressionLanguageExpressionLanguage
arguments:
$providers: !tagged_iterator app.my_custom_tag
Add Comment Support
Good configuration languages support comments. That’s why, starting from Symfony 7.2,
the ExpressionLanguage component will add support for single or multi-line comments
with the syntax: /* this is a comment */. This will be helpful for example to
explain certain magic values (e.g. those used in tests):
// Before
$expression = ‚customer.group == ‚vip_customers‚ or customer.id == 123‘;
// After
$expression = <<<EXPRESSION
customer.group == ‚vip_customers‘
or customer.id == 123 /* 123 is an internal test customer */
EXPRESSION;
Symfony Blog