added an error when defining two blocks with the same name in a template (closes...
authorFabien Potencier <fabien.potencier@gmail.com>
Fri, 20 Apr 2012 09:17:22 +0000 (11:17 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Fri, 20 Apr 2012 09:17:22 +0000 (11:17 +0200)
CHANGELOG
lib/Twig/Parser.php
lib/Twig/TokenParser/Block.php
test/Twig/Tests/Fixtures/tags/block/block_unique_name.test [new file with mode: 0644]

index 1613419..0badb9e 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,6 @@
 * 1.7.0 (2012-XX-XX)
 
+ * added an error when defining two blocks with the same name in a template
  * fixed a PHP notice when trying to access a key on a non-object/array variable
  * enhanced error reporting when the template file is an instance of SplFileInfo
  * added Twig_Environment::mergeGlobals()
index 72d3637..e8db493 100644 (file)
@@ -229,9 +229,14 @@ class Twig_Parser implements Twig_ParserInterface
         return isset($this->blocks[$name]);
     }
 
+    public function getBlock($name)
+    {
+        return $this->blocks[$name];
+    }
+
     public function setBlock($name, $value)
     {
-        $this->blocks[$name] = new Twig_Node_Body(array($value));
+        $this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getLine());
     }
 
     public function hasMacro($name)
index 36643f7..994078e 100644 (file)
@@ -35,8 +35,9 @@ class Twig_TokenParser_Block extends Twig_TokenParser
         $stream = $this->parser->getStream();
         $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
         if ($this->parser->hasBlock($name)) {
-            throw new Twig_Error_Syntax("The block '$name' has already been defined", $lineno);
+            throw new Twig_Error_Syntax(sprintf("The block '$name' has already been defined line %d", $this->parser->getBlock($name)->getLine()), $lineno);
         }
+        $this->parser->setBlock($name, $block = new Twig_Node_Block($name, new Twig_Node(array()), $lineno));
         $this->parser->pushLocalScope();
         $this->parser->pushBlockStack($name);
 
@@ -58,8 +59,7 @@ class Twig_TokenParser_Block extends Twig_TokenParser
         }
         $stream->expect(Twig_Token::BLOCK_END_TYPE);
 
-        $block = new Twig_Node_Block($name, $body, $lineno);
-        $this->parser->setBlock($name, $block);
+        $block->setNode('body', $body);
         $this->parser->popBlockStack();
         $this->parser->popLocalScope();
 
diff --git a/test/Twig/Tests/Fixtures/tags/block/block_unique_name.test b/test/Twig/Tests/Fixtures/tags/block/block_unique_name.test
new file mode 100644 (file)
index 0000000..5c205c0
--- /dev/null
@@ -0,0 +1,11 @@
+--TEST--
+"block" tag
+--TEMPLATE--
+{% block content %}
+    {% block content %}
+    {% endblock %}
+{% endblock %}
+--DATA--
+return array()
+--EXCEPTION--
+Twig_Error_Syntax: The block 'content' has already been defined line 2 in "index.twig" at line 3