8000 Removed references of 2.2 by wouterj · Pull Request #3240 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Removed references of 2.2 #3240

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added article about custom functions
  • Loading branch information
wouterj committed Nov 29, 2013
commit 95c9a4ed42e4a4f6f5e55614d4fbb1f91086e821
87 changes: 87 additions & 0 deletions components/expression_language/extending.rst
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);
});
}
}
5 changes: 5 additions & 0 deletions components/expression_language/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ constant::

This will print ``root``.

.. tip::

To read how to register your own function to use in an expression, see
":doc:`/components/expression_language/extending`".

.. _component-expression-arrays:

Working with Arrays
Expand Down
0