8000 bug #21722 [ExpressionLanguage] Registering functions after calling e… · symfony/symfony@fcb83a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit fcb83a8

Browse files
committed
bug #21722 [ExpressionLanguage] Registering functions after calling evaluate(), compile() or parse() is not supported (maidmaid)
This PR was squashed before being merged into the 2.7 branch (closes #21722). Discussion ---------- [ExpressionLanguage] Registering functions after calling evaluate(), compile() or parse() is not supported | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a If we add expr. function after first eval/compile like this: ```php $el = new ExpressionLanguage(); $el->evaluate('1 + 1'); $el->addFunction(new ExpressionFunction('fn', function () {}, function () {})); $el->evaluate('fn()'); ``` A ``SyntaxError`` is thrown that says ``The function "fn" does not exist around position 1.``. It's the same bug with ``$el->compile('fn()')``. This PR fixes this (duplicate of #21098 that was closed). Commits ------- e305369 [ExpressionLanguage] Registering functions after calling evaluate(), compile() or parse() is not supported
2 p
8000
arents b675d05 + e305369 commit fcb83a8

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,16 @@ public function parse($expression, $names)
110110
* @param callable $compiler A callable able to compile the function
111111
* @param callable $evaluator A callable able to evaluate the function
112112
*
113+
* @throws \LogicException when registering a function after calling evaluate(), compile() or parse()
114+
*
113115
* @see ExpressionFunction
114116
*/
115117
public function register($name, $compiler, $evaluator)
116118
{
119+
if (null !== $this->parser) {
120+
throw new \LogicException('Registering functions after calling evaluate(), compile() or parse() is not supported.');
121+
}
122+
117123
$this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
118124
}
119125

src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\ExpressionLanguage\Tests;
1313

14+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1617
use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;
@@ -139,4 +140,58 @@ public function testCachingWithDifferentNamesOrder()
139140
$expressionLanguage->compile($expression, array('a', 'B' => 'b'));
140141
$expressionLanguage->compile($expression, array('B' => 'b', 'a'));
141142
}
143+
144+
/**
145+
* @dataProvider getRegisterCallbacks
146+
* @expectedException \LogicException
147+
*/
148+
public function testRegisterAfterParse($registerCallback)
149+
{
150+
$el = new ExpressionLanguage();
151+
$el->parse('1 + 1', array());
152+
$registerCallback($el);
153+
}
154+
155+
/**
156+
* @dataProvider getRegisterCallbacks
157+
* @expectedException \LogicException
158+
*/
159+
public function testRegisterAfterEval($registerCallback)
160+
{
161+
$el = new ExpressionLanguage();
162+
$el->evaluate('1 + 1');
163+
$registerCallback($el);
164+
}
165+
166+
/**
167+
* @dataProvider getRegisterCallbacks
168+
* @expectedException \LogicException
169+
*/
170+
public function testRegisterAfterCompile($registerCallback)
171+
{
172+
$el = new ExpressionLanguage();
173+
$el->compile('1 + 1');
174+
$registerCallback($el);
175+
}
176+
177+
public function getRegisterCallbacks()
178+
{
179+
return array(
180+
array(
181+
function (ExpressionLanguage $el) {
182+
$el->register('fn', function () {}, function () {});
183+
},
184+
),
185+
array(
186+
function (ExpressionLanguage $el) {
187+
$el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
188+
},
189+
),
190+
array(
191+
function (ExpressionLanguage $el) {
192+
$el->registerProvider(new TestProvider());
193+
},
194+
),
195+
);
196+
}
142197
}

0 commit comments

Comments
 (0)
0