Dotclear

source: inc/libs/Twig/Node/For.php @ 1149:1657e862089c

Revision 1149:1657e862089c, 4.3 KB checked in by dsls, 13 years ago (diff)

Fixed unix case-sensitive twig directory

Line 
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
13/**
14 * Represents a for node.
15 *
16 * @package    twig
17 * @author     Fabien Potencier <fabien@symfony.com>
18 */
19class Twig_Node_For extends Twig_Node
20{
21    protected $loop;
22
23    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)
24    {
25        $body = new Twig_Node(array($body, $this->loop = new Twig_Node_ForLoop($lineno, $tag)));
26
27        if (null !== $ifexpr) {
28            $body = new Twig_Node_If(new Twig_Node(array($ifexpr, $body)), null, $lineno, $tag);
29        }
30
31        parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else), array('with_loop' => true, 'ifexpr' => null !== $ifexpr), $lineno, $tag);
32    }
33
34    /**
35     * Compiles the node to PHP.
36     *
37     * @param Twig_Compiler A Twig_Compiler instance
38     */
39    public function compile(Twig_Compiler $compiler)
40    {
41        $compiler
42            ->addDebugInfo($this)
43            // the (array) cast bypasses a PHP 5.2.6 bug
44            ->write("\$context['_parent'] = (array) \$context;\n")
45            ->write("\$context['_seq'] = twig_ensure_traversable(")
46            ->subcompile($this->getNode('seq'))
47            ->raw(");\n")
48        ;
49
50        if (null !== $this->getNode('else')) {
51            $compiler->write("\$context['_iterated'] = false;\n");
52        }
53
54        if ($this->getAttribute('with_loop')) {
55            $compiler
56                ->write("\$context['loop'] = array(\n")
57                ->write("  'parent' => \$context['_parent'],\n")
58                ->write("  'index0' => 0,\n")
59                ->write("  'index'  => 1,\n")
60                ->write("  'first'  => true,\n")
61                ->write(");\n")
62            ;
63
64            if (!$this->getAttribute('ifexpr')) {
65                $compiler
66                    ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")
67                    ->indent()
68                    ->write("\$length = count(\$context['_seq']);\n")
69                    ->write("\$context['loop']['revindex0'] = \$length - 1;\n")
70                    ->write("\$context['loop']['revindex'] = \$length;\n")
71                    ->write("\$context['loop']['length'] = \$length;\n")
72                    ->write("\$context['loop']['last'] = 1 === \$length;\n")
73                    ->outdent()
74                    ->write("}\n")
75                ;
76            }
77        }
78
79        $this->loop->setAttribute('else', null !== $this->getNode('else'));
80        $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
81        $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr'));
82
83        $compiler
84            ->write("foreach (\$context['_seq'] as ")
85            ->subcompile($this->getNode('key_target'))
86            ->raw(" => ")
87            ->subcompile($this->getNode('value_target'))
88            ->raw(") {\n")
89            ->indent()
90            ->subcompile($this->getNode('body'))
91            ->outdent()
92            ->write("}\n")
93        ;
94
95        if (null !== $this->getNode('else')) {
96            $compiler
97                ->write("if (!\$context['_iterated']) {\n")
98                ->indent()
99                ->subcompile($this->getNode('else'))
100                ->outdent()
101                ->write("}\n")
102            ;
103        }
104
105        $compiler->write("\$_parent = \$context['_parent'];\n");
106
107        // remove some "private" loop variables (needed for nested loops)
108        $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");
109
110        // keep the values set in the inner context for variables defined in the outer context
111        $compiler->write("\$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent));\n");
112    }
113}
Note: See TracBrowser for help on using the repository browser.

Sites map