diff --git a/CHANGELOG b/CHANGELOG index 71228b784..9379b82b6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ -* 1.19.1 (2015-XX-XX) +* 1.20.0 (2015-XX-XX) - * n/a + * added Twig_BaseNodeVisitor to ease the compatibility of node visitors + between 1.x and 2.x * 1.19.0 (2015-07-31) diff --git a/composer.json b/composer.json index efb2fa1f5..14f59c38a 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.19-dev" + "dev-master": "1.20-dev" } } } diff --git a/doc/deprecated.rst b/doc/deprecated.rst index 9c4d4d376..beac49eac 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -107,6 +107,13 @@ Loaders * As of Twig 1.x, ``Twig_Loader_String`` is deprecated and will be removed in 2.0. +Node Visitors +------------- + +* Because of the removal of ``Twig_NodeInterface`` in 2.0, you need to extend + ``Twig_BaseNodeVistor`` instead of implementing ``Twig_NodeVisitorInterface`` + directly to make your node visitors compatible with both Twig 1.x and 2.x. + Globals ------- diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index b172c0aed..4431db4e8 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.19.1-DEV" +#define PHP_TWIG_VERSION "1.20.0-DEV" #include "php.h" diff --git a/lib/Twig/BaseNodeVisitor.php b/lib/Twig/BaseNodeVisitor.php new file mode 100644 index 000000000..bb865d6f9 --- /dev/null +++ b/lib/Twig/BaseNodeVisitor.php @@ -0,0 +1,62 @@ + + */ +abstract class Twig_BaseNodeVisitor implements Twig_NodeVisitorInterface +{ + /** + * {@inheritdoc} + */ + final public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) + { + if (!$node instanceof Twig_Node) { + throw new LogicException('Twig_BaseNodeVisitor only supports Twig_Node instances.'); + } + + return $this->doEnterNode($node, $env); + } + + /** + * {@inheritdoc} + */ + final public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) + { + if (!$node instanceof Twig_Node) { + throw new LogicException('Twig_BaseNodeVisitor only supports Twig_Node instances.'); + } + + return $this->doLeaveNode($node, $env); + } + + /** + * Called before child nodes are visited. + * + * @param Twig_Node $node The node to visit + * @param Twig_Environment $env The Twig environment instance + * + * @return Twig_Node The modified node + */ + abstract protected function doEnterNode(Twig_Node $node, Twig_Environment $env); + + /** + * Called after child nodes are visited. + * + * @param Twig_Node $node The node to visit + * @param Twig_Environment $env The Twig environment instance + * + * @return Twig_Node|false The modified node or false if the node must be removed + */ + abstract protected function doLeaveNode(Twig_Node $node, Twig_Environment $env); +} diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 9c1a8a5f1..31226aac0 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,7 +16,7 @@ */ class Twig_Environment { - const VERSION = '1.19.1-DEV'; + const VERSION = '1.20.0-DEV'; protected $charset; protected $loader; diff --git a/lib/Twig/NodeVisitor/Escaper.php b/lib/Twig/NodeVisitor/Escaper.php index cc4b3d717..5c9497774 100644 --- a/lib/Twig/NodeVisitor/Escaper.php +++ b/lib/Twig/NodeVisitor/Escaper.php @@ -14,7 +14,7 @@ * * @author Fabien Potencier */ -class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface +class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor { protected $statusStack = array(); protected $blocks = array(); @@ -29,14 +29,9 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface } /** - * Called before child nodes are visited. - * - * @param Twig_NodeInterface $node The node to visit - * @param Twig_Environment $env The Twig environment instance - * - * @return Twig_NodeInterface The modified node + * {@inheritdoc} */ - public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) + protected function doEnterNode(Twig_Node $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { if ($env->hasExtension('escaper') && $defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename'))) { @@ -55,14 +50,9 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface } /** - * Called after child nodes are visited. - * - * @param Twig_NodeInterface $node The node to visit - * @param Twig_Environment $env The Twig environment instance - * - * @return Twig_NodeInterface The modified node + * {@inheritdoc} */ - public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) + protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $this->defaultStrategy = false; diff --git a/lib/Twig/NodeVisitor/Optimizer.php b/lib/Twig/NodeVisitor/Optimizer.php index 9cd1f8c01..872b7feaa 100644 --- a/lib/Twig/NodeVisitor/Optimizer.php +++ b/lib/Twig/NodeVisitor/Optimizer.php @@ -19,7 +19,7 @@ * * @author Fabien Potencier */ -class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface +class Twig_NodeVisitor_Optimizer extends Twig_BaseNodeVisitor { const OPTIMIZE_ALL = -1; const OPTIMIZE_NONE = 0; @@ -50,7 +50,7 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface /** * {@inheritdoc} */ - public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) + protected function doEnterNode(Twig_Node $node, Twig_Environment $env) { if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) { $this->enterOptimizeFor($node, $env); @@ -76,7 +76,7 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface /** * {@inheritdoc} */ - public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) + protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) { $expression = $node instanceof Twig_Node_Expression; diff --git a/lib/Twig/NodeVisitor/SafeAnalysis.php b/lib/Twig/NodeVisitor/SafeAnalysis.php index a5d06de2c..439f5bf49 100644 --- a/lib/Twig/NodeVisitor/SafeAnalysis.php +++ b/lib/Twig/NodeVisitor/SafeAnalysis.php @@ -1,6 +1,15 @@ */ -class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface +class Twig_NodeVisitor_Sandbox extends Twig_BaseNodeVisitor { protected $inAModule = false; protected $tags; @@ -22,14 +22,9 @@ class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface protected $functions; /** - * Called before child nodes are visited. - * - * @param Twig_NodeInterface $node The node to visit - * @param Twig_Environment $env The Twig environment instance - * - * @return Twig_NodeInterface The modified node + * {@inheritdoc} */ - public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) + protected function doEnterNode(Twig_Node $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $this->inAModule = true; @@ -64,14 +59,9 @@ class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface } /** - * Called after child nodes are visited. - * - * @param Twig_NodeInterface $node The node to visit - * @param Twig_Environment $env The Twig environment instance - * - * @return Twig_NodeInterface The modified node + * {@inheritdoc} */ - public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) + protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $this->inAModule = false; diff --git a/lib/Twig/Profiler/NodeVisitor/Profiler.php b/lib/Twig/Profiler/NodeVisitor/Profiler.php index 58beb0a54..4b0baa82e 100644 --- a/lib/Twig/Profiler/NodeVisitor/Profiler.php +++ b/lib/Twig/Profiler/NodeVisitor/Profiler.php @@ -12,7 +12,7 @@ /** * @author Fabien Potencier */ -class Twig_Profiler_NodeVisitor_Profiler implements Twig_NodeVisitorInterface +class Twig_Profiler_NodeVisitor_Profiler extends Twig_BaseNodeVisitor { private $extensionName; @@ -24,7 +24,7 @@ class Twig_Profiler_NodeVisitor_Profiler implements Twig_NodeVisitorInterface /** * {@inheritdoc} */ - public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) + protected function doEnterNode(Twig_Node $node, Twig_Environment $env) { return $node; } @@ -32,7 +32,7 @@ class Twig_Profiler_NodeVisitor_Profiler implements Twig_NodeVisitorInterface /** * {@inheritdoc} */ - public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) + protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $varName = $this->getVarName();