Changeset 1101:7273894e61b8 for inc/libs/twig/TokenParser/For.php
- Timestamp:
- 02/15/13 08:35:19 (13 years ago)
- Branch:
- twig
- Children:
- 1106:a4487f3ca4b4, 1147:2e5cb79e4782
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/libs/twig/TokenParser/For.php
r991 r1101 34 34 { 35 35 $lineno = $token->getLine(); 36 $stream = $this->parser->getStream(); 36 37 $targets = $this->parser->getExpressionParser()->parseAssignmentExpression(); 37 $ this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, 'in');38 $stream->expect(Twig_Token::OPERATOR_TYPE, 'in'); 38 39 $seq = $this->parser->getExpressionParser()->parseExpression(); 39 40 40 41 $ifexpr = null; 41 if ($ this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'if')) {42 $ this->parser->getStream()->next();42 if ($stream->test(Twig_Token::NAME_TYPE, 'if')) { 43 $stream->next(); 43 44 $ifexpr = $this->parser->getExpressionParser()->parseExpression(); 44 45 } 45 46 46 $ this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);47 $stream->expect(Twig_Token::BLOCK_END_TYPE); 47 48 $body = $this->parser->subparse(array($this, 'decideForFork')); 48 if ($ this->parser->getStream()->next()->getValue() == 'else') {49 $ this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);49 if ($stream->next()->getValue() == 'else') { 50 $stream->expect(Twig_Token::BLOCK_END_TYPE); 50 51 $else = $this->parser->subparse(array($this, 'decideForEnd'), true); 51 52 } else { 52 53 $else = null; 53 54 } 54 $ this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);55 $stream->expect(Twig_Token::BLOCK_END_TYPE); 55 56 56 57 if (count($targets) > 1) { … … 63 64 $valueTarget = $targets->getNode(0); 64 65 $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine()); 66 } 67 68 if ($ifexpr) { 69 $this->checkLoopUsageCondition($stream, $ifexpr); 70 $this->checkLoopUsageBody($stream, $body); 65 71 } 66 72 … … 78 84 } 79 85 86 // the loop variable cannot be used in the condition 87 protected function checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node) 88 { 89 if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) { 90 throw new Twig_Error_Syntax('The "loop" variable cannot be used in a looping condition', $node->getLine(), $stream->getFilename()); 91 } 92 93 foreach ($node as $n) { 94 if (!$n) { 95 continue; 96 } 97 98 $this->checkLoopUsageCondition($stream, $n); 99 } 100 } 101 102 // check usage of non-defined loop-items 103 // it does not catch all problems (for instance when a for is included into another or when the variable is used in an include) 104 protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node) 105 { 106 if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) { 107 $attribute = $node->getNode('attribute'); 108 if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) { 109 throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition', $attribute->getAttribute('value')), $node->getLine(), $stream->getFilename()); 110 } 111 } 112 113 // should check for parent.loop.XXX usage 114 if ($node instanceof Twig_Node_For) { 115 return; 116 } 117 118 foreach ($node as $n) { 119 if (!$n) { 120 continue; 121 } 122 123 $this->checkLoopUsageBody($stream, $n); 124 } 125 } 126 80 127 /** 81 128 * Gets the tag name associated with this token parser.
Note: See TracChangeset
for help on using the changeset viewer.