composer package updates
[openemr.git] / vendor / twig / twig / lib / Twig / NodeVisitor / SafeAnalysis.php
blobca31c8fc76b759a484cbc815a43a13be3e7db4e8
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 * @final
15 class Twig_NodeVisitor_SafeAnalysis extends Twig_BaseNodeVisitor
17 protected $data = array();
18 protected $safeVars = array();
20 public function setSafeVars($safeVars)
22 $this->safeVars = $safeVars;
25 public function getSafe(Twig_NodeInterface $node)
27 $hash = spl_object_hash($node);
28 if (!isset($this->data[$hash])) {
29 return;
32 foreach ($this->data[$hash] as $bucket) {
33 if ($bucket['key'] !== $node) {
34 continue;
37 if (in_array('html_attr', $bucket['value'])) {
38 $bucket['value'][] = 'html';
41 return $bucket['value'];
45 protected function setSafe(Twig_NodeInterface $node, array $safe)
47 $hash = spl_object_hash($node);
48 if (isset($this->data[$hash])) {
49 foreach ($this->data[$hash] as &$bucket) {
50 if ($bucket['key'] === $node) {
51 $bucket['value'] = $safe;
53 return;
57 $this->data[$hash][] = array(
58 'key' => $node,
59 'value' => $safe,
63 protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
65 return $node;
68 protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
70 if ($node instanceof Twig_Node_Expression_Constant) {
71 // constants are marked safe for all
72 $this->setSafe($node, array('all'));
73 } elseif ($node instanceof Twig_Node_Expression_BlockReference) {
74 // blocks are safe by definition
75 $this->setSafe($node, array('all'));
76 } elseif ($node instanceof Twig_Node_Expression_Parent) {
77 // parent block is safe by definition
78 $this->setSafe($node, array('all'));
79 } elseif ($node instanceof Twig_Node_Expression_Conditional) {
80 // intersect safeness of both operands
81 $safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
82 $this->setSafe($node, $safe);
83 } elseif ($node instanceof Twig_Node_Expression_Filter) {
84 // filter expression is safe when the filter is safe
85 $name = $node->getNode('filter')->getAttribute('value');
86 $args = $node->getNode('arguments');
87 if (false !== $filter = $env->getFilter($name)) {
88 $safe = $filter->getSafe($args);
89 if (null === $safe) {
90 $safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety());
92 $this->setSafe($node, $safe);
93 } else {
94 $this->setSafe($node, array());
96 } elseif ($node instanceof Twig_Node_Expression_Function) {
97 // function expression is safe when the function is safe
98 $name = $node->getAttribute('name');
99 $args = $node->getNode('arguments');
100 $function = $env->getFunction($name);
101 if (false !== $function) {
102 $this->setSafe($node, $function->getSafe($args));
103 } else {
104 $this->setSafe($node, array());
106 } elseif ($node instanceof Twig_Node_Expression_MethodCall) {
107 if ($node->getAttribute('safe')) {
108 $this->setSafe($node, array('all'));
109 } else {
110 $this->setSafe($node, array());
112 } elseif ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name) {
113 $name = $node->getNode('node')->getAttribute('name');
114 // attributes on template instances are safe
115 if ('_self' == $name || in_array($name, $this->safeVars)) {
116 $this->setSafe($node, array('all'));
117 } else {
118 $this->setSafe($node, array());
120 } else {
121 $this->setSafe($node, array());
124 return $node;
127 protected function intersectSafe(array $a = null, array $b = null)
129 if (null === $a || null === $b) {
130 return array();
133 if (in_array('all', $a)) {
134 return $b;
137 if (in_array('all', $b)) {
138 return $a;
141 return array_intersect($a, $b);
144 public function getPriority()
146 return 0;
150 class_alias('Twig_NodeVisitor_SafeAnalysis', 'Twig\NodeVisitor\SafeAnalysisNodeVisitor', false);