Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

    
12
/**
13
 * Twig_BaseNodeVisitor can be used to make node visitors compatible with Twig 1.x and 2.x.
14
 *
15
 * @author Fabien Potencier <fabien@symfony.com>
16
 */
17
abstract class Twig_BaseNodeVisitor implements Twig_NodeVisitorInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    final public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
23
    {
24
        if (!$node instanceof Twig_Node) {
25
            throw new LogicException('Twig_BaseNodeVisitor only supports Twig_Node instances.');
26
        }
27

    
28
        return $this->doEnterNode($node, $env);
29
    }
30

    
31
    /**
32
     * {@inheritdoc}
33
     */
34
    final public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
35
    {
36
        if (!$node instanceof Twig_Node) {
37
            throw new LogicException('Twig_BaseNodeVisitor only supports Twig_Node instances.');
38
        }
39

    
40
        return $this->doLeaveNode($node, $env);
41
    }
42

    
43
    /**
44
     * Called before child nodes are visited.
45
     *
46
     * @param Twig_Node        $node The node to visit
47
     * @param Twig_Environment $env  The Twig environment instance
48
     *
49
     * @return Twig_Node The modified node
50
     */
51
    abstract protected function doEnterNode(Twig_Node $node, Twig_Environment $env);
52

    
53
    /**
54
     * Called after child nodes are visited.
55
     *
56
     * @param Twig_Node        $node The node to visit
57
     * @param Twig_Environment $env  The Twig environment instance
58
     *
59
     * @return Twig_Node|false The modified node or false if the node must be removed
60
     */
61
    abstract protected function doLeaveNode(Twig_Node $node, Twig_Environment $env);
62
}
(2-2/43)