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:
1 2 3 4 5 6 7
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
$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):
1
$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:
1 2 3 4
app.my_expression_language:
class: Symfony\Component\ExpressionLanguage\ExpressionLanguage
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):
1 2 3 4 5 6 7 8 9 10
// Before
$expression = "customer.group == 'vip_customers' or customer.id == 123";
// After
$expression = <<<EXPRESSION
/* This expression checks that the purchase is made by
a customer who doesn't belong to the regular users group */
customer.group == 'vip_customers'
or customer.id == 123 /* 123 is an internal test customer */
EXPRESSION;