Compiler.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /**
  107. * Appends an indentation to the current PHP code after compilation.
  108. *
  109. * @return Twig_Compiler The current compiler instance
  110. */
  111. public function addIndentation()
  112. {
  113. $this->source .= str_repeat(' ', $this->indentation * 4);
  114. return $this;
  115. }
  116. /**
  117. * Adds a quoted string to the compiled code.
  118. *
  119. * @param string $value The string
  120. *
  121. * @return Twig_Compiler The current compiler instance
  122. */
  123. public function string($value)
  124. {
  125. $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
  126. return $this;
  127. }
  128. /**
  129. * Returns a PHP representation of a given value.
  130. *
  131. * @param mixed $value The value to convert
  132. *
  133. * @return Twig_Compiler The current compiler instance
  134. */
  135. public function repr($value)
  136. {
  137. if (is_int($value) || is_float($value)) {
  138. if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
  139. setlocale(LC_NUMERIC, 'C');
  140. }
  141. $this->raw($value);
  142. if (false !== $locale) {
  143. setlocale(LC_NUMERIC, $locale);
  144. }
  145. } elseif (null === $value) {
  146. $this->raw('null');
  147. } elseif (is_bool($value)) {
  148. $this->raw($value ? 'true' : 'false');
  149. } elseif (is_array($value)) {
  150. $this->raw('array(');
  151. $i = 0;
  152. foreach ($value as $key => $value) {
  153. if ($i++) {
  154. $this->raw(', ');
  155. }
  156. $this->repr($key);
  157. $this->raw(' => ');
  158. $this->repr($value);
  159. }
  160. $this->raw(')');
  161. } else {
  162. $this->string($value);
  163. }
  164. return $this;
  165. }
  166. /**
  167. * Adds debugging information.
  168. *
  169. * @param Twig_NodeInterface $node The related twig node
  170. *
  171. * @return Twig_Compiler The current compiler instance
  172. */
  173. public function addDebugInfo(Twig_NodeInterface $node)
  174. {
  175. if ($node->getLine() != $this->lastLine) {
  176. // when mbstring.func_overload is set to 2
  177. // mb_substr_count() replaces substr_count()
  178. // but they have different signatures!
  179. if (((int) ini_get('mbstring.func_overload')) & 2) {
  180. // this is much slower than the "right" version
  181. $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n");
  182. } else {
  183. $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
  184. }
  185. $this->sourceOffset = strlen($this->source);
  186. $this->debugInfo[$this->sourceLine] = $node->getLine();
  187. $this->lastLine = $node->getLine();
  188. $this->write("// line {$node->getLine()}\n");
  189. }
  190. return $this;
  191. }
  192. public function getDebugInfo()
  193. {
  194. return $this->debugInfo;
  195. }
  196. /**
  197. * Indents the generated code.
  198. *
  199. * @param integer $step The number of indentation to add
  200. *
  201. * @return Twig_Compiler The current compiler instance
  202. */
  203. public function indent($step = 1)
  204. {
  205. $this->indentation += $step;
  206. return $this;
  207. }
  208. /**
  209. * Outdents the generated code.
  210. *
  211. * @param integer $step The number of indentation to remove
  212. *
  213. * @return Twig_Compiler The current compiler instance
  214. */
  215. public function outdent($step = 1)
  216. {
  217. // can't outdent by more steps that the current indentation level
  218. if ($this->indentation < $step) {
  219. throw new Twig_Error('Unable to call outdent() as the indentation would become negative');
  220. }
  221. $this->indentation -= $step;
  222. return $this;
  223. }
  224. }