]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/twig/twig/lib/Twig/Compiler.php
4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
7 * (c) 2009 Armin Ronacher
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;
25 protected $sourceOffset;
26 protected $sourceLine;
32 * @param Twig_Environment $env The twig environment instance
34 public function __construct(Twig_Environment
$env)
37 $this->debugInfo
= array();
40 public function getFilename()
42 return $this->filename
;
46 * Returns the environment instance related to this compiler.
48 * @return Twig_Environment The environment instance
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 integer $indentation The current indentation
71 * @return Twig_Compiler The current compiler instance
73 public function compile(Twig_NodeInterface
$node, $indentation = 0)
75 $this->lastLine
= null;
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 $this->filename
= $node->getAttribute('filename');
86 $node->compile($this);
91 public function subcompile(Twig_NodeInterface
$node, $raw = true)
94 $this->addIndentation();
97 $node->compile($this);
103 * Adds a raw string to the compiled code.
105 * @param string $string The string
107 * @return Twig_Compiler The current compiler instance
109 public function raw($string)
111 $this->source
.= $string;
117 * Writes a string to the compiled code by adding indentation.
119 * @return Twig_Compiler The current compiler instance
121 public function write()
123 $strings = func_get_args();
124 foreach ($strings as $string) {
125 $this->addIndentation();
126 $this->source
.= $string;
133 * Appends an indentation to the current PHP code after compilation.
135 * @return Twig_Compiler The current compiler instance
137 public function addIndentation()
139 $this->source
.= str_repeat(' ', $this->indentation
* 4);
145 * Adds a quoted string to the compiled code.
147 * @param string $value The string
149 * @return Twig_Compiler The current compiler instance
151 public function string($value)
153 $this->source
.= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
159 * Returns a PHP representation of a given value.
161 * @param mixed $value The value to convert
163 * @return Twig_Compiler The current compiler instance
165 public function repr($value)
167 if (is_int($value) || is_float($value)) {
168 if (false !== $locale = setlocale(LC_NUMERIC
, 0)) {
169 setlocale(LC_NUMERIC
, 'C');
174 if (false !== $locale) {
175 setlocale(LC_NUMERIC
, $locale);
177 } elseif (null === $value) {
179 } elseif (is_bool($value)) {
180 $this->raw($value ? 'true' : 'false');
181 } elseif (is_array($value)) {
182 $this->raw('array(');
184 foreach ($value as $key => $value) {
194 $this->string($value);
201 * Adds debugging information.
203 * @param Twig_NodeInterface $node The related twig node
205 * @return Twig_Compiler The current compiler instance
207 public function addDebugInfo(Twig_NodeInterface
$node)
209 if ($node->getLine() != $this->lastLine
) {
210 $this->write("// line {$node->getLine()}\n");
212 // when mbstring.func_overload is set to 2
213 // mb_substr_count() replaces substr_count()
214 // but they have different signatures!
215 if (((int) ini_get('mbstring.func_overload')) & 2) {
216 // this is much slower than the "right" version
217 $this->sourceLine +
= mb_substr_count(mb_substr($this->source
, $this->sourceOffset
), "\n");
219 $this->sourceLine +
= substr_count($this->source
, "\n", $this->sourceOffset
);
221 $this->sourceOffset
= strlen($this->source
);
222 $this->debugInfo
[$this->sourceLine
] = $node->getLine();
224 $this->lastLine
= $node->getLine();
230 public function getDebugInfo()
232 return $this->debugInfo
;
236 * Indents the generated code.
238 * @param integer $step The number of indentation to add
240 * @return Twig_Compiler The current compiler instance
242 public function indent($step = 1)
244 $this->indentation +
= $step;
250 * Outdents the generated code.
252 * @param integer $step The number of indentation to remove
254 * @return Twig_Compiler The current compiler instance
256 public function outdent($step = 1)
258 // can't outdent by more steps than the current indentation level
259 if ($this->indentation
< $step) {
260 throw new LogicException('Unable to call outdent() as the indentation would become negative');
263 $this->indentation
-= $step;