/vendor/twig/twig/src/Compiler.php

https://github.com/pct/4money · PHP · 288 lines · 202 code · 24 blank · 62 comment · 2 complexity · 912e07b7d8c03b3acf6e16cb85a68b98 MD5 · raw file

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