PageRenderTime 21ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/catalogo/myCore/lib/Twig/Compiler.php

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