From 6d39e22499e575f14dcda93b63332f8011b9f2e5 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 26 May 2010 07:49:34 +0200 Subject: [PATCH] added two interfaces: Twig_NodeInterface and Twig_TokenParserInterface --- CHANGELOG | 1 + lib/Twig/Compiler.php | 10 +++++----- lib/Twig/CompilerInterface.php | 4 ++-- lib/Twig/Environment.php | 2 +- lib/Twig/Node.php | 6 ++---- lib/Twig/Node/Expression/Binary.php | 2 +- lib/Twig/Node/Expression/Filter.php | 2 +- lib/Twig/Node/Expression/GetAttr.php | 2 +- lib/Twig/Node/Expression/Unary.php | 2 +- lib/Twig/NodeInterface.php | 26 ++++++++++++++++++++++++++ lib/Twig/NodeTraverser.php | 2 +- lib/Twig/NodeVisitor/Escaper.php | 6 +++--- lib/Twig/NodeVisitor/Filter.php | 6 +++--- lib/Twig/NodeVisitor/Sandbox.php | 10 +++++----- lib/Twig/NodeVisitorInterface.php | 4 ++-- lib/Twig/TokenParser.php | 7 +------ lib/Twig/TokenParserInterface.php | 18 ++++++++++++++++++ 17 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 lib/Twig/NodeInterface.php create mode 100644 lib/Twig/TokenParserInterface.php diff --git a/CHANGELOG b/CHANGELOG index 3caf8dd..c80757f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ Backward incompatibilities: * The short notation of the `block` tag changed. + * added two interfaces: Twig_NodeInterface and Twig_TokenParserInterface * changed the generated code to match the new coding standards * fixed sandbox mode (__toString() method check was not enforced if called implicitly from a simple statement like {{ article }}) * added a 'as' string to the block tag short notation ({% block title "Title" %} must now be {% block title as "Title" %}) diff --git a/lib/Twig/Compiler.php b/lib/Twig/Compiler.php index 5b7e724..9abc90e 100644 --- a/lib/Twig/Compiler.php +++ b/lib/Twig/Compiler.php @@ -52,11 +52,11 @@ class Twig_Compiler implements Twig_CompilerInterface /** * Compiles a node. * - * @param Twig_Node $node The node to compile + * @param Twig_NodeInterface $node The node to compile * * @return Twig_Compiler The current compiler instance */ - public function compile(Twig_Node $node) + public function compile(Twig_NodeInterface $node) { $this->lastLine = null; $this->source = ''; @@ -67,7 +67,7 @@ class Twig_Compiler implements Twig_CompilerInterface return $this; } - public function subcompile(Twig_Node $node) + public function subcompile(Twig_NodeInterface $node) { $node->compile($this); @@ -154,11 +154,11 @@ class Twig_Compiler implements Twig_CompilerInterface /** * Adds debugging information. * - * @param Twig_Node $node The related twig node + * @param Twig_NodeInterface $node The related twig node * * @return Twig_Compiler The current compiler instance */ - public function addDebugInfo(Twig_Node $node) + public function addDebugInfo(Twig_NodeInterface $node) { if ($node->getLine() != $this->lastLine) { $this->lastLine = $node->getLine(); diff --git a/lib/Twig/CompilerInterface.php b/lib/Twig/CompilerInterface.php index 7d1ad8a..27c448a 100644 --- a/lib/Twig/CompilerInterface.php +++ b/lib/Twig/CompilerInterface.php @@ -21,11 +21,11 @@ interface Twig_CompilerInterface /** * Compiles a node. * - * @param Twig_Node $node The node to compile + * @param Twig_NodeInterface $node The node to compile * * @return Twig_Compiler The current compiler instance */ - public function compile(Twig_Node $node); + public function compile(Twig_NodeInterface $node); /** * Gets the current PHP code after compilation. diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 8462318..d7acfd7 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -254,7 +254,7 @@ class Twig_Environment $compiler->setEnvironment($this); } - public function compile(Twig_Node $node) + public function compile(Twig_NodeInterface $node) { return $this->getCompiler()->compile($node)->getSource(); } diff --git a/lib/Twig/Node.php b/lib/Twig/Node.php index 538e202..4762819 100644 --- a/lib/Twig/Node.php +++ b/lib/Twig/Node.php @@ -17,7 +17,7 @@ * @author Fabien Potencier * @version SVN: $Id$ */ -abstract class Twig_Node +abstract class Twig_Node implements Twig_NodeInterface { protected $lineno; protected $tag; @@ -33,14 +33,12 @@ abstract class Twig_Node return get_class($this).'()'; } - abstract public function compile($compiler); - public function getLine() { return $this->lineno; } - public function getTag() + public function getNodeTag() { return $this->tag; } diff --git a/lib/Twig/Node/Expression/Binary.php b/lib/Twig/Node/Expression/Binary.php index 41b1b3c..9af1635 100644 --- a/lib/Twig/Node/Expression/Binary.php +++ b/lib/Twig/Node/Expression/Binary.php @@ -14,7 +14,7 @@ abstract class Twig_Node_Expression_Binary extends Twig_Node_Expression protected $left; protected $right; - public function __construct(Twig_Node $left, Twig_Node $right, $lineno) + public function __construct(Twig_NodeInterface $left, Twig_NodeInterface $right, $lineno) { parent::__construct($lineno); $this->left = $left; diff --git a/lib/Twig/Node/Expression/Filter.php b/lib/Twig/Node/Expression/Filter.php index f8d63b0..3131714 100644 --- a/lib/Twig/Node/Expression/Filter.php +++ b/lib/Twig/Node/Expression/Filter.php @@ -14,7 +14,7 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression implements Twig_N protected $node; protected $filters; - public function __construct(Twig_Node $node, array $filters, $lineno, $tag = null) + public function __construct(Twig_NodeInterface $node, array $filters, $lineno, $tag = null) { parent::__construct($lineno, $tag); diff --git a/lib/Twig/Node/Expression/GetAttr.php b/lib/Twig/Node/Expression/GetAttr.php index 7c7c9cc..b8989dd 100644 --- a/lib/Twig/Node/Expression/GetAttr.php +++ b/lib/Twig/Node/Expression/GetAttr.php @@ -15,7 +15,7 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression implements Twig_ protected $attr; protected $arguments; - public function __construct(Twig_Node $node, $attr, $arguments, $lineno, $token_value) + public function __construct(Twig_NodeInterface $node, $attr, $arguments, $lineno, $token_value) { parent::__construct($lineno); $this->node = $node; diff --git a/lib/Twig/Node/Expression/Unary.php b/lib/Twig/Node/Expression/Unary.php index 416e846..d3fec09 100644 --- a/lib/Twig/Node/Expression/Unary.php +++ b/lib/Twig/Node/Expression/Unary.php @@ -13,7 +13,7 @@ abstract class Twig_Node_Expression_Unary extends Twig_Node_Expression { protected $node; - public function __construct(Twig_Node $node, $lineno) + public function __construct(Twig_NodeInterface $node, $lineno) { parent::__construct($lineno); $this->node = $node; diff --git a/lib/Twig/NodeInterface.php b/lib/Twig/NodeInterface.php new file mode 100644 index 0000000..9530913 --- /dev/null +++ b/lib/Twig/NodeInterface.php @@ -0,0 +1,26 @@ + + * @version SVN: $Id$ + */ +interface Twig_NodeInterface +{ + public function compile($compiler); + + public function getLine(); + + public function getNodeTag(); +} diff --git a/lib/Twig/NodeTraverser.php b/lib/Twig/NodeTraverser.php index bde3d4b..49aae44 100644 --- a/lib/Twig/NodeTraverser.php +++ b/lib/Twig/NodeTraverser.php @@ -17,7 +17,7 @@ class Twig_NodeTraverser $this->env = $env; } - public function traverse(Twig_Node $node, Twig_NodeVisitorInterface $visitor) + public function traverse(Twig_NodeInterface $node, Twig_NodeVisitorInterface $visitor) { $node = $visitor->enterNode($node, $this->env); diff --git a/lib/Twig/NodeVisitor/Escaper.php b/lib/Twig/NodeVisitor/Escaper.php index 4dc7559..be8c3ed 100644 --- a/lib/Twig/NodeVisitor/Escaper.php +++ b/lib/Twig/NodeVisitor/Escaper.php @@ -13,7 +13,7 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface protected $statusStack = array(); protected $blocks = array(); - public function enterNode(Twig_Node $node, Twig_Environment $env) + public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_AutoEscape) { $this->statusStack[] = $node->getValue(); @@ -26,7 +26,7 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface return $node; } - public function leaveNode(Twig_Node $node, Twig_Environment $env) + public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_AutoEscape || $node instanceof Twig_Node_Block) { array_pop($this->statusStack); @@ -37,7 +37,7 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface return $node; } - protected function escapeNode(Twig_Node $node, Twig_Environment $env, $type) + protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type) { if (false === $type) { return $node; diff --git a/lib/Twig/NodeVisitor/Filter.php b/lib/Twig/NodeVisitor/Filter.php index bd5006d..e37b673 100644 --- a/lib/Twig/NodeVisitor/Filter.php +++ b/lib/Twig/NodeVisitor/Filter.php @@ -12,7 +12,7 @@ class Twig_NodeVisitor_Filter implements Twig_NodeVisitorInterface { protected $statusStack = array(); - public function enterNode(Twig_Node $node, Twig_Environment $env) + public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Filter) { $this->statusStack[] = $node->getFilters(); @@ -23,7 +23,7 @@ class Twig_NodeVisitor_Filter implements Twig_NodeVisitorInterface return $node; } - public function leaveNode(Twig_Node $node, Twig_Environment $env) + public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Filter) { array_pop($this->statusStack); @@ -32,7 +32,7 @@ class Twig_NodeVisitor_Filter implements Twig_NodeVisitorInterface return $node; } - protected function applyFilters(Twig_Node $node) + protected function applyFilters(Twig_NodeInterface $node) { if (false === $filters = $this->getCurrentFilters()) { return $node; diff --git a/lib/Twig/NodeVisitor/Sandbox.php b/lib/Twig/NodeVisitor/Sandbox.php index 55f751d..d624f25 100644 --- a/lib/Twig/NodeVisitor/Sandbox.php +++ b/lib/Twig/NodeVisitor/Sandbox.php @@ -14,7 +14,7 @@ class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface protected $tags; protected $filters; - public function enterNode(Twig_Node $node, Twig_Environment $env) + public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $this->inAModule = true; @@ -24,8 +24,8 @@ class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface return $node; } elseif ($this->inAModule) { // look for tags - if ($node->getTag()) { - $this->tags[$node->getTag()] = true; + if ($node->getNodeTag()) { + $this->tags[$node->getNodeTag()] = true; } // look for filters @@ -37,14 +37,14 @@ class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface // look for simple print statement ({{ article }}) if ($node instanceof Twig_Node_Print && $node->getExpression() instanceof Twig_Node_Expression_Name) { - return new Twig_Node_SandboxPrint($node->getExpression(), $node->getLine(), $node->getTag()); + return new Twig_Node_SandboxPrint($node->getExpression(), $node->getLine(), $node->getNodeTag()); } } return $node; } - public function leaveNode(Twig_Node $node, Twig_Environment $env) + public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $node->setUsedFilters(array_keys($this->filters)); diff --git a/lib/Twig/NodeVisitorInterface.php b/lib/Twig/NodeVisitorInterface.php index d3c2f2f..898e3d1 100644 --- a/lib/Twig/NodeVisitorInterface.php +++ b/lib/Twig/NodeVisitorInterface.php @@ -2,7 +2,7 @@ interface Twig_NodeVisitorInterface { - public function enterNode(Twig_Node $node, Twig_Environment $env); + public function enterNode(Twig_NodeInterface $node, Twig_Environment $env); - public function leaveNode(Twig_Node $node, Twig_Environment $env); + public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env); } diff --git a/lib/Twig/TokenParser.php b/lib/Twig/TokenParser.php index 6f9ff0a..a18537f 100644 --- a/lib/Twig/TokenParser.php +++ b/lib/Twig/TokenParser.php @@ -4,12 +4,11 @@ * This file is part of Twig. * * (c) 2009 Fabien Potencier - * (c) 2009 Armin Ronacher * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -abstract class Twig_TokenParser +abstract class Twig_TokenParser implements Twig_TokenParserInterface { protected $parser; @@ -17,8 +16,4 @@ abstract class Twig_TokenParser { $this->parser = $parser; } - - abstract public function parse(Twig_Token $token); - - abstract public function getTag(); } diff --git a/lib/Twig/TokenParserInterface.php b/lib/Twig/TokenParserInterface.php new file mode 100644 index 0000000..77407e9 --- /dev/null +++ b/lib/Twig/TokenParserInterface.php @@ -0,0 +1,18 @@ +