From: Fabien Potencier Date: Fri, 23 Dec 2011 07:58:23 +0000 (+0100) Subject: Revert "merged branch arnaud-lb/hash-key-expr (PR #514)" X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=99b2c5bf99ea299685886e33d54fbbcd6348b849;p=web%2Fkonrad%2Ftwig.git Revert "merged branch arnaud-lb/hash-key-expr (PR #514)" This reverts commit 8665c6463461ccd0afec1ff943b45b56ad01a06e, reversing changes made to 4aba337cda03b0170cbf30b43cd82cb83146f2b4. --- diff --git a/CHANGELOG b/CHANGELOG index 77be635..bfbab34 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,5 @@ * 1.5.0 - * allowed hash keys to be any expression * added a do tag * added a flush tag * added support for dynamically named filters and functions diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index bd8ba78..2fc8306 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -198,7 +198,6 @@ class Twig_ExpressionParser $stream = $this->parser->getStream(); $stream->expect(Twig_Token::PUNCTUATION_TYPE, '[', 'An array element was expected'); $elements = array(); - $index = 0; while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) { if (!empty($elements)) { $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'An array element must be followed by a comma'); @@ -209,12 +208,7 @@ class Twig_ExpressionParser } } - $value = $this->parseExpression(); - $key = new Twig_Node_Expression_Constant($index, $value->getLine()); - - array_push($elements, $key, $value); - - $index += 1; + $elements[] = $this->parseExpression(); } $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']', 'An opened array is not properly closed'); @@ -236,11 +230,14 @@ class Twig_ExpressionParser } } - $key = $this->parseExpression(); - $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)'); - $value = $this->parseExpression(); + if (!$stream->test(Twig_Token::STRING_TYPE) && !$stream->test(Twig_Token::NUMBER_TYPE)) { + $current = $stream->getCurrent(); + throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string or a number (unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($current->getType(), $current->getLine()), $current->getValue()), $current->getLine()); + } - array_push($elements, $key, $value); + $key = $stream->next()->getValue(); + $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)'); + $elements[$key] = $this->parseExpression(); } $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed'); diff --git a/lib/Twig/Node/Expression/Array.php b/lib/Twig/Node/Expression/Array.php index 648c6d4..2d86082 100644 --- a/lib/Twig/Node/Expression/Array.php +++ b/lib/Twig/Node/Expression/Array.php @@ -15,20 +15,6 @@ class Twig_Node_Expression_Array extends Twig_Node_Expression parent::__construct($elements, array(), $lineno); } - public function getKeyValuePairs() - { - $pairs = array(); - - foreach (array_chunk($this->nodes, 2) as $pair) { - $pairs[] = array( - 'key' => $pair[0], - 'value' => $pair[1], - ); - } - - return $pairs; - } - /** * Compiles the node to PHP. * @@ -38,16 +24,16 @@ class Twig_Node_Expression_Array extends Twig_Node_Expression { $compiler->raw('array('); $first = true; - foreach ($this->getKeyValuePairs() as $pair) { + foreach ($this->nodes as $name => $node) { if (!$first) { $compiler->raw(', '); } $first = false; $compiler - ->subcompile($pair['key']) + ->repr($name) ->raw(' => ') - ->subcompile($pair['value']) + ->subcompile($node) ; } $compiler->raw(')'); diff --git a/test/Twig/Tests/ExpressionParserTest.php b/test/Twig/Tests/ExpressionParserTest.php index bb7b38a..ae5acb7 100644 --- a/test/Twig/Tests/ExpressionParserTest.php +++ b/test/Twig/Tests/ExpressionParserTest.php @@ -64,6 +64,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase { return array( array('{{ [1, "a": "b"] }}'), + array('{{ {a: "b"} }}'), array('{{ {"a": "b", 2} }}'), ); } @@ -73,9 +74,6 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase return array( // simple array array('{{ [1, 2] }}', new Twig_Node_Expression_Array(array( - new Twig_Node_Expression_Constant(0, 1), - new Twig_Node_Expression_Constant(1, 1), - new Twig_Node_Expression_Constant(1, 1), new Twig_Node_Expression_Constant(2, 1), ), 1), @@ -83,9 +81,6 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase // array with trailing , array('{{ [1, 2, ] }}', new Twig_Node_Expression_Array(array( - new Twig_Node_Expression_Constant(0, 1), - new Twig_Node_Expression_Constant(1, 1), - new Twig_Node_Expression_Constant(1, 1), new Twig_Node_Expression_Constant(2, 1), ), 1), @@ -93,52 +88,35 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase // simple hash array('{{ {"a": "b", "b": "c"} }}', new Twig_Node_Expression_Array(array( - new Twig_Node_Expression_Constant('a', 1), - new Twig_Node_Expression_Constant('b', 1), - - new Twig_Node_Expression_Constant('b', 1), - new Twig_Node_Expression_Constant('c', 1), + 'a' => new Twig_Node_Expression_Constant('b', 1), + 'b' => new Twig_Node_Expression_Constant('c', 1), ), 1), ), // hash with trailing , array('{{ {"a": "b", "b": "c", } }}', new Twig_Node_Expression_Array(array( - new Twig_Node_Expression_Constant('a', 1), - new Twig_Node_Expression_Constant('b', 1), - - new Twig_Node_Expression_Constant('b', 1), - new Twig_Node_Expression_Constant('c', 1), + 'a' => new Twig_Node_Expression_Constant('b', 1), + 'b' => new Twig_Node_Expression_Constant('c', 1), ), 1), ), // hash in an array array('{{ [1, {"a": "b", "b": "c"}] }}', new Twig_Node_Expression_Array(array( - new Twig_Node_Expression_Constant(0, 1), - new Twig_Node_Expression_Constant(1, 1), - new Twig_Node_Expression_Constant(1, 1), new Twig_Node_Expression_Array(array( - new Twig_Node_Expression_Constant('a', 1), - new Twig_Node_Expression_Constant('b', 1), - - new Twig_Node_Expression_Constant('b', 1), - new Twig_Node_Expression_Constant('c', 1), + 'a' => new Twig_Node_Expression_Constant('b', 1), + 'b' => new Twig_Node_Expression_Constant('c', 1), ), 1), ), 1), ), // array in a hash array('{{ {"a": [1, 2], "b": "c"} }}', new Twig_Node_Expression_Array(array( - new Twig_Node_Expression_Constant('a', 1), - new Twig_Node_Expression_Array(array( - new Twig_Node_Expression_Constant(0, 1), - new Twig_Node_Expression_Constant(1, 1), - + 'a' => new Twig_Node_Expression_Array(array( new Twig_Node_Expression_Constant(1, 1), new Twig_Node_Expression_Constant(2, 1), ), 1), - new Twig_Node_Expression_Constant('b', 1), - new Twig_Node_Expression_Constant('c', 1), + 'b' => new Twig_Node_Expression_Constant('c', 1), ), 1), ), ); diff --git a/test/Twig/Tests/Fixtures/expressions/array.test b/test/Twig/Tests/Fixtures/expressions/array.test index 5b81a0e..e505ca4 100644 --- a/test/Twig/Tests/Fixtures/expressions/array.test +++ b/test/Twig/Tests/Fixtures/expressions/array.test @@ -27,13 +27,6 @@ Twig supports array notation 2, ]|join(',') }} - -{# keys can be any expression #} -{% set a = 1 %} -{% set b = "foo" %} -{% set ary = { a: 'a', b: 'b', 'c': 'c', a~b: 'd' } %} -{{ ary|keys|join(',') }} -{{ ary|join(',') }} --DATA-- return array('bar' => 'bar', 'foo' => array('bar' => 'bar')) --EXPECT-- @@ -50,6 +43,3 @@ bar FOO,BAR, 1,2 - -1,foo,c,1foo -a,b,c,d diff --git a/test/Twig/Tests/Node/Expression/ArrayTest.php b/test/Twig/Tests/Node/Expression/ArrayTest.php index 06b30e9..1048cf4 100644 --- a/test/Twig/Tests/Node/Expression/ArrayTest.php +++ b/test/Twig/Tests/Node/Expression/ArrayTest.php @@ -18,10 +18,10 @@ class Twig_Tests_Node_Expression_ArrayTest extends Twig_Tests_Node_TestCase */ public function testConstructor() { - $elements = array(new Twig_Node_Expression_Constant('foo', 0), $foo = new Twig_Node_Expression_Constant('bar', 0)); + $elements = array('foo' => $foo = new Twig_Node_Expression_Constant('bar', 0)); $node = new Twig_Node_Expression_Array($elements, 0); - $this->assertEquals($foo, $node->getNode(1)); + $this->assertEquals($foo, $node->getNode('foo')); } /** @@ -36,11 +36,8 @@ class Twig_Tests_Node_Expression_ArrayTest extends Twig_Tests_Node_TestCase public function getTests() { $elements = array( - new Twig_Node_Expression_Constant('foo', 0), - new Twig_Node_Expression_Constant('bar', 0), - - new Twig_Node_Expression_Constant('bar', 0), - new Twig_Node_Expression_Constant('foo', 0), + 'foo' => new Twig_Node_Expression_Constant('bar', 0), + 'bar' => new Twig_Node_Expression_Constant('foo', 0), ); $node = new Twig_Node_Expression_Array($elements, 0); diff --git a/test/Twig/Tests/Node/IncludeTest.php b/test/Twig/Tests/Node/IncludeTest.php index d068392..9d25ff2 100644 --- a/test/Twig/Tests/Node/IncludeTest.php +++ b/test/Twig/Tests/Node/IncludeTest.php @@ -25,7 +25,7 @@ class Twig_Tests_Node_IncludeTest extends Twig_Tests_Node_TestCase $this->assertEquals($expr, $node->getNode('expr')); $this->assertFalse($node->getAttribute('only')); - $vars = new Twig_Node_Expression_Array(array(new Twig_Node_Expression_Constant('foo', 0), new Twig_Node_Expression_Constant(true, 0)), 0); + $vars = new Twig_Node_Expression_Array(array('foo' => new Twig_Node_Expression_Constant(true, 0)), 0); $node = new Twig_Node_Include($expr, $vars, true, false, 0); $this->assertEquals($vars, $node->getNode('variables')); $this->assertTrue($node->getAttribute('only')); @@ -62,7 +62,7 @@ EOF ); $expr = new Twig_Node_Expression_Constant('foo.twig', 0); - $vars = new Twig_Node_Expression_Array(array(new Twig_Node_Expression_Constant('foo', 0), new Twig_Node_Expression_Constant(true, 0)), 0); + $vars = new Twig_Node_Expression_Array(array('foo' => new Twig_Node_Expression_Constant(true, 0)), 0); $node = new Twig_Node_Include($expr, $vars, false, false, 0); $tests[] = array($node, '$this->env->loadTemplate("foo.twig")->display(array_merge($context, array("foo" => true)));');