Skip to content

Commit

Permalink
feature #1753 added a base node visitor to make compat between 1.x an…
Browse files Browse the repository at this point in the history
…d 2.x possible (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

added a base node visitor to make compat between 1.x and 2.x possible

As suggested in #1644, that allows better compat between 1.x and 2.x

Commits
-------

881fb63 added a base node visitor to make compat between 1.x and 2.x possible
3f521d4 bumped to 1.20
  • Loading branch information
fabpot committed Aug 11, 2015
2 parents e375c9c + 881fb63 commit 61d45b3
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 44 deletions.
5 changes: 3 additions & 2 deletions 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)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -36,7 +36,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.19-dev"
"dev-master": "1.20-dev"
}
}
}
7 changes: 7 additions & 0 deletions doc/deprecated.rst
Expand Up @@ -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
-------

Expand Down
2 changes: 1 addition & 1 deletion ext/twig/php_twig.h
Expand Up @@ -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"

Expand Down
62 changes: 62 additions & 0 deletions lib/Twig/BaseNodeVisitor.php
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Twig_BaseNodeVisitor can be used to make node visitors compatible with Twig 1.x and 2.x.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
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);
}
2 changes: 1 addition & 1 deletion lib/Twig/Environment.php
Expand Up @@ -16,7 +16,7 @@
*/
class Twig_Environment
{
const VERSION = '1.19.1-DEV';
const VERSION = '1.20.0-DEV';

protected $charset;
protected $loader;
Expand Down
20 changes: 5 additions & 15 deletions lib/Twig/NodeVisitor/Escaper.php
Expand Up @@ -14,7 +14,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface
class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor
{
protected $statusStack = array();
protected $blocks = array();
Expand All @@ -29,14 +29,9 @@ public function __construct()
}

/**
* 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'))) {
Expand All @@ -55,14 +50,9 @@ public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
}

/**
* 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;
Expand Down
6 changes: 3 additions & 3 deletions lib/Twig/NodeVisitor/Optimizer.php
Expand Up @@ -19,7 +19,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
class Twig_NodeVisitor_Optimizer extends Twig_BaseNodeVisitor
{
const OPTIMIZE_ALL = -1;
const OPTIMIZE_NONE = 0;
Expand Down Expand Up @@ -50,7 +50,7 @@ public function __construct($optimizers = -1)
/**
* {@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);
Expand All @@ -76,7 +76,7 @@ public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
/**
* {@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;

Expand Down
21 changes: 18 additions & 3 deletions lib/Twig/NodeVisitor/SafeAnalysis.php
@@ -1,6 +1,15 @@
<?php

class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class Twig_NodeVisitor_SafeAnalysis extends Twig_BaseNodeVisitor
{
protected $data = array();
protected $safeVars = array();
Expand Down Expand Up @@ -48,12 +57,18 @@ protected function setSafe(Twig_NodeInterface $node, array $safe)
);
}

public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
/**
* {@inheritdoc}
*/
protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
{
return $node;
}

public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
/**
* {@inheritdoc}
*/
protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
{
if ($node instanceof Twig_Node_Expression_Constant) {
// constants are marked safe for all
Expand Down
20 changes: 5 additions & 15 deletions lib/Twig/NodeVisitor/Sandbox.php
Expand Up @@ -14,22 +14,17 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface
class Twig_NodeVisitor_Sandbox extends Twig_BaseNodeVisitor
{
protected $inAModule = false;
protected $tags;
protected $filters;
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;
Expand Down Expand Up @@ -64,14 +59,9 @@ public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
}

/**
* 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;
Expand Down
6 changes: 3 additions & 3 deletions lib/Twig/Profiler/NodeVisitor/Profiler.php
Expand Up @@ -12,7 +12,7 @@
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Profiler_NodeVisitor_Profiler implements Twig_NodeVisitorInterface
class Twig_Profiler_NodeVisitor_Profiler extends Twig_BaseNodeVisitor
{
private $extensionName;

Expand All @@ -24,15 +24,15 @@ public function __construct($extensionName)
/**
* {@inheritdoc}
*/
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
{
return $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) {
$varName = $this->getVarName();
Expand Down

0 comments on commit 61d45b3

Please sign in to comment.