4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
14 * Compiles a node to PHP code.
16 * @author Fabien Potencier <fabien@symfony.com>
18 class Twig_Compiler implements Twig_CompilerInterface
22 protected $indentation;
24 protected $debugInfo = array();
25 protected $sourceOffset;
26 protected $sourceLine;
29 public function __construct(Twig_Environment $env)
35 * @deprecated since 1.25 (to be removed in 2.0)
37 public function getFilename()
39 @trigger_error(sprintf('The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);
41 return $this->filename;
45 * Returns the environment instance related to this compiler.
47 * @return Twig_Environment
49 public function getEnvironment()
55 * Gets the current PHP code after compilation.
57 * @return string The PHP code
59 public function getSource()
67 * @param Twig_NodeInterface $node The node to compile
68 * @param int $indentation The current indentation
72 public function compile(Twig_NodeInterface $node, $indentation = 0)
74 $this->lastLine = null;
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;
82 if ($node instanceof Twig_Node_Module) {
83 // to be removed in 2.0
84 $this->filename = $node->getTemplateName();
87 $node->compile($this);
92 public function subcompile(Twig_NodeInterface $node, $raw = true)
95 $this->source .= str_repeat(' ', $this->indentation * 4);
98 $node->compile($this);
104 * Adds a raw string to the compiled code.
106 * @param string $string The string
110 public function raw($string)
112 $this->source .= $string;
118 * Writes a string to the compiled code by adding indentation.
122 public function write()
124 $strings = func_get_args();
125 foreach ($strings as $string) {
126 $this->source .= str_repeat(' ', $this->indentation * 4).$string;
133 * Appends an indentation to the current PHP code after compilation.
137 * @deprecated since 1.27 (to be removed in 2.0).
139 public function addIndentation()
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);
143 $this->source .= str_repeat(' ', $this->indentation * 4);
149 * Adds a quoted string to the compiled code.
151 * @param string $value The string
155 public function string($value)
157 $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
163 * Returns a PHP representation of a given value.
165 * @param mixed $value The value to convert
169 public function repr($value)
171 if (is_int($value) || is_float($value)) {
172 if (false !== $locale = setlocale(LC_NUMERIC, '0')) {
173 setlocale(LC_NUMERIC, 'C');
178 if (false !== $locale) {
179 setlocale(LC_NUMERIC, $locale);
181 } elseif (null === $value) {
183 } elseif (is_bool($value)) {
184 $this->raw($value ? 'true' : 'false');
185 } elseif (is_array($value)) {
186 $this->raw('array(');
188 foreach ($value as $key => $v) {
199 $this->string($value);
206 * Adds debugging information.
210 public function addDebugInfo(Twig_NodeInterface $node)
212 if ($node->getTemplateLine() != $this->lastLine) {
213 $this->write(sprintf("// line %d\n", $node->getTemplateLine()));
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);
221 // this is much slower than the "right" version
222 $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n");
224 $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
226 $this->sourceOffset = strlen($this->source);
227 $this->debugInfo[$this->sourceLine] = $node->getTemplateLine();
229 $this->lastLine = $node->getTemplateLine();
235 public function getDebugInfo()
237 ksort($this->debugInfo);
239 return $this->debugInfo;
243 * Indents the generated code.
245 * @param int $step The number of indentation to add
249 public function indent($step = 1)
251 $this->indentation += $step;
257 * Outdents the generated code.
259 * @param int $step The number of indentation to remove
263 * @throws LogicException When trying to outdent too much so the indentation would become negative
265 public function outdent($step = 1)
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.');
272 $this->indentation -= $step;
277 public function getVarName()
279 return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));