source: pro-violet-viettel/sourcecode/application/third_party/Twig/NodeVisitor/SafeAnalysis.php @ 689

Last change on this file since 689 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 3.0 KB
Line 
1<?php
2
3class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface
4{
5    protected $data = array();
6
7    public function getSafe(Twig_NodeInterface $node)
8    {
9        $hash = spl_object_hash($node);
10        if (isset($this->data[$hash])) {
11            foreach($this->data[$hash] as $bucket) {
12                if ($bucket['key'] === $node) {
13                    return $bucket['value'];
14                }
15            }
16        }
17        return null;
18    }
19
20    protected function setSafe(Twig_NodeInterface $node, array $safe)
21    {
22        $hash = spl_object_hash($node);
23        if (isset($this->data[$hash])) {
24            foreach($this->data[$hash] as &$bucket) {
25                if ($bucket['key'] === $node) {
26                    $bucket['value'] = $safe;
27                    return;
28                }
29            }
30        }
31        $this->data[$hash][] = array(
32            'key' => $node,
33            'value' => $safe,
34        );
35    }
36
37    public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
38    {
39        return $node;
40    }
41
42    public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
43    {
44        if ($node instanceof Twig_Node_Expression_Constant) {
45            // constants are marked safe for all
46            $this->setSafe($node, array('all'));
47        } elseif ($node instanceof Twig_Node_Expression_Conditional) {
48            // intersect safeness of both operands
49            $safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
50            $this->setSafe($node, $safe);
51        } elseif ($node instanceof Twig_Node_Expression_Filter) {
52            // filter expression is safe when the filter is safe
53            $name = $node->getNode('filter')->getAttribute('value');
54            $args = $node->getNode('arguments');
55            if (false !== $filter = $env->getFilter($name)) {
56                $this->setSafe($node, $filter->getSafe($args));
57            } else {
58                $this->setSafe($node, array());
59            }
60        } elseif ($node instanceof Twig_Node_Expression_Function) {
61            // function expression is safe when the function is safe
62            $name = $node->getNode('name')->getAttribute('name');
63            $args = $node->getNode('arguments');
64            $function = $env->getFunction($name);
65            if (false !== $function) {
66                $this->setSafe($node, $function->getSafe($args));
67            } else {
68                $this->setSafe($node, array());
69            }
70        } else {
71            $this->setSafe($node, array());
72        }
73
74        return $node;
75    }
76
77    protected function intersectSafe(array $a = null, array $b = null)
78    {
79        if (null === $a || null === $b) {
80            return array();
81        }
82
83        if (in_array('all', $a)) {
84            return $b;
85        }
86
87        if (in_array('all', $b)) {
88            return $a;
89        }
90
91        return array_intersect($a, $b);
92    }
93
94    /**
95     * {@inheritdoc}
96     */
97    public function getPriority()
98    {
99        return 0;
100    }
101}
Note: See TracBrowser for help on using the repository browser.