PEEL Shopping
Open source ecommerce : PEEL Shopping
For.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) 2009 Fabien Potencier
7  * (c) 2009 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 
25 {
33  public function parse(Twig_Token $token)
34  {
35  $lineno = $token->getLine();
36  $stream = $this->parser->getStream();
37  $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
38  $stream->expect(Twig_Token::OPERATOR_TYPE, 'in');
39  $seq = $this->parser->getExpressionParser()->parseExpression();
40 
41  $ifexpr = null;
42  if ($stream->test(Twig_Token::NAME_TYPE, 'if')) {
43  $stream->next();
44  $ifexpr = $this->parser->getExpressionParser()->parseExpression();
45  }
46 
47  $stream->expect(Twig_Token::BLOCK_END_TYPE);
48  $body = $this->parser->subparse(array($this, 'decideForFork'));
49  if ($stream->next()->getValue() == 'else') {
50  $stream->expect(Twig_Token::BLOCK_END_TYPE);
51  $else = $this->parser->subparse(array($this, 'decideForEnd'), true);
52  } else {
53  $else = null;
54  }
55  $stream->expect(Twig_Token::BLOCK_END_TYPE);
56 
57  if (count($targets) > 1) {
58  $keyTarget = $targets->getNode(0);
59  $keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getLine());
60  $valueTarget = $targets->getNode(1);
61  $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
62  } else {
63  $keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
64  $valueTarget = $targets->getNode(0);
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);
71  }
72 
73  return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
74  }
75 
76  public function decideForFork(Twig_Token $token)
77  {
78  return $token->test(array('else', 'endfor'));
79  }
80 
81  public function decideForEnd(Twig_Token $token)
82  {
83  return $token->test('endfor');
84  }
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 
132  public function getTag()
133  {
134  return 'for';
135  }
136 }
getLine()
Definition: Node.php:109
Base class for all token parsers.
Definition: TokenParser.php:17
const OPERATOR_TYPE
Definition: Token.php:33
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
decideForEnd(Twig_Token $token)
Definition: For.php:81
checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:104
getAttribute($name)
Gets an attribute.
Definition: Node.php:138
parse(Twig_Token $token)
Parses a token and returns a node.
Definition: For.php:33
Represents a token stream.
Definition: TokenStream.php:18
Represents a for node.
Definition: For.php:18
getTag()
Gets the tag name associated with this token parser.
Definition: For.php:132
getFilename()
Gets the filename associated with this stream.
Loops over each item of a sequence.
Definition: For.php:24
const BLOCK_END_TYPE
Definition: Token.php:28
Represents a node in the AST.
decideForFork(Twig_Token $token)
Definition: For.php:76
Represents a Token.
Definition: Token.php:18
checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:87
const NAME_TYPE
Definition: Token.php:30
getLine()
Gets the line.
Definition: Token.php:94
test($type, $values=null)
Tests the current token for a type and/or a value.
Definition: Token.php:75
getNode($name)
Gets a node by name.
Definition: Node.php:187

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:18 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.