This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/ExpressionLanguage
Fabien Potencier 8d7b498665 Merge branch '2.8'
* 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
  ...
2015-09-27 12:13:28 +02:00
..
Node [expression-language] Code Cleanup for GetAttrNode 2015-08-13 19:30:11 +08:00
ParserCache Fixed typos 2013-11-09 12:55:32 +01:00
Resources/bin Fix license headers 2015-09-26 14:15:17 +02:00
Tests Fix license headers 2015-09-26 14:15:17 +02:00
.gitignore [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
CHANGELOG.md [ExpressionLanguage] added ExpressionFunction and ExpressionFunctionProviderInterface 2014-09-25 19:08:38 +02:00
Compiler.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
composer.json Merge branch '2.8' 2015-05-12 17:48:43 +02:00
Expression.php added optimized versions of expressions 2013-09-19 12:59:12 +02:00
ExpressionFunction.php [2.6] CS Fixes And Removed An Unused Import 2014-12-07 19:23:39 +01:00
ExpressionFunctionProviderInterface.php [ExpressionLanguage] added ExpressionFunction and ExpressionFunctionProviderInterface 2014-09-25 19:08:38 +02:00
ExpressionLanguage.php [ExpressionLanguage] Fixed expressions cache key generation 2015-09-07 14:45:02 +02:00
Lexer.php [ExpressionLanguage] Fixed an issue with # characters in double quoted string literals 2014-07-22 15:26:34 +02:00
LICENSE Updated copyright to 2015 2015-01-01 14:13:41 +01:00
ParsedExpression.php added optimized versions of expressions 2013-09-19 12:59:12 +02:00
Parser.php Merge branch '2.8' 2015-09-09 20:22:56 +02:00
phpunit.xml.dist Merge branch '2.3' into 2.6 2015-02-24 12:52:21 +01:00
README.md Added missing parameter to the compile function 2013-11-08 16:59:08 +01:00
SerializedParsedExpression.php Removed dead code (unused use statements). 2013-10-16 13:59:56 +02:00
SyntaxError.php [ExpressionLanguage] added the component 2013-09-19 12:59:10 +02:00
Token.php CS fixes 2014-12-04 20:26:11 +00:00
TokenStream.php fixes CS 2015-08-24 09:13:45 +02:00

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