ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
For.php
Go to the documentation of this file.
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 
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 
42  $body = $this->parser->subparse(array($this, 'decideForFork'));
43  if ('else' == $stream->next()->getValue()) {
45  $else = $this->parser->subparse(array($this, 'decideForEnd'), true);
46  } else {
47  $else = null;
48  }
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
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)
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);
Represents a node in the AST.
getSourceContext()
Gets the source associated with this stream.
test($type, $values=null)
Tests the current token for a type and/or a value.
Definition: Token.php:70
Represents a token stream.
Definition: TokenStream.php:20
checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:98
$stream
PHP stream implementation.
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
Base class for all token parsers.
Definition: TokenParser.php:17
checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:81
decideForFork(Twig_Token $token)
Definition: For.php:70
getAttribute($name)
Definition: Node.php:152
getTemplateLine()
Definition: Node.php:121
$n
Definition: RandomTest.php:85
getNode($name)
Definition: Node.php:186
Loops over each item of a sequence.
Definition: For.php:26
parse(Twig_Token $token)
Parses a token and returns a node.
Definition: For.php:28
getTag()
Gets the tag name associated with this token parser.
Definition: For.php:121
Represents a for node.
Definition: For.php:18
getLine()
Definition: Token.php:87
const OPERATOR_TYPE
Definition: Token.php:35
decideForEnd(Twig_Token $token)
Definition: For.php:75
const NAME_TYPE
Definition: Token.php:32
const BLOCK_END_TYPE
Definition: Token.php:30