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

Commit6e2e583

Browse files
committed
Added article about custom functions
1 parent0725a8b commit6e2e583

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
..index::
2+
single: Extending; ExpressionLanguage
3+
4+
Extending the ExpressionLanguage
5+
================================
6+
7+
The ExpressionLanguage can be extended by adding custom functions. For
8+
instance, in the framework, the security has custom functions to check the
9+
user's role.
10+
11+
..note::
12+
13+
If you want to learn how to use functions in an expression, read
14+
":ref:`component-expression-functions`".
15+
16+
Register Functions
17+
------------------
18+
19+
Functions will be registered on the current ``ExpressionLanguage`` instance.
20+
That means the functions can be used in any expression executed by that
21+
instance.
22+
23+
To register a function, use
24+
:method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::register``.
25+
This method has 3 arguments:
26+
27+
* **name** - The name of the function in an expression;
28+
* **compiler** - A function executed when compiling an expression using the
29+
function;
30+
* **evaluator** - A function executed when the expression is evaluated.
31+
32+
..code-block::php
33+
34+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
35+
36+
$language = new ExpressionLanguage();
37+
$language->register('lowercase', function ($str) {
38+
if (!is_string($str)) {
39+
return $str;
40+
}
41+
42+
return sprintf('strtolower(%s)', $str);
43+
}, function ($str) {
44+
if (!is_string($str)) {
45+
return $str;
46+
}
47+
48+
return strtolower($str);
49+
});
50+
51+
echo $language->evaluate('lowercase("HELLO")');
52+
53+
This will print ``hello``.
54+
55+
Creating a new ExpressionLanguage class
56+
---------------------------------------
57+
58+
When you use the ``ExpressionLanguage`` class in your library, it's recommend
59+
to create a new ``ExpressionLanguage`` class and register the functions there.
60+
The class will execute ``registerFunctions`` to register the default
61+
functions, you can override this to also add your own functions::
62+
63+
namespace Acme\AwesomeLib\ExpressionLanguage;
64+
65+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage;
66+
67+
class ExpressionLanguage extends BaseExpressionLanguage
68+
{
69+
protected function registerFunctions()
70+
{
71+
parent::registerFunctions(); // do not forget to also register core functions
72+
73+
$this->register('lowercase', function ($str) {
74+
if (!is_string($str)) {
75+
return $str;
76+
}
77+
78+
return sprintf('strtolower(%s)', $str);
79+
}, function ($str) {
80+
if (!is_string($str)) {
81+
return $str;
82+
}
83+
84+
return strtolower($str);
85+
});
86+
}
87+
}

‎components/expression_language/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Expression Language
66

77
introduction
88
syntax
9+
extending

‎components/expression_language/syntax.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ constant::
9999

100100
This will print ``root``.
101101

102+
..tip::
103+
104+
To read how to register your own function to use in an expression, see
105+
":doc:`/components/expression_language/extending`".
106+
102107
.. _component-expression-arrays:
103108

104109
Working with Arrays

‎components/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
* :doc:`/components/expression_language/introduction`
6363
* :doc:`/components/expression_language/syntax`
64+
* :doc:`/components/expression_language/extending`
6465

6566
***Filesystem**
6667

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp