Compiler.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Compiles a node to PHP code.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Twig_Compiler implements Twig_CompilerInterface
  18. {
  19. protected $lastLine;
  20. protected $source;
  21. protected $indentation;
  22. protected $env;
  23. /**
  24. * Constructor.
  25. *
  26. * @param Twig_Environment $env The twig environment instance
  27. */
  28. public function __construct(Twig_Environment $env)
  29. {
  30. $this->env = $env;
  31. }
  32. /**
  33. * Returns the environment instance related to this compiler.
  34. *
  35. * @return Twig_Environment The environment instance
  36. */
  37. public function getEnvironment()
  38. {
  39. return $this->env;
  40. }
  41. /**
  42. * Gets the current PHP code after compilation.
  43. *
  44. * @return string The PHP code
  45. */
  46. public function getSource()
  47. {
  48. return $this->source;
  49. }
  50. /**
  51. * Compiles a node.
  52. *
  53. * @param Twig_NodeInterface $node The node to compile
  54. * @param integer $indent The current indentation
  55. *
  56. * @return Twig_Compiler The current compiler instance
  57. */
  58. public function compile(Twig_NodeInterface $node, $indentation = 0)
  59. {
  60. $this->lastLine = null;
  61. $this->source = '';
  62. $this->indentation = $indentation;
  63. $node->compile($this);
  64. return $this;
  65. }
  66. public function subcompile(Twig_NodeInterface $node, $raw = true)
  67. {
  68. if (false === $raw) {
  69. $this->addIndentation();
  70. }
  71. $node->compile($this);
  72. return $this;
  73. }
  74. /**
  75. * Adds a raw string to the compiled code.
  76. *
  77. * @param string $string The string
  78. *
  79. * @return Twig_Compiler The current compiler instance
  80. */
  81. public function raw($string)
  82. {
  83. $this->source .= $string;
  84. return $this;
  85. }
  86. /**
  87. * Writes a string to the compiled code by adding indentation.
  88. *
  89. * @return Twig_Compiler The current compiler instance
  90. */
  91. public function write()
  92. {
  93. $strings = func_get_args();
  94. foreach ($strings as $string) {
  95. $this->addIndentation();
  96. $this->source .= $string;
  97. }
  98. return $this;
  99. }
  100. public function addIndentation()
  101. {
  102. $this->source .= str_repeat(' ', $this->indentation * 4);
  103. return $this;
  104. }
  105. /**
  106. * Adds a quoted string to the compiled code.
  107. *
  108. * @param string $string The string
  109. *
  110. * @return Twig_Compiler The current compiler instance
  111. */
  112. public function string($value)
  113. {
  114. $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
  115. return $this;
  116. }
  117. /**
  118. * Returns a PHP representation of a given value.
  119. *
  120. * @param mixed $value The value to convert
  121. *
  122. * @return Twig_Compiler The current compiler instance
  123. */
  124. public function repr($value)
  125. {
  126. if (is_int($value) || is_float($value)) {
  127. $this->raw($value);
  128. } else if (null === $value) {
  129. $this->raw('null');
  130. } else if (is_bool($value)) {
  131. $this->raw($value ? 'true' : 'false');
  132. } else if (is_array($value)) {
  133. $this->raw('array(');
  134. $i = 0;
  135. foreach ($value as $key => $value) {
  136. if ($i++) {
  137. $this->raw(', ');
  138. }
  139. $this->repr($key);
  140. $this->raw(' => ');
  141. $this->repr($value);
  142. }
  143. $this->raw(')');
  144. } else {
  145. $this->string($value);
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Adds debugging information.
  151. *
  152. * @param Twig_NodeInterface $node The related twig node
  153. *
  154. * @return Twig_Compiler The current compiler instance
  155. */
  156. public function addDebugInfo(Twig_NodeInterface $node)
  157. {
  158. if ($node->getLine() != $this->lastLine) {
  159. $this->lastLine = $node->getLine();
  160. $this->write("// line {$node->getLine()}\n");
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Indents the generated code.
  166. *
  167. * @param integer $indent The number of indentation to add
  168. *
  169. * @return Twig_Compiler The current compiler instance
  170. */
  171. public function indent($step = 1)
  172. {
  173. $this->indentation += $step;
  174. return $this;
  175. }
  176. /**
  177. * Outdents the generated code.
  178. *
  179. * @param integer $indent The number of indentation to remove
  180. *
  181. * @return Twig_Compiler The current compiler instance
  182. */
  183. public function outdent($step = 1)
  184. {
  185. $this->indentation -= $step;
  186. if ($this->indentation < 0) {
  187. throw new Twig_Error('Unable to call outdent() as the indentation would become negative');
  188. }
  189. return $this;
  190. }
  191. }