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
Contributed by
Alexandre Daubois
and HypeMC
in
#58052
and #58341
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 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):
$el->evaluate('$a xor $b'); // true if either $a or $b is true, but not both
Contributed by
HypeMC
in
#57685
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: Symfony\Component\ExpressionLanguage\ExpressionLanguage
arguments:
$providers: !tagged_iterator app.my_custom_tag
Contributed by
Valtteri R
in
#54978
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 = <<
Sponsor the Symfony project.