From b00930fd7f1527a99d01c5e0398e178e640573b6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 21 Sep 2016 11:29:47 -0700 Subject: [PATCH] [ExpressionLanguage] fixed a BC break --- .../ExpressionLanguage/Node/ConstantNode.php | 9 +++++++-- .../ExpressionLanguage/Node/GetAttrNode.php | 8 ++++---- src/Symfony/Component/ExpressionLanguage/Parser.php | 2 +- .../Tests/Node/ConstantNodeTest.php | 1 + .../Tests/Node/GetAttrNodeTest.php | 8 ++++---- .../ExpressionLanguage/Tests/ParserTest.php | 12 +++++------- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Component/ExpressionLanguage/Node/ConstantNode.php b/src/Symfony/Component/ExpressionLanguage/Node/ConstantNode.php index b5f51d340a51f..733d481b28183 100644 --- a/src/Symfony/Component/ExpressionLanguage/Node/ConstantNode.php +++ b/src/Symfony/Component/ExpressionLanguage/Node/ConstantNode.php @@ -20,8 +20,11 @@ */ class ConstantNode extends Node { - public function __construct($value) + private $isIdentifier; + + public function __construct($value, $isIdentifier = false) { + $this->isIdentifier = $isIdentifier; parent::__construct( array(), array('value' => $value) @@ -43,7 +46,9 @@ public function toArray() $array = array(); $value = $this->attributes['value']; - if (true === $value) { + if ($this->isIdentifier) { + $array[] = $value; + } elseif (true === $value) { $array[] = 'true'; } elseif (false === $value) { $array[] = 'false'; diff --git a/src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php b/src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php index 7cd7361c377de..c5c99d78ca7bf 100644 --- a/src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php +++ b/src/Symfony/Component/ExpressionLanguage/Node/GetAttrNode.php @@ -39,7 +39,7 @@ public function compile(Compiler $compiler) $compiler ->compile($this->nodes['node']) ->raw('->') - ->raw($this->nodes['attribute']->attributes['name']) + ->raw($this->nodes['attribute']->attributes['value']) ; break; @@ -47,7 +47,7 @@ public function compile(Compiler $compiler) $compiler ->compile($this->nodes['node']) ->raw('->') - ->raw($this->nodes['attribute']->attributes['name']) + ->raw($this->nodes['attribute']->attributes['value']) ->raw('(') ->compile($this->nodes['arguments']) ->raw(')') @@ -73,7 +73,7 @@ public function evaluate($functions, $values) throw new \RuntimeException('Unable to get a property on a non-object.'); } - $property = $this->nodes['attribute']->attributes['name']; + $property = $this->nodes['attribute']->attributes['value']; return $obj->$property; @@ -83,7 +83,7 @@ public function evaluate($functions, $values) throw new \RuntimeException('Unable to get a property on a non-object.'); } - return call_user_func_array(array($obj, $this->nodes['attribute']->attributes['name']), $this->nodes['arguments']->evaluate($functions, $values)); + return call_user_func_array(array($obj, $this->nodes['attribute']->attributes['value']), $this->nodes['arguments']->evaluate($functions, $values)); case self::ARRAY_CALL: $array = $this->nodes['node']->evaluate($functions, $values); diff --git a/src/Symfony/Component/ExpressionLanguage/Parser.php b/src/Symfony/Component/ExpressionLanguage/Parser.php index e4ebee1dd27eb..f8900bfb12916 100644 --- a/src/Symfony/Component/ExpressionLanguage/Parser.php +++ b/src/Symfony/Component/ExpressionLanguage/Parser.php @@ -330,7 +330,7 @@ public function parsePostfixExpression($node) throw new SyntaxError('Expected name', $token->cursor); } - $arg = new Node\NameNode($token->value); + $arg = new Node\ConstantNode($token->value, true); $arguments = new Node\ArgumentsNode(); if ($this->stream->current->test(Token::PUNCTUATION_TYPE, '(')) { diff --git a/src/Symfony/Component/ExpressionLanguage/Tests/Node/ConstantNodeTest.php b/src/Symfony/Component/ExpressionLanguage/Tests/Node/ConstantNodeTest.php index 1ba8ea96c6b95..af79a91c5c9c4 100644 --- a/src/Symfony/Component/ExpressionLanguage/Tests/Node/ConstantNodeTest.php +++ b/src/Symfony/Component/ExpressionLanguage/Tests/Node/ConstantNodeTest.php @@ -50,6 +50,7 @@ public function getDumpData() array('3', new ConstantNode(3)), array('3.3', new ConstantNode(3.3)), array('"foo"', new ConstantNode('foo')), + array('foo', new ConstantNode('foo', true)), array('{0: 1, "b": "a", 1: true}', new ConstantNode(array(1, 'b' => 'a', true))), array('{"a\\"b": "c", "a\\\\b": "d"}', new ConstantNode(array('a"b' => 'c', 'a\\b' => 'd'))), array('["c", "d"]', new ConstantNode(array('c', 'd'))), diff --git a/src/Symfony/Component/ExpressionLanguage/Tests/Node/GetAttrNodeTest.php b/src/Symfony/Component/ExpressionLanguage/Tests/Node/GetAttrNodeTest.php index de7177a805d0b..cf7cd7a03be52 100644 --- a/src/Symfony/Component/ExpressionLanguage/Tests/Node/GetAttrNodeTest.php +++ b/src/Symfony/Component/ExpressionLanguage/Tests/Node/GetAttrNodeTest.php @@ -24,9 +24,9 @@ public function getEvaluateData() array('b', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), $this->getArrayNode(), GetAttrNode::ARRAY_CALL), array('foo' => array('b' => 'a', 'b'))), array('a', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL), array('foo' => array('b' => 'a', 'b'))), - array('bar', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())), + array('bar', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())), - array('baz', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())), + array('baz', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())), array('a', new GetAttrNode(new NameNode('foo'), new NameNode('index'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL), array('foo' => array('b' => 'a', 'b'), 'index' => 'b')), ); } @@ -37,9 +37,9 @@ public function getCompileData() array('$foo[0]', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)), array('$foo["b"]', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)), - array('$foo->foo', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())), + array('$foo->foo', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::PROPERTY_CALL), array('foo' => new Obj())), - array('$foo->foo(array("b" => "a", 0 => "b"))', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())), + array('$foo->foo(array("b" => "a", 0 => "b"))', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), $this->getArrayNode(), GetAttrNode::METHOD_CALL), array('foo' => new Obj())), array('$foo[$index]', new GetAttrNode(new NameNode('foo'), new NameNode('index'), $this->getArrayNode(), GetAttrNode::ARRAY_CALL)), ); } diff --git a/src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php b/src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php index 8f5a3ce11fb41..ae9b2910a27f3 100644 --- a/src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php +++ b/src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php @@ -98,24 +98,24 @@ public function getParseData() '(3 - 3) * 2', ), array( - new Node\GetAttrNode(new Node\NameNode('foo'), new Node\NameNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::PROPERTY_CALL), + new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('bar', true), new Node\ArgumentsNode(), Node\GetAttrNode::PROPERTY_CALL), 'foo.bar', array('foo'), ), array( - new Node\GetAttrNode(new Node\NameNode('foo'), new Node\NameNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL), + new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('bar', true), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL), 'foo.bar()', array('foo'), ), array( - new Node\GetAttrNode(new Node\NameNode('foo'), new Node\NameNode('not'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL), + new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('not', true), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL), 'foo.not()', array('foo'), ), array( new Node\GetAttrNode( new Node\NameNode('foo'), - new Node\NameNode('bar'), + new Node\ConstantNode('bar', true), $arguments, Node\GetAttrNode::METHOD_CALL ), @@ -159,9 +159,7 @@ public function getParseData() private function createGetAttrNode($node, $item, $type) { - $attr = Node\GetAttrNode::ARRAY_CALL === $type ? new Node\ConstantNode($item) : new Node\NameNode($item); - - return new Node\GetAttrNode($node, $attr, new Node\ArgumentsNode(), $type); + return new Node\GetAttrNode($node, new Node\ConstantNode($item, Node\GetAttrNode::ARRAY_CALL !== $type), new Node\ArgumentsNode(), $type); } /**