made a small refactoring
authorFabien Potencier <fabien.potencier@gmail.com>
Sat, 12 Jun 2010 15:28:34 +0000 (17:28 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Sat, 12 Jun 2010 15:28:34 +0000 (17:28 +0200)
lib/Twig/Node.php
lib/Twig/Parser.php

index 488918d..fe0264b 100644 (file)
@@ -24,6 +24,17 @@ class Twig_Node implements Twig_NodeInterface, ArrayAccess, Countable, Iterator
     protected $lineno;
     protected $tag;
 
+    /**
+     * Constructor.
+     *
+     * The nodes are automatically made available as properties ($this->node).
+     * The attributes are automatically made available as array items ($this['name']).
+     *
+     * @param array   $nodes      An array of named nodes
+     * @param array   $attributes An array of attributes (should not be nodes)
+     * @param integer $lineno     The line number
+     * @param string  $tag        The tag name associated with the Node
+     */
     public function __construct(array $nodes = array(), array $attributes = array(), $lineno = 0, $tag = null)
     {
         $this->nodes = array();
index 56e2f3e..5d1f1be 100644 (file)
@@ -42,10 +42,8 @@ class Twig_Parser implements Twig_ParserInterface
      */
     public function parse(Twig_TokenStream $stream)
     {
-        $this->handlers = array();
-        $this->visitors = array();
-
         // tag handlers
+        $this->handlers = array();
         foreach ($this->env->getTokenParsers() as $handler) {
             $handler->setParser($this);
 
@@ -76,17 +74,7 @@ class Twig_Parser implements Twig_ParserInterface
         }
 
         if (null !== $this->parent) {
-            // check that the body only contains block references and empty text nodes
-            foreach ($body as $node)
-            {
-                if (
-                    ($node instanceof Twig_Node_Text && !preg_match('/^\s*$/s', $node['data']))
-                    ||
-                    (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference)
-                ) {
-                    throw new Twig_SyntaxError('A template that extends another one cannot have a body', $node->getLine(), $this->stream->getFilename());
-                }
-            }
+            $this->checkBodyNodes($body);
         }
 
         $node = new Twig_Node_Module($body, $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), $this->stream->getFilename());
@@ -96,7 +84,7 @@ class Twig_Parser implements Twig_ParserInterface
         return $traverser->traverse($node);
     }
 
-    public function subparse($test, $drop_needle = false)
+    public function subparse($test, $dropNeedle = false)
     {
         $lineno = $this->getCurrentToken()->getLine();
         $rv = array();
@@ -123,7 +111,7 @@ class Twig_Parser implements Twig_ParserInterface
                     }
 
                     if (!is_null($test) && call_user_func($test, $token)) {
-                        if ($drop_needle) {
+                        if ($dropNeedle) {
                             $this->stream->next();
                         }
 
@@ -225,4 +213,19 @@ class Twig_Parser implements Twig_ParserInterface
     {
         return $this->stream->getCurrent();
     }
+
+    protected function checkBodyNodes($body)
+    {
+        // check that the body only contains block references and empty text nodes
+        foreach ($body as $node)
+        {
+            if (
+                ($node instanceof Twig_Node_Text && !preg_match('/^\s*$/s', $node['data']))
+                ||
+                (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference)
+            ) {
+                throw new Twig_SyntaxError('A template that extends another one cannot have a body', $node->getLine(), $this->stream->getFilename());
+            }
+        }
+    }
 }