1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-23 21:04:52 +01:00
vichan/inc/lib/Twig/Node/For.php

114 lines
4.2 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?php
/*
* This file is part of Twig.
*
2018-05-10 12:24:53 +02:00
* (c) Fabien Potencier
* (c) Armin Ronacher
2011-10-05 06:22:53 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Represents a for node.
*
2013-08-01 21:20:12 +02:00
* @author Fabien Potencier <fabien@symfony.com>
2011-10-05 06:22:53 +02:00
*/
class Twig_Node_For extends Twig_Node
{
2013-08-01 21:20:12 +02:00
protected $loop;
2011-10-05 06:22:53 +02:00
public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_Node_Expression $ifexpr = null, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $lineno, $tag = null)
{
2013-08-01 21:20:12 +02:00
$body = new Twig_Node(array($body, $this->loop = new Twig_Node_ForLoop($lineno, $tag)));
if (null !== $ifexpr) {
$body = new Twig_Node_If(new Twig_Node(array($ifexpr, $body)), null, $lineno, $tag);
}
2018-05-10 12:24:53 +02:00
$nodes = array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body);
if (null !== $else) {
$nodes['else'] = $else;
}
parent::__construct($nodes, array('with_loop' => true, 'ifexpr' => null !== $ifexpr), $lineno, $tag);
2011-10-05 06:22:53 +02:00
}
public function compile(Twig_Compiler $compiler)
{
$compiler
->addDebugInfo($this)
2018-05-10 12:24:53 +02:00
->write("\$context['_parent'] = \$context;\n")
2011-10-05 06:22:53 +02:00
->write("\$context['_seq'] = twig_ensure_traversable(")
->subcompile($this->getNode('seq'))
->raw(");\n")
;
2018-05-10 12:24:53 +02:00
if ($this->hasNode('else')) {
2011-10-05 06:22:53 +02:00
$compiler->write("\$context['_iterated'] = false;\n");
}
if ($this->getAttribute('with_loop')) {
$compiler
->write("\$context['loop'] = array(\n")
->write(" 'parent' => \$context['_parent'],\n")
->write(" 'index0' => 0,\n")
->write(" 'index' => 1,\n")
->write(" 'first' => true,\n")
->write(");\n")
;
2013-08-01 21:20:12 +02:00
if (!$this->getAttribute('ifexpr')) {
2011-10-05 06:22:53 +02:00
$compiler
->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")
->indent()
->write("\$length = count(\$context['_seq']);\n")
->write("\$context['loop']['revindex0'] = \$length - 1;\n")
->write("\$context['loop']['revindex'] = \$length;\n")
->write("\$context['loop']['length'] = \$length;\n")
->write("\$context['loop']['last'] = 1 === \$length;\n")
->outdent()
->write("}\n")
;
}
}
2018-05-10 12:24:53 +02:00
$this->loop->setAttribute('else', $this->hasNode('else'));
2013-08-01 21:20:12 +02:00
$this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
$this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr'));
2011-10-05 06:22:53 +02:00
$compiler
->write("foreach (\$context['_seq'] as ")
->subcompile($this->getNode('key_target'))
2018-05-10 12:24:53 +02:00
->raw(' => ')
2011-10-05 06:22:53 +02:00
->subcompile($this->getNode('value_target'))
->raw(") {\n")
->indent()
2013-08-01 21:20:12 +02:00
->subcompile($this->getNode('body'))
2011-10-05 06:22:53 +02:00
->outdent()
->write("}\n")
;
2018-05-10 12:24:53 +02:00
if ($this->hasNode('else')) {
2011-10-05 06:22:53 +02:00
$compiler
->write("if (!\$context['_iterated']) {\n")
->indent()
->subcompile($this->getNode('else'))
->outdent()
->write("}\n")
;
}
$compiler->write("\$_parent = \$context['_parent'];\n");
// remove some "private" loop variables (needed for nested loops)
$compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
// keep the values set in the inner context for variables defined in the outer context
2013-08-01 21:20:12 +02:00
$compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
2011-10-05 06:22:53 +02:00
}
}
2018-05-10 12:24:53 +02:00
class_alias('Twig_Node_For', 'Twig\Node\ForNode', false);