123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
-
-
-
-
- class Twig_Compiler implements Twig_CompilerInterface
- {
- protected $lastLine;
- protected $source;
- protected $indentation;
- protected $env;
-
-
-
- public function __construct(Twig_Environment $env)
- {
- $this->env = $env;
- }
-
-
-
- public function getEnvironment()
- {
- return $this->env;
- }
-
-
-
- public function getSource()
- {
- return $this->source;
- }
-
-
-
- public function compile(Twig_NodeInterface $node, $indentation = 0)
- {
- $this->lastLine = null;
- $this->source = '';
- $this->indentation = $indentation;
-
- $node->compile($this);
-
- return $this;
- }
-
- public function subcompile(Twig_NodeInterface $node, $raw = true)
- {
- if (false === $raw) {
- $this->addIndentation();
- }
-
- $node->compile($this);
-
- return $this;
- }
-
-
-
- public function raw($string)
- {
- $this->source .= $string;
-
- return $this;
- }
-
-
-
- public function write()
- {
- $strings = func_get_args();
- foreach ($strings as $string) {
- $this->addIndentation();
- $this->source .= $string;
- }
-
- return $this;
- }
-
- public function addIndentation()
- {
- $this->source .= str_repeat(' ', $this->indentation * 4);
-
- return $this;
- }
-
-
-
- public function string($value)
- {
- $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
-
- return $this;
- }
-
-
-
- public function repr($value)
- {
- if (is_int($value) || is_float($value)) {
- $this->raw($value);
- } else if (null === $value) {
- $this->raw('null');
- } else if (is_bool($value)) {
- $this->raw($value ? 'true' : 'false');
- } else if (is_array($value)) {
- $this->raw('array(');
- $i = 0;
- foreach ($value as $key => $value) {
- if ($i++) {
- $this->raw(', ');
- }
- $this->repr($key);
- $this->raw(' => ');
- $this->repr($value);
- }
- $this->raw(')');
- } else {
- $this->string($value);
- }
-
- return $this;
- }
-
-
-
- public function addDebugInfo(Twig_NodeInterface $node)
- {
- if ($node->getLine() != $this->lastLine) {
- $this->lastLine = $node->getLine();
- $this->write("// line {$node->getLine()}\n");
- }
-
- return $this;
- }
-
-
-
- public function indent($step = 1)
- {
- $this->indentation += $step;
-
- return $this;
- }
-
-
-
- public function outdent($step = 1)
- {
- $this->indentation -= $step;
-
- if ($this->indentation < 0) {
- throw new Twig_Error('Unable to call outdent() as the indentation would become negative');
- }
-
- return $this;
- }
- }
|