8d7b498665
* 2.8: (28 commits) Detect Mintty for color support on Windows Detect Mintty for color support on Windows [WebProfilerBundle] Fix search button click listener [Form][Type Date/Time] added choice_translation_domain option. Massively simplifying the BC and deprecated-throwing code thanks to suggestions by stof in #15870 Making all "debug" messages use the debug router Making GuardTokenInterface extend TokenInterface Updating behavior to not continue after an authenticator has set the response Add a group for tests of the finder against the FTP server Fix trigger_error calls Fix legacy security tests tweaking message related to configuration edge case that we want to be helpful with Minor tweaks - lowering the required security-http requirement and nulling out a test field Fix license headers Fix license headers Fix license headers Ensure the ClockMock is loaded before using it in the testsuite Allow serializer 3.0 in the PropertyInfo component Add the replace rules for the security-guard component Forbid serializing a Crawler ... |
||
---|---|---|
.. | ||
Node | ||
ParserCache | ||
Resources/bin | ||
Tests | ||
.gitignore | ||
CHANGELOG.md | ||
Compiler.php | ||
composer.json | ||
Expression.php | ||
ExpressionFunction.php | ||
ExpressionFunctionProviderInterface.php | ||
ExpressionLanguage.php | ||
Lexer.php | ||
LICENSE | ||
ParsedExpression.php | ||
Parser.php | ||
phpunit.xml.dist | ||
README.md | ||
SerializedParsedExpression.php | ||
SyntaxError.php | ||
Token.php | ||
TokenStream.php |
ExpressionLanguage Component
The ExpressionLanguage component provides an engine that can compile and evaluate expressions:
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
$language = new ExpressionLanguage();
echo $language->evaluate('1 + foo', array('foo' => 2));
// would output 3
echo $language->compile('1 + foo', array('foo'));
// would output (1 + $foo)
By default, the engine implements simple math and logic functions, method calls, property accesses, and array accesses.
You can extend your DSL with functions:
$compiler = function ($arg) {
return sprintf('strtoupper(%s)', $arg);
};
$evaluator = function (array $variables, $value) {
return strtoupper($value);
};
$language->register('upper', $compiler, $evaluator);
echo $language->evaluate('"foo" ~ upper(foo)', array('foo' => 'bar'));
// would output fooBAR
echo $language->compile('"foo" ~ upper(foo)');
// would output ("foo" . strtoupper($foo))
Resources
You can run the unit tests with the following command:
$ cd path/to/Symfony/Component/ExpressionLanguage/
$ composer.phar install --dev
$ phpunit