Twig_Compiler

Compiles a node to PHP code.

Defined (1)

The class is defined in the following location(s).

/vendor/twig/twig/lib/Twig/Compiler.php  
  1. class Twig_Compiler implements Twig_CompilerInterface 
  2. protected $lastLine; 
  3. protected $source; 
  4. protected $indentation; 
  5. protected $env; 
  6. protected $debugInfo = array(); 
  7. protected $sourceOffset; 
  8. protected $sourceLine; 
  9. protected $filename; 
  10.  
  11. public function __construct(Twig_Environment $env) 
  12. $this->env = $env; 
  13.  
  14. /** 
  15. * @deprecated since 1.25 (to be removed in 2.0) 
  16. */ 
  17. public function getFilename() 
  18. @trigger_error(sprintf('The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED); 
  19.  
  20. return $this->filename; 
  21.  
  22. /** 
  23. * Returns the environment instance related to this compiler. 
  24. * @return Twig_Environment 
  25. */ 
  26. public function getEnvironment() 
  27. return $this->env; 
  28.  
  29. /** 
  30. * Gets the current PHP code after compilation. 
  31. * @return string The PHP code 
  32. */ 
  33. public function getSource() 
  34. return $this->source; 
  35.  
  36. /** 
  37. * Compiles a node. 
  38. * @param Twig_NodeInterface $node The node to compile 
  39. * @param int $indentation The current indentation 
  40. * @return $this 
  41. */ 
  42. public function compile(Twig_NodeInterface $node, $indentation = 0) 
  43. $this->lastLine = null; 
  44. $this->source = ''; 
  45. $this->debugInfo = array(); 
  46. $this->sourceOffset = 0; 
  47. // source code starts at 1 (as we then increment it when we encounter new lines) 
  48. $this->sourceLine = 1; 
  49. $this->indentation = $indentation; 
  50.  
  51. if ($node instanceof Twig_Node_Module) { 
  52. // to be removed in 2.0 
  53. $this->filename = $node->getTemplateName(); 
  54.  
  55. $node->compile($this); 
  56.  
  57. return $this; 
  58.  
  59. public function subcompile(Twig_NodeInterface $node, $raw = true) 
  60. if (false === $raw) { 
  61. $this->source .= str_repeat(' ', $this->indentation * 4); 
  62.  
  63. $node->compile($this); 
  64.  
  65. return $this; 
  66.  
  67. /** 
  68. * Adds a raw string to the compiled code. 
  69. * @param string $string The string 
  70. * @return $this 
  71. */ 
  72. public function raw($string) 
  73. $this->source .= $string; 
  74.  
  75. return $this; 
  76.  
  77. /** 
  78. * Writes a string to the compiled code by adding indentation. 
  79. * @return $this 
  80. */ 
  81. public function write() 
  82. $strings = func_get_args(); 
  83. foreach ($strings as $string) { 
  84. $this->source .= str_repeat(' ', $this->indentation * 4).$string; 
  85.  
  86. return $this; 
  87.  
  88. /** 
  89. * Appends an indentation to the current PHP code after compilation. 
  90. * @return $this 
  91. * @deprecated since 1.27 (to be removed in 2.0). 
  92. */ 
  93. public function addIndentation() 
  94. @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use write(\'\') instead.', E_USER_DEPRECATED); 
  95.  
  96. $this->source .= str_repeat(' ', $this->indentation * 4); 
  97.  
  98. return $this; 
  99.  
  100. /** 
  101. * Adds a quoted string to the compiled code. 
  102. * @param string $value The string 
  103. * @return $this 
  104. */ 
  105. public function string($value) 
  106. $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\")); 
  107.  
  108. return $this; 
  109.  
  110. /** 
  111. * Returns a PHP representation of a given value. 
  112. * @param mixed $value The value to convert 
  113. * @return $this 
  114. */ 
  115. public function repr($value) 
  116. if (is_int($value) || is_float($value)) { 
  117. if (false !== $locale = setlocale(LC_NUMERIC, '0')) { 
  118. setlocale(LC_NUMERIC, 'C'); 
  119.  
  120. $this->raw($value); 
  121.  
  122. if (false !== $locale) { 
  123. setlocale(LC_NUMERIC, $locale); 
  124. } elseif (null === $value) { 
  125. $this->raw('null'); 
  126. } elseif (is_bool($value)) { 
  127. $this->raw($value ? 'true' : 'false'); 
  128. } elseif (is_array($value)) { 
  129. $this->raw('array('); 
  130. $first = true; 
  131. foreach ($value as $key => $v) { 
  132. if (!$first) { 
  133. $this->raw(', '); 
  134. $first = false; 
  135. $this->repr($key); 
  136. $this->raw(' => '); 
  137. $this->repr($v); 
  138. $this->raw(')'); 
  139. } else { 
  140. $this->string($value); 
  141.  
  142. return $this; 
  143.  
  144. /** 
  145. * Adds debugging information. 
  146. * @return $this 
  147. */ 
  148. public function addDebugInfo(Twig_NodeInterface $node) 
  149. if ($node->getTemplateLine() != $this->lastLine) { 
  150. $this->write(sprintf("// line %d\n", $node->getTemplateLine())); 
  151.  
  152. // when mbstring.func_overload is set to 2 
  153. // mb_substr_count() replaces substr_count() 
  154. // but they have different signatures! 
  155. if (((int) ini_get('mbstring.func_overload')) & 2) { 
  156. @trigger_error('Support for having "mbstring.func_overload" different from 0 is deprecated version 1.29 and will be removed in 2.0.', E_USER_DEPRECATED); 
  157.  
  158. // this is much slower than the "right" version 
  159. $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n"); 
  160. } else { 
  161. $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset); 
  162. $this->sourceOffset = strlen($this->source); 
  163. $this->debugInfo[$this->sourceLine] = $node->getTemplateLine(); 
  164.  
  165. $this->lastLine = $node->getTemplateLine(); 
  166.  
  167. return $this; 
  168.  
  169. public function getDebugInfo() 
  170. ksort($this->debugInfo); 
  171.  
  172. return $this->debugInfo; 
  173.  
  174. /** 
  175. * Indents the generated code. 
  176. * @param int $step The number of indentation to add 
  177. * @return $this 
  178. */ 
  179. public function indent($step = 1) 
  180. $this->indentation += $step; 
  181.  
  182. return $this; 
  183.  
  184. /** 
  185. * Outdents the generated code. 
  186. * @param int $step The number of indentation to remove 
  187. * @return $this 
  188. * @throws LogicException When trying to outdent too much so the indentation would become negative 
  189. */ 
  190. public function outdent($step = 1) 
  191. // can't outdent by more steps than the current indentation level 
  192. if ($this->indentation < $step) { 
  193. throw new LogicException('Unable to call outdent() as the indentation would become negative.'); 
  194.  
  195. $this->indentation -= $step; 
  196.  
  197. return $this; 
  198.  
  199. public function getVarName() 
  200. return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));