4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Twig_NodeVisitor_Sandbox implements sandboxing.
15 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_NodeVisitor_Sandbox
implements Twig_NodeVisitorInterface
19 protected $inAModule = false;
25 * Called before child nodes are visited.
27 * @param Twig_NodeInterface $node The node to visit
28 * @param Twig_Environment $env The Twig environment instance
30 * @return Twig_NodeInterface The modified node
32 public function enterNode(Twig_NodeInterface
$node, Twig_Environment
$env)
34 if ($node instanceof Twig_Node_Module
) {
35 $this->inAModule
= true;
36 $this->tags
= array();
37 $this->filters
= array();
38 $this->functions
= array();
41 } elseif ($this->inAModule
) {
43 if ($node->getNodeTag()) {
44 $this->tags
[] = $node->getNodeTag();
48 if ($node instanceof Twig_Node_Expression_Filter
) {
49 $this->filters
[] = $node->getNode('filter')->getAttribute('value');
53 if ($node instanceof Twig_Node_Expression_Function
) {
54 $this->functions
[] = $node->getAttribute('name');
57 // wrap print to check __toString() calls
58 if ($node instanceof Twig_Node_Print
) {
59 return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
67 * Called after child nodes are visited.
69 * @param Twig_NodeInterface $node The node to visit
70 * @param Twig_Environment $env The Twig environment instance
72 * @return Twig_NodeInterface The modified node
74 public function leaveNode(Twig_NodeInterface
$node, Twig_Environment
$env)
76 if ($node instanceof Twig_Node_Module
) {
77 $this->inAModule
= false;
79 return new Twig_Node_SandboxedModule($node, array_unique($this->filters
), array_unique($this->tags
), array_unique($this->functions
));
88 public function getPriority()