composer package updates
[openemr.git] / vendor / twig / twig / lib / Twig / NodeVisitor / Sandbox.php
blob71aa4f029b4b99dd753e80a96a709a3d5eb27053
1 <?php
3 /*
4 * This file is part of Twig.
6 * (c) Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 /**
13 * Twig_NodeVisitor_Sandbox implements sandboxing.
15 * @final
17 * @author Fabien Potencier <fabien@symfony.com>
19 class Twig_NodeVisitor_Sandbox extends Twig_BaseNodeVisitor
21 protected $inAModule = false;
22 protected $tags;
23 protected $filters;
24 protected $functions;
26 protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
28 if ($node instanceof Twig_Node_Module) {
29 $this->inAModule = true;
30 $this->tags = array();
31 $this->filters = array();
32 $this->functions = array();
34 return $node;
35 } elseif ($this->inAModule) {
36 // look for tags
37 if ($node->getNodeTag() && !isset($this->tags[$node->getNodeTag()])) {
38 $this->tags[$node->getNodeTag()] = $node;
41 // look for filters
42 if ($node instanceof Twig_Node_Expression_Filter && !isset($this->filters[$node->getNode('filter')->getAttribute('value')])) {
43 $this->filters[$node->getNode('filter')->getAttribute('value')] = $node;
46 // look for functions
47 if ($node instanceof Twig_Node_Expression_Function && !isset($this->functions[$node->getAttribute('name')])) {
48 $this->functions[$node->getAttribute('name')] = $node;
51 // the .. operator is equivalent to the range() function
52 if ($node instanceof Twig_Node_Expression_Binary_Range && !isset($this->functions['range'])) {
53 $this->functions['range'] = $node;
56 // wrap print to check __toString() calls
57 if ($node instanceof Twig_Node_Print) {
58 return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getTemplateLine(), $node->getNodeTag());
62 return $node;
65 protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
67 if ($node instanceof Twig_Node_Module) {
68 $this->inAModule = false;
70 $node->setNode('display_start', new Twig_Node(array(new Twig_Node_CheckSecurity($this->filters, $this->tags, $this->functions), $node->getNode('display_start'))));
73 return $node;
76 public function getPriority()
78 return 0;
82 class_alias('Twig_NodeVisitor_Sandbox', 'Twig\NodeVisitor\SandboxNodeVisitor', false);