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;
28 private $varNameSalt = 0;
30 public function __construct(Twig_Environment $env)
36 * @deprecated since 1.25 (to be removed in 2.0)
38 public function getFilename()
40 @trigger_error(sprintf('The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);
42 return $this->filename;
46 * Returns the environment instance related to this compiler.
48 * @return Twig_Environment
50 public function getEnvironment()
56 * Gets the current PHP code after compilation.
58 * @return string The PHP code
60 public function getSource()
68 * @param Twig_NodeInterface $node The node to compile
69 * @param int $indentation The current indentation
73 public function compile(Twig_NodeInterface $node, $indentation = 0)
75 $this->lastLine = null;
77 $this->debugInfo = array();
78 $this->sourceOffset = 0;
79 // source code starts at 1 (as we then increment it when we encounter new lines)
80 $this->sourceLine = 1;
81 $this->indentation = $indentation;
82 $this->varNameSalt = 0;
84 if ($node instanceof Twig_Node_Module) {
85 // to be removed in 2.0
86 $this->filename = $node->getTemplateName();
89 $node->compile($this);
94 public function subcompile(Twig_NodeInterface $node, $raw = true)
97 $this->source .= str_repeat(' ', $this->indentation * 4);
100 $node->compile($this);
106 * Adds a raw string to the compiled code.
108 * @param string $string The string
112 public function raw($string)
114 $this->source .= $string;
120 * Writes a string to the compiled code by adding indentation.
124 public function write()
126 $strings = func_get_args();
127 foreach ($strings as $string) {
128 $this->source .= str_repeat(' ', $this->indentation * 4).$string;
135 * Appends an indentation to the current PHP code after compilation.
139 * @deprecated since 1.27 (to be removed in 2.0).
141 public function addIndentation()
143 @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use write(\'\') instead.', E_USER_DEPRECATED);
145 $this->source .= str_repeat(' ', $this->indentation * 4);
151 * Adds a quoted string to the compiled code.
153 * @param string $value The string
157 public function string($value)
159 $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
165 * Returns a PHP representation of a given value.
167 * @param mixed $value The value to convert
171 public function repr($value)
173 if (is_int($value) || is_float($value)) {
174 if (false !== $locale = setlocale(LC_NUMERIC, '0')) {
175 setlocale(LC_NUMERIC, 'C');
180 if (false !== $locale) {
181 setlocale(LC_NUMERIC, $locale);
183 } elseif (null === $value) {
185 } elseif (is_bool($value)) {
186 $this->raw($value ? 'true' : 'false');
187 } elseif (is_array($value)) {
188 $this->raw('array(');
190 foreach ($value as $key => $v) {
201 $this->string($value);
208 * Adds debugging information.
212 public function addDebugInfo(Twig_NodeInterface $node)
214 if ($node->getTemplateLine() != $this->lastLine) {
215 $this->write(sprintf("// line %d\n", $node->getTemplateLine()));
217 // when mbstring.func_overload is set to 2
218 // mb_substr_count() replaces substr_count()
219 // but they have different signatures!
220 if (((int) ini_get('mbstring.func_overload')) & 2) {
221 @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);
223 // this is much slower than the "right" version
224 $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n");
226 $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
228 $this->sourceOffset = strlen($this->source);
229 $this->debugInfo[$this->sourceLine] = $node->getTemplateLine();
231 $this->lastLine = $node->getTemplateLine();
237 public function getDebugInfo()
239 ksort($this->debugInfo);
241 return $this->debugInfo;
245 * Indents the generated code.
247 * @param int $step The number of indentation to add
251 public function indent($step = 1)
253 $this->indentation += $step;
259 * Outdents the generated code.
261 * @param int $step The number of indentation to remove
265 * @throws LogicException When trying to outdent too much so the indentation would become negative
267 public function outdent($step = 1)
269 // can't outdent by more steps than the current indentation level
270 if ($this->indentation < $step) {
271 throw new LogicException('Unable to call outdent() as the indentation would become negative.');
274 $this->indentation -= $step;
279 public function getVarName()
281 return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->varNameSalt++));
285 class_alias('Twig_Compiler', 'Twig\Compiler', false);
286 class_exists('Twig_Node');