8000 [ExpressionLanguage] Registering functions after calling evaluate(), compile() or parse() is not supported by maidmaid · Pull Request #21722 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ExpressionLanguage] Registering functions after calling evaluate(), compile() or parse() is not supported #21722

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
wants to merge 7 commits into from
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
Next Next commit
Throw exception
  • Loading branch information
maidmaid committed Feb 22, 2017
commit 463434a2159624bf98eccaaa8e91ae1ef4b08588
4 changes: 2 additions & 2 deletions src/Symfony/Component/ExpressionLanguage/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class Compiler
private $source;
private $functions;

public function __construct(array &$functions)
public function __construct(array $functions)
{
$this->functions = &$functions;
$this->functions = $functions;
}

public function getFunction($name)
Expand Down
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 register a function after calling evaluate/compilate the first time
*
* @see ExpressionFunction
*/
public function register($name, $compiler, $evaluator)
{
if ($this->parser) {
throw new \LogicException('It\' 8000 ;s impossible to register function after calling evaluate/compilate the first time.');
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/ExpressionLanguage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class Parser
private $functions;
private $names;

public function __construct(array &$functions)
public function __construct(array $functions)
{
$this->functions = &$functions;
$this->functions = $functions;

$this->unaryOperators = array(
'not' => array('precedence' => 50),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,23 @@ public function testCachingWithDifferentNamesOrder()
$expressionLanguage->compile($expression, array('B' => 'b', 'a'));
}

public function testAddingFunctionAfterEval()
/**
* @expectedException \LogicException
*/
public function testRegisterAfterEval()
{
$el = new ExpressionLanguage();
$el->evaluate('1 + 1');
$el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
$result = $el->evaluate('fn()');
$this->assertNull($result);
$el->register('fn', function () {}, function () {});
}

public function testAddingFunctionAfterCompile()
/**
* @expectedException \LogicException
*/
public function testRegisterAfterCompile()
{
$el = new ExpressionLanguage();
$el->compile('1 + 1');
$el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
$result = $el->compile('fn()');
$this->assertEmpty($result);
$el->register('fn', function () {}, function () {});
}
}
12 changes: 4 additions & 8 deletions src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class ParserTest extends TestCase
public function testParseWithInvalidName()
{
$lexer = new Lexer();
$functions = array();
$parser = new Parser($functions);
$parser = new Parser(array());
$parser->parse($lexer->tokenize('foo'));
}

Expand All @@ -37,8 +36,7 @@ public function testParseWithInvalidName()
public function testParseWithZeroInNames()
{
$lexer = new Lexer();
$functions = array();
$parser = new Parser($functions);
$parser = new Parser(array());
$parser->parse($lexer->tokenize('foo'), array(0));
}

Expand All @@ -48,8 +46,7 @@ public function testParseWithZeroInNames()
public function testParse($node, $expression, $names = array())
{
$lexer = new Lexer();
$functions = array();
$parser = new Parser($functions);
$parser = new Parser(array());
$this->assertEquals($node, $parser->parse($lexer->tokenize($expression), $names));
}

Expand Down Expand Up @@ -173,8 +170,7 @@ private function createGetAttrNode($node, $item, $type)
public function testParseWithInvalidPostfixData($expr, $names = array())
{
$lexer = new Lexer();
$functions = array();
$parser = new Parser($functions);
$parser = new Parser(array());
$parser->parse($lexer->tokenize($expr), $names);
}

Expand Down
0