Skip to content

Commit

Permalink
bug #21722 [ExpressionLanguage] Registering functions after calling e…
Browse files Browse the repository at this point in the history
…valuate(), 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
  • Loading branch information
fabpot committed Feb 22, 2017
2 parents b675d05 + e305369 commit fcb83a8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ public function parse($expression, $names)
* @param callable $compiler A callable able to compile the function
* @param callable $evaluator A callable able to evaluate the function
*
* @throws \LogicException when registering a function after calling evaluate(), compile() or parse()
*
* @see ExpressionFunction
*/
public function register($name, $compiler, $evaluator)
{
if (null !== $this->parser) {
throw new \LogicException('Registering functions after calling evaluate(), compile() or parse() is not supported.');
}

$this->functions[$name] = array('compiler' => $compiler, 'evaluator' => $evaluator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\ExpressionLanguage\Tests;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;
Expand Down Expand Up @@ -139,4 +140,58 @@ public function testCachingWithDifferentNamesOrder()
$expressionLanguage->compile($expression, array('a', 'B' => 'b'));
$expressionLanguage->compile($expression, array('B' => 'b', 'a'));
}

/**
* @dataProvider getRegisterCallbacks
* @expectedException \LogicException
*/
public function testRegisterAfterParse($registerCallback)
{
$el = new ExpressionLanguage();
$el->parse('1 + 1', array());
$registerCallback($el);
}

/**
* @dataProvider getRegisterCallbacks
* @expectedException \LogicException
*/
public function testRegisterAfterEval($registerCallback)
{
$el = new ExpressionLanguage();
$el->evaluate('1 + 1');
$registerCallback($el);
}

/**
* @dataProvider getRegisterCallbacks
* @expectedException \LogicException
*/
public function testRegisterAfterCompile($registerCallback)
{
$el = new ExpressionLanguage();
$el->compile('1 + 1');
$registerCallback($el);
}

public function getRegisterCallbacks()
{
return array(
array(
function (ExpressionLanguage $el) {
$el->register('fn', function () {}, function () {});
},
),
array(
function (ExpressionLanguage $el) {
$el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
},
),
array(
function (ExpressionLanguage $el) {
$el->registerProvider(new TestProvider());
},
),
);
}
}

0 comments on commit fcb83a8

Please sign in to comment.