source: pro-violet-viettel/sourcecode/application/third_party/Twig/Compiler.php @ 545

Last change on this file since 545 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 5.0 KB
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 * Compiles a node to PHP code.
15 *
16 * @package    twig
17 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18 */
19class Twig_Compiler implements Twig_CompilerInterface
20{
21    protected $lastLine;
22    protected $source;
23    protected $indentation;
24    protected $env;
25
26    /**
27     * Constructor.
28     *
29     * @param Twig_Environment $env The twig environment instance
30     */
31    public function __construct(Twig_Environment $env)
32    {
33        $this->env = $env;
34    }
35
36    /**
37     * Returns the environment instance related to this compiler.
38     *
39     * @return Twig_Environment The environment instance
40     */
41    public function getEnvironment()
42    {
43        return $this->env;
44    }
45
46    /**
47     * Gets the current PHP code after compilation.
48     *
49     * @return string The PHP code
50     */
51    public function getSource()
52    {
53        return $this->source;
54    }
55
56    /**
57     * Compiles a node.
58     *
59     * @param Twig_NodeInterface $node   The node to compile
60     * @param integer            $indent The current indentation
61     *
62     * @return Twig_Compiler The current compiler instance
63     */
64    public function compile(Twig_NodeInterface $node, $indentation = 0)
65    {
66        $this->lastLine = null;
67        $this->source = '';
68        $this->indentation = $indentation;
69
70        $node->compile($this);
71
72        return $this;
73    }
74
75    public function subcompile(Twig_NodeInterface $node, $raw = true)
76    {
77        if (false === $raw)
78        {
79            $this->addIndentation();
80        }
81
82        $node->compile($this);
83
84        return $this;
85    }
86
87    /**
88     * Adds a raw string to the compiled code.
89     *
90     * @param  string $string The string
91     *
92     * @return Twig_Compiler The current compiler instance
93     */
94    public function raw($string)
95    {
96        $this->source .= $string;
97
98        return $this;
99    }
100
101    /**
102     * Writes a string to the compiled code by adding indentation.
103     *
104     * @return Twig_Compiler The current compiler instance
105     */
106    public function write()
107    {
108        $strings = func_get_args();
109        foreach ($strings as $string) {
110            $this->addIndentation();
111            $this->source .= $string;
112        }
113
114        return $this;
115    }
116
117    public function addIndentation()
118    {
119        $this->source .= str_repeat(' ', $this->indentation * 4);
120
121        return $this;
122    }
123
124    /**
125     * Adds a quoted string to the compiled code.
126     *
127     * @param  string $string The string
128     *
129     * @return Twig_Compiler The current compiler instance
130     */
131    public function string($value)
132    {
133        $this->source .= sprintf('"%s"', addcslashes($value, "\t\"\$\\"));
134
135        return $this;
136    }
137
138    /**
139     * Returns a PHP representation of a given value.
140     *
141     * @param  mixed $value The value to convert
142     *
143     * @return Twig_Compiler The current compiler instance
144     */
145    public function repr($value)
146    {
147        if (is_int($value) || is_float($value)) {
148            $this->raw($value);
149        } else if (null === $value) {
150            $this->raw('null');
151        } else if (is_bool($value)) {
152            $this->raw($value ? 'true' : 'false');
153        } else if (is_array($value)) {
154            $this->raw('array(');
155            $i = 0;
156            foreach ($value as $key => $value) {
157                if ($i++) {
158                    $this->raw(', ');
159                }
160                $this->repr($key);
161                $this->raw(' => ');
162                $this->repr($value);
163            }
164            $this->raw(')');
165        } else {
166            $this->string($value);
167        }
168
169        return $this;
170    }
171
172    /**
173     * Adds debugging information.
174     *
175     * @param Twig_NodeInterface $node The related twig node
176     *
177     * @return Twig_Compiler The current compiler instance
178     */
179    public function addDebugInfo(Twig_NodeInterface $node)
180    {
181        if ($node->getLine() != $this->lastLine) {
182            $this->lastLine = $node->getLine();
183            $this->write("// line {$node->getLine()}\n");
184        }
185
186        return $this;
187    }
188
189    /**
190     * Indents the generated code.
191     *
192     * @param integer $indent The number of indentation to add
193     *
194     * @return Twig_Compiler The current compiler instance
195     */
196    public function indent($step = 1)
197    {
198        $this->indentation += $step;
199
200        return $this;
201    }
202
203    /**
204     * Outdents the generated code.
205     *
206     * @param integer $indent The number of indentation to remove
207     *
208     * @return Twig_Compiler The current compiler instance
209     */
210    public function outdent($step = 1)
211    {
212        $this->indentation -= $step;
213
214        if ($this->indentation < 0) {
215            throw new Twig_Error('Unable to call outdent() as the indentation would become negative');
216        }
217
218        return $this;
219    }
220}
Note: See TracBrowser for help on using the repository browser.