Compiler.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. protected $debugInfo;
  24. protected $sourceOffset;
  25. protected $sourceLine;
  26. /**
  27. * Constructor.
  28. *
  29. * @param Twig_Environment $env The twig environment instance
  30. */
  31. public function __construct(Twig_Environment $env)
  32. {
  33. $this->env = $env;
  34. $this->debugInfo = array();
  35. }
  36. /**
  37. * Returns the environment instance related to this compiler.
  38. *
  39. * @return Twig_Environment The environment instance
  40. */
  41. public function getEnvironment()
  42. {
  43. return $this->env;
  44. }
  45. /**
  46. * Gets the current PHP code after compilation.
  47. *
  48. * @return string The PHP code
  49. */
  50. public function getSource()
  51. {
  52. return $this->source;
  53. }
  54. /**
  55. * Compiles a node.
  56. *
  57. * @param Twig_NodeInterface $node The node to compile
  58. * @param integer $indentation The current indentation
  59. *
  60. * @return Twig_Compiler The current compiler instance
  61. */
  62. public function compile(Twig_NodeInterface $node, $indentation = 0)
  63. {
  64. $this->lastLine = null;
  65. $this->source = '';
  66. $this->sourceOffset = 0;
  67. $this->sourceLine = 0;
  68. $this->indentation = $indentation;
  69. $node->compile($this);
  70. return $this;
  71. }
  72. public function subcompile(Twig_NodeInterface $node, $raw = true)
  73. {
  74. if (false === $raw) {
  75. $this->addIndentation();
  76. }
  77. $node->compile($this);
  78. return $this;
  79. }
  80. /**
  81. * Adds a raw string to the compiled code.
  82. *
  83. * @param string $string The string
  84. *
  85. * @return Twig_Compiler The current compiler instance
  86. */
  87. public function raw($string)
  88. {
  89. $this->source .= $string;
  90. return $this;
  91. }
  92. /**
  93. * Writes a string to the compiled code by adding indentation.
  94. *
  95. * @return Twig_Compiler The current compiler instance
  96. */
  97. public function write()
  98. {
  99. $strings = func_get_args();
  100. foreach ($strings as $string) {
  101. $this->addIndentation();
  102. $this->source .= $string;
  103. }
  104. return $this;
  105. }
  106. public function addIndentation()
  107. {
  108. $this->source .= str_repeat(' ', $this->indentation * 4);
  109. return $this;
  110. }
  111. /**
  112. * Adds a quoted string to the compiled code.
  113. *
  114. * @param string $value The string
  115. *
  116. * @return Twig_Compiler The current compiler instance
  117. */
  118. public function string($value)
  119. {
  120. $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
  121. return $this;
  122. }
  123. /**
  124. * Returns a PHP representation of a given value.
  125. *
  126. * @param mixed $value The value to convert
  127. *
  128. * @return Twig_Compiler The current compiler instance
  129. */
  130. public function repr($value)
  131. {
  132. if (is_int($value) || is_float($value)) {
  133. if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
  134. setlocale(LC_NUMERIC, 'C');
  135. }
  136. $this->raw($value);
  137. if (false !== $locale) {
  138. setlocale(LC_NUMERIC, $locale);
  139. }
  140. } elseif (null === $value) {
  141. $this->raw('null');
  142. } elseif (is_bool($value)) {
  143. $this->raw($value ? 'true' : 'false');
  144. } elseif (is_array($value)) {
  145. $this->raw('array(');
  146. $i = 0;
  147. foreach ($value as $key => $value) {
  148. if ($i++) {
  149. $this->raw(', ');
  150. }
  151. $this->repr($key);
  152. $this->raw(' => ');
  153. $this->repr($value);
  154. }
  155. $this->raw(')');
  156. } else {
  157. $this->string($value);
  158. }
  159. return $this;
  160. }
  161. /**
  162. * Adds debugging information.
  163. *
  164. * @param Twig_NodeInterface $node The related twig node
  165. *
  166. * @return Twig_Compiler The current compiler instance
  167. */
  168. public function addDebugInfo(Twig_NodeInterface $node)
  169. {
  170. if ($node->getLine() != $this->lastLine) {
  171. $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
  172. $this->sourceOffset = strlen($this->source);
  173. $this->debugInfo[$this->sourceLine] = $node->getLine();
  174. $this->lastLine = $node->getLine();
  175. $this->write("// line {$node->getLine()}\n");
  176. }
  177. return $this;
  178. }
  179. public function getDebugInfo()
  180. {
  181. return $this->debugInfo;
  182. }
  183. /**
  184. * Indents the generated code.
  185. *
  186. * @param integer $step The number of indentation to add
  187. *
  188. * @return Twig_Compiler The current compiler instance
  189. */
  190. public function indent($step = 1)
  191. {
  192. $this->indentation += $step;
  193. return $this;
  194. }
  195. /**
  196. * Outdents the generated code.
  197. *
  198. * @param integer $step The number of indentation to remove
  199. *
  200. * @return Twig_Compiler The current compiler instance
  201. */
  202. public function outdent($step = 1)
  203. {
  204. $this->indentation -= $step;
  205. if ($this->indentation < 0) {
  206. throw new Twig_Error('Unable to call outdent() as the indentation would become negative');
  207. }
  208. return $this;
  209. }
  210. }