From 698b522eda233a185826bb3c53895c0ce6213144 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 12 Jun 2010 17:28:34 +0200 Subject: [PATCH] made a small refactoring --- lib/Twig/Node.php | 11 +++++++++++ lib/Twig/Parser.php | 35 +++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/Twig/Node.php b/lib/Twig/Node.php index 488918d..fe0264b 100644 --- a/lib/Twig/Node.php +++ b/lib/Twig/Node.php @@ -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(); diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 56e2f3e..5d1f1be 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -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()); + } + } + } } -- 1.7.2.5