Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Merged
Show file tree
Hide file tree
Changes fromall commits
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
87 changes: 87 additions & 0 deletionscomponents/expression_language/extending.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,3 +6,4 @@ Expression Language

introduction
syntax
extending
48 changes: 43 additions & 5 deletionscomponents/expression_language/syntax.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -81,6 +81,29 @@ #"687662cdc0af9b27fb2a4aea073382249fed7b5dd61682c6a3ae4f29c7961bd4">
This will print ``Hi Hi Hi!``.

.. _component-expression-functions:

Working with Functions
----------------------

You can also use registered functions in the expression by using the same
syntax as PHP and JavaScript. The ExpressionLanguage component comes with one
function by default: ``constant()`` Which will return the value of the PHP
constant::

define('DB_USER', 'root');

echo $language->evaluate(
'constant("DB_USER")'
);

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 DownExpand Up@@ -128,11 +151,6 @@ For example::

This will print out ``42``.

Assignment Operators
~~~~~~~~~~~~~~~~~~~~

* ``=``

Bitwise Operators
~~~~~~~~~~~~~~~~~

Expand DownExpand Up@@ -249,6 +267,26 @@ Numeric Operators

* ``..`` (range)

For example::

class User
{
public $age;
}

$user = new User();
$user->age = 34;

$language->evaluate(
'user.age in 18..45',
array(
'user' => $user,
)
);

This will evaluate to ``true``, because ``user.age`` is in the range from
``18`` till ``45``

Ternary Operators
~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletionscomponents/map.rst.inc
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,7 @@

* :doc:`/components/expression_language/introduction`
* :doc:`/components/expression_language/syntax`
* :doc:`/components/expression_language/extending`

* **Filesystem**

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp