e90bc987d7a27bcc95236ad6373894e9d36f2f84
[yaffs-website] / vendor / twig / twig / lib / Twig / Compiler.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  * (c) 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  * @author Fabien Potencier <fabien@symfony.com>
17  */
18 class Twig_Compiler implements Twig_CompilerInterface
19 {
20     protected $lastLine;
21     protected $source;
22     protected $indentation;
23     protected $env;
24     protected $debugInfo = array();
25     protected $sourceOffset;
26     protected $sourceLine;
27     protected $filename;
28
29     public function __construct(Twig_Environment $env)
30     {
31         $this->env = $env;
32     }
33
34     /**
35      * @deprecated since 1.25 (to be removed in 2.0)
36      */
37     public function getFilename()
38     {
39         @trigger_error(sprintf('The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);
40
41         return $this->filename;
42     }
43
44     /**
45      * Returns the environment instance related to this compiler.
46      *
47      * @return Twig_Environment
48      */
49     public function getEnvironment()
50     {
51         return $this->env;
52     }
53
54     /**
55      * Gets the current PHP code after compilation.
56      *
57      * @return string The PHP code
58      */
59     public function getSource()
60     {
61         return $this->source;
62     }
63
64     /**
65      * Compiles a node.
66      *
67      * @param Twig_NodeInterface $node        The node to compile
68      * @param int                $indentation The current indentation
69      *
70      * @return $this
71      */
72     public function compile(Twig_NodeInterface $node, $indentation = 0)
73     {
74         $this->lastLine = null;
75         $this->source = '';
76         $this->debugInfo = array();
77         $this->sourceOffset = 0;
78         // source code starts at 1 (as we then increment it when we encounter new lines)
79         $this->sourceLine = 1;
80         $this->indentation = $indentation;
81
82         if ($node instanceof Twig_Node_Module) {
83             // to be removed in 2.0
84             $this->filename = $node->getTemplateName();
85         }
86
87         $node->compile($this);
88
89         return $this;
90     }
91
92     public function subcompile(Twig_NodeInterface $node, $raw = true)
93     {
94         if (false === $raw) {
95             $this->source .= str_repeat(' ', $this->indentation * 4);
96         }
97
98         $node->compile($this);
99
100         return $this;
101     }
102
103     /**
104      * Adds a raw string to the compiled code.
105      *
106      * @param string $string The string
107      *
108      * @return $this
109      */
110     public function raw($string)
111     {
112         $this->source .= $string;
113
114         return $this;
115     }
116
117     /**
118      * Writes a string to the compiled code by adding indentation.
119      *
120      * @return $this
121      */
122     public function write()
123     {
124         $strings = func_get_args();
125         foreach ($strings as $string) {
126             $this->source .= str_repeat(' ', $this->indentation * 4).$string;
127         }
128
129         return $this;
130     }
131
132     /**
133      * Appends an indentation to the current PHP code after compilation.
134      *
135      * @return $this
136      *
137      * @deprecated since 1.27 (to be removed in 2.0).
138      */
139     public function addIndentation()
140     {
141         @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use write(\'\') instead.', E_USER_DEPRECATED);
142
143         $this->source .= str_repeat(' ', $this->indentation * 4);
144
145         return $this;
146     }
147
148     /**
149      * Adds a quoted string to the compiled code.
150      *
151      * @param string $value The string
152      *
153      * @return $this
154      */
155     public function string($value)
156     {
157         $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
158
159         return $this;
160     }
161
162     /**
163      * Returns a PHP representation of a given value.
164      *
165      * @param mixed $value The value to convert
166      *
167      * @return $this
168      */
169     public function repr($value)
170     {
171         if (is_int($value) || is_float($value)) {
172             if (false !== $locale = setlocale(LC_NUMERIC, '0')) {
173                 setlocale(LC_NUMERIC, 'C');
174             }
175
176             $this->raw($value);
177
178             if (false !== $locale) {
179                 setlocale(LC_NUMERIC, $locale);
180             }
181         } elseif (null === $value) {
182             $this->raw('null');
183         } elseif (is_bool($value)) {
184             $this->raw($value ? 'true' : 'false');
185         } elseif (is_array($value)) {
186             $this->raw('array(');
187             $first = true;
188             foreach ($value as $key => $v) {
189                 if (!$first) {
190                     $this->raw(', ');
191                 }
192                 $first = false;
193                 $this->repr($key);
194                 $this->raw(' => ');
195                 $this->repr($v);
196             }
197             $this->raw(')');
198         } else {
199             $this->string($value);
200         }
201
202         return $this;
203     }
204
205     /**
206      * Adds debugging information.
207      *
208      * @return $this
209      */
210     public function addDebugInfo(Twig_NodeInterface $node)
211     {
212         if ($node->getTemplateLine() != $this->lastLine) {
213             $this->write(sprintf("// line %d\n", $node->getTemplateLine()));
214
215             // when mbstring.func_overload is set to 2
216             // mb_substr_count() replaces substr_count()
217             // but they have different signatures!
218             if (((int) ini_get('mbstring.func_overload')) & 2) {
219                 @trigger_error('Support for having "mbstring.func_overload" different from 0 is deprecated version 1.29 and will be removed in 2.0.', E_USER_DEPRECATED);
220
221                 // this is much slower than the "right" version
222                 $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n");
223             } else {
224                 $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
225             }
226             $this->sourceOffset = strlen($this->source);
227             $this->debugInfo[$this->sourceLine] = $node->getTemplateLine();
228
229             $this->lastLine = $node->getTemplateLine();
230         }
231
232         return $this;
233     }
234
235     public function getDebugInfo()
236     {
237         ksort($this->debugInfo);
238
239         return $this->debugInfo;
240     }
241
242     /**
243      * Indents the generated code.
244      *
245      * @param int $step The number of indentation to add
246      *
247      * @return $this
248      */
249     public function indent($step = 1)
250     {
251         $this->indentation += $step;
252
253         return $this;
254     }
255
256     /**
257      * Outdents the generated code.
258      *
259      * @param int $step The number of indentation to remove
260      *
261      * @return $this
262      *
263      * @throws LogicException When trying to outdent too much so the indentation would become negative
264      */
265     public function outdent($step = 1)
266     {
267         // can't outdent by more steps than the current indentation level
268         if ($this->indentation < $step) {
269             throw new LogicException('Unable to call outdent() as the indentation would become negative.');
270         }
271
272         $this->indentation -= $step;
273
274         return $this;
275     }
276
277     public function getVarName()
278     {
279         return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));
280     }
281 }
282
283 class_alias('Twig_Compiler', 'Twig\Compiler', false);
284 class_exists('Twig_Node');