Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
Continuing ExpressionLanguage component docs#3241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletionscomponents/expression_language/extending.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
.. index:: | ||
single: Extending; ExpressionLanguage | ||
Extending the ExpressionLanguage | ||
================================ | ||
The ExpressionLanguage can be extended by adding custom functions. For | ||
instance, in the framework, the security has custom functions to check the | ||
user's role. | ||
.. note:: | ||
If you want to learn how to use functions in an expression, read | ||
":ref:`component-expression-functions`". | ||
Register Functions | ||
------------------ | ||
Functions will be registered on the current ``ExpressionLanguage`` instance. | ||
That means the functions can be used in any expression executed by that | ||
instance. | ||
To register a function, use | ||
:method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::register``. | ||
This method has 3 arguments: | ||
* **name** - The name of the function in an expression; | ||
* **compiler** - A function executed when compiling an expression using the | ||
function; | ||
* **evaluator** - A function executed when the expression is evaluated. | ||
.. code-block:: php | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
$language = new ExpressionLanguage(); | ||
$language->register('lowercase', function ($str) { | ||
if (!is_string($str)) { | ||
return $str; | ||
} | ||
return sprintf('strtolower(%s)', $str); | ||
}, function ($str) { | ||
if (!is_string($str)) { | ||
return $str; | ||
} | ||
return strtolower($str); | ||
}); | ||
echo $language->evaluate('lowercase("HELLO")'); | ||
This will print ``hello``. | ||
Creating a new ExpressionLanguage class | ||
--------------------------------------- | ||
When you use the ``ExpressionLanguage`` class in your library, it's recommend | ||
to create a new ``ExpressionLanguage`` class and register the functions there. | ||
The class will execute ``registerFunctions`` to register the default | ||
functions, you can override this to also add your own functions:: | ||
namespace Acme\AwesomeLib\ExpressionLanguage; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage; | ||
class ExpressionLanguage extends BaseExpressionLanguage | ||
{ | ||
protected function registerFunctions() | ||
{ | ||
parent::registerFunctions(); // do not forget to also register core functions | ||
$this->register('lowercase', function ($str) { | ||
if (!is_string($str)) { | ||
return $str; | ||
} | ||
return sprintf('strtolower(%s)', $str); | ||
}, function ($str) { | ||
if (!is_string($str)) { | ||
return $str; | ||
} | ||
return strtolower($str); | ||
}); | ||
} | ||
} |
1 change: 1 addition & 0 deletionscomponents/expression_language/index.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -6,3 +6,4 @@ Expression Language | ||
introduction | ||
syntax | ||
extending |
48 changes: 43 additions & 5 deletionscomponents/expression_language/syntax.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscomponents/map.rst.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.