Dotclear


Ignore:
Timestamp:
02/15/13 08:35:19 (13 years ago)
Author:
Dsls <dsls@…>
Branch:
twig
Children:
1106:a4487f3ca4b4, 1147:2e5cb79e4782
Message:

Twig 1.12.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/libs/twig/TokenParser/For.php

    r991 r1101  
    3434    { 
    3535        $lineno = $token->getLine(); 
     36        $stream = $this->parser->getStream(); 
    3637        $targets = $this->parser->getExpressionParser()->parseAssignmentExpression(); 
    37         $this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, 'in'); 
     38        $stream->expect(Twig_Token::OPERATOR_TYPE, 'in'); 
    3839        $seq = $this->parser->getExpressionParser()->parseExpression(); 
    3940 
    4041        $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(); 
    4344            $ifexpr = $this->parser->getExpressionParser()->parseExpression(); 
    4445        } 
    4546 
    46         $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); 
     47        $stream->expect(Twig_Token::BLOCK_END_TYPE); 
    4748        $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); 
    5051            $else = $this->parser->subparse(array($this, 'decideForEnd'), true); 
    5152        } else { 
    5253            $else = null; 
    5354        } 
    54         $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); 
     55        $stream->expect(Twig_Token::BLOCK_END_TYPE); 
    5556 
    5657        if (count($targets) > 1) { 
     
    6364            $valueTarget = $targets->getNode(0); 
    6465            $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); 
    6571        } 
    6672 
     
    7884    } 
    7985 
     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 
    80127    /** 
    81128     * Gets the tag name associated with this token parser. 
Note: See TracChangeset for help on using the changeset viewer.

Sites map