Security update to Drupal 8.4.6
[yaffs-website] / vendor / twig / twig / lib / Twig / TokenParser / For.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  * (c) Armin Ronacher
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12
13 /**
14  * Loops over each item of a sequence.
15  *
16  * <pre>
17  * <ul>
18  *  {% for user in users %}
19  *    <li>{{ user.username|e }}</li>
20  *  {% endfor %}
21  * </ul>
22  * </pre>
23  *
24  * @final
25  */
26 class Twig_TokenParser_For extends Twig_TokenParser
27 {
28     public function parse(Twig_Token $token)
29     {
30         $lineno = $token->getLine();
31         $stream = $this->parser->getStream();
32         $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
33         $stream->expect(Twig_Token::OPERATOR_TYPE, 'in');
34         $seq = $this->parser->getExpressionParser()->parseExpression();
35
36         $ifexpr = null;
37         if ($stream->nextIf(Twig_Token::NAME_TYPE, 'if')) {
38             $ifexpr = $this->parser->getExpressionParser()->parseExpression();
39         }
40
41         $stream->expect(Twig_Token::BLOCK_END_TYPE);
42         $body = $this->parser->subparse(array($this, 'decideForFork'));
43         if ('else' == $stream->next()->getValue()) {
44             $stream->expect(Twig_Token::BLOCK_END_TYPE);
45             $else = $this->parser->subparse(array($this, 'decideForEnd'), true);
46         } else {
47             $else = null;
48         }
49         $stream->expect(Twig_Token::BLOCK_END_TYPE);
50
51         if (count($targets) > 1) {
52             $keyTarget = $targets->getNode(0);
53             $keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine());
54             $valueTarget = $targets->getNode(1);
55             $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
56         } else {
57             $keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
58             $valueTarget = $targets->getNode(0);
59             $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
60         }
61
62         if ($ifexpr) {
63             $this->checkLoopUsageCondition($stream, $ifexpr);
64             $this->checkLoopUsageBody($stream, $body);
65         }
66
67         return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
68     }
69
70     public function decideForFork(Twig_Token $token)
71     {
72         return $token->test(array('else', 'endfor'));
73     }
74
75     public function decideForEnd(Twig_Token $token)
76     {
77         return $token->test('endfor');
78     }
79
80     // the loop variable cannot be used in the condition
81     protected function checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
82     {
83         if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
84             throw new Twig_Error_Syntax('The "loop" variable cannot be used in a looping condition.', $node->getTemplateLine(), $stream->getSourceContext());
85         }
86
87         foreach ($node as $n) {
88             if (!$n) {
89                 continue;
90             }
91
92             $this->checkLoopUsageCondition($stream, $n);
93         }
94     }
95
96     // check usage of non-defined loop-items
97     // it does not catch all problems (for instance when a for is included into another or when the variable is used in an include)
98     protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
99     {
100         if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
101             $attribute = $node->getNode('attribute');
102             if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) {
103                 throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition.', $attribute->getAttribute('value')), $node->getTemplateLine(), $stream->getSourceContext());
104             }
105         }
106
107         // should check for parent.loop.XXX usage
108         if ($node instanceof Twig_Node_For) {
109             return;
110         }
111
112         foreach ($node as $n) {
113             if (!$n) {
114                 continue;
115             }
116
117             $this->checkLoopUsageBody($stream, $n);
118         }
119     }
120
121     public function getTag()
122     {
123         return 'for';
124     }
125 }
126
127 class_alias('Twig_TokenParser_For', 'Twig\TokenParser\ForTokenParser', false);