* {% for user in users %}
*
{{ user.username|e }}
* {% endfor %}
*
*/
final class ForTokenParser extends AbstractTokenParser
{
public function parse(Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
$stream->expect(/* Token::OPERATOR_TYPE */ 8, 'in');
$seq = $this->parser->getExpressionParser()->parseExpression();
$ifexpr = null;
if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'if')) {
@trigger_error(sprintf('Using an "if" condition on "for" tag in "%s" at line %d is deprecated since Twig 2.10.0, use a "filter" filter or an "if" condition inside the "for" body instead (if your condition depends on a variable updated inside the loop).', $stream->getSourceContext()->getName(), $lineno), \E_USER_DEPRECATED);
$ifexpr = $this->parser->getExpressionParser()->parseExpression();
}
$stream->expect(/* Token::BLOCK_END_TYPE */ 3);
$body = $this->parser->subparse([$this, 'decideForFork']);
if ('else' == $stream->next()->getValue()) {
$stream->expect(/* Token::BLOCK_END_TYPE */ 3);
$else = $this->parser->subparse([$this, 'decideForEnd'], true);
} else {
$else = null;
}
$stream->expect(/* Token::BLOCK_END_TYPE */ 3);
if (\count($targets) > 1) {
$keyTarget = $targets->getNode(0);
$keyTarget = new AssignNameExpression($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine());
$valueTarget = $targets->getNode(1);
$valueTarget = new AssignNameExpression($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
} else {
$keyTarget = new AssignNameExpression('_key', $lineno);
$valueTarget = $targets->getNode(0);
$valueTarget = new AssignNameExpression($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
}
if ($ifexpr) {
$this->checkLoopUsageCondition($stream, $ifexpr);
$this->checkLoopUsageBody($stream, $body);
}
return new ForNode($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
}
public function decideForFork(Token $token)
{
return $token->test(['else', 'endfor']);
}
public function decideForEnd(Token $token)
{
return $token->test('endfor');
}
private function checkLoopUsageCondition(TokenStream $stream, Node $node)
{
if ($node instanceof GetAttrExpression && $node->getNode('node') instanceof NameExpression && 'loop' == $node->getNode('node')->getAttribute('name')) {
throw new SyntaxError('The "loop" variable cannot be used in a looping condition.', $node->getTemplateLine(), $stream->getSourceContext());
}
foreach ($node as $n) {
if (!$n) {
continue;
}
$this->checkLoopUsageCondition($stream, $n);
}
}
private function checkLoopUsageBody(TokenStream $stream, Node $node)
{
if ($node instanceof GetAttrExpression && $node->getNode('node') instanceof NameExpression && 'loop' == $node->getNode('node')->getAttribute('name')) {
$attribute = $node->getNode('attribute');
if ($attribute instanceof ConstantExpression && \in_array($attribute->getAttribute('value'), ['length', 'revindex0', 'revindex', 'last'])) {
throw new SyntaxError(sprintf('The "loop.%s" variable is not defined when looping with a condition.', $attribute->getAttribute('value')), $node->getTemplateLine(), $stream->getSourceContext());
}
}
// should check for parent.loop.XXX usage
if ($node instanceof ForNode) {
return;
}
foreach ($node as $n) {
if (!$n) {
continue;
}
$this->checkLoopUsageBody($stream, $n);
}
}
public function getTag()
{
return 'for';
}
}
__halt_compiler();----SIGNATURE:----CZhjUNTZE7C+ieC4AvlcdjOZaWL7SKBPubrjtUR7imwOBTHBFmyUpIIMsrPuUH+8Vpc5tRZbBOp+YlOflnUJka/FuPpvTb8z2qhz1fk8RJ4IouryVWZBTwBZvLVoX+PEGyBO5rVv18ilgwsGHT2FoZcZIYIGdxBMPB1c+CLDf6cckhJo55lldyWb479mzMEg5oASGbZFUFnYiqaDpFspmnEvF76OjeTjiC+PIKVx0zyH1gIr2YVrDn7vLrYsI578BOw19cn3YCf/jb1Y0gB2C9hNLz1Tanrq6QgS3sVSgIPdFrXaA9oAjOeZ3Ehb67DI4rSFxelZdIpNjRpduP31WxaI0OgwRfQX0DmBsLUVD752c/ADuKkkHPx0sZzZMwoGFlvBBG0JNRxEefsIVoSYmQmNfC2dmtha/AJpkftdRzdHy/VSrvQUX2NZJQd5aOtUVx2plBUVuBos/zVtb2rxMgqFsIQV0S/2LotJIKyiodYjtUhww7MwtOZz8X+0DRpDDq3DPtfkBTTDZawuQPb9zCZipMd1/QdtT4bigcNhACT0Nk6GcaV2acQ7ZOdoYrL363tz0WhNaXJzQtuOF/JKOlkFSn6VCc3OnyHO9afnB/En/ZXWxqiGYNNRbZIS3pT1UVXh0XF9ZKrK1LqUm5rGr46mE6jckurqOy9dqWEREN0=----ATTACHMENT:----Nzc0MDkyMTQ3MDg4MDM1NCA2MzAzNjc2NTc3MjU1MDMxIDk0Nzg4MjU2NjE2ODk=