PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/smarty/sysplugins/smarty_internal_parsetree.php

http://github.com/modolabs/Kurogo-Mobile-Web
PHP | 236 lines | 184 code | 21 blank | 31 comment | 43 complexity | f30e277f260006da67d5451a61a68e97 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Templateparser Parsetrees
  4. *
  5. * These are classes to build parsetrees in the template parser
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Thue Kristensen
  10. * @author Uwe Tews
  11. */
  12. abstract class _smarty_parsetree {
  13. abstract public function to_smarty_php();
  14. }
  15. /**
  16. * A complete smarty tag.
  17. */
  18. class _smarty_tag extends _smarty_parsetree
  19. {
  20. public $parser;
  21. public $data;
  22. public $saved_block_nesting;
  23. function __construct($parser, $data)
  24. {
  25. $this->parser = $parser;
  26. $this->data = $data;
  27. $this->saved_block_nesting = $parser->block_nesting_level;
  28. }
  29. public function to_smarty_php()
  30. {
  31. return $this->data;
  32. }
  33. public function assign_to_var()
  34. {
  35. $var = sprintf('$_tmp%d', ++$this->parser->prefix_number);
  36. $this->parser->compiler->prefix_code[] = sprintf('<?php ob_start();?>%s<?php %s=ob_get_clean();?>',
  37. $this->data, $var);
  38. return $var;
  39. }
  40. }
  41. /**
  42. * Code fragment inside a tag.
  43. */
  44. class _smarty_code extends _smarty_parsetree {
  45. public $parser;
  46. public $data;
  47. function __construct($parser, $data)
  48. {
  49. $this->parser = $parser;
  50. $this->data = $data;
  51. }
  52. public function to_smarty_php()
  53. {
  54. return sprintf("(%s)", $this->data);
  55. }
  56. }
  57. /**
  58. * Double quoted string inside a tag.
  59. */
  60. class _smarty_doublequoted extends _smarty_parsetree {
  61. public $parser;
  62. public $subtrees = Array();
  63. function __construct($parser, _smarty_parsetree $subtree)
  64. {
  65. $this->parser = $parser;
  66. $this->subtrees[] = $subtree;
  67. if ($subtree instanceof _smarty_tag) {
  68. $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack);
  69. }
  70. }
  71. function append_subtree(_smarty_parsetree $subtree)
  72. {
  73. $last_subtree = count($this->subtrees)-1;
  74. if ($last_subtree >= 0 && $this->subtrees[$last_subtree] instanceof _smarty_tag && $this->subtrees[$last_subtree]->saved_block_nesting < $this->parser->block_nesting_level) {
  75. if ($subtree instanceof _smarty_code) {
  76. $this->subtrees[$last_subtree]->data .= '<?php echo ' . $subtree->data . ';?>';
  77. } elseif ($subtree instanceof _smarty_dq_content) {
  78. $this->subtrees[$last_subtree]->data .= '<?php echo "' . $subtree->data . '";?>';
  79. } else {
  80. $this->subtrees[$last_subtree]->data .= $subtree->data;
  81. }
  82. } else {
  83. $this->subtrees[] = $subtree;
  84. }
  85. if ($subtree instanceof _smarty_tag) {
  86. $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack);
  87. }
  88. }
  89. public function to_smarty_php()
  90. {
  91. $code = '';
  92. foreach ($this->subtrees as $subtree) {
  93. if ($code !== "") {
  94. $code .= ".";
  95. }
  96. if ($subtree instanceof _smarty_tag) {
  97. $more_php = $subtree->assign_to_var();
  98. } else {
  99. $more_php = $subtree->to_smarty_php();
  100. }
  101. $code .= $more_php;
  102. if (!$subtree instanceof _smarty_dq_content) {
  103. $this->parser->compiler->has_variable_string = true;
  104. }
  105. }
  106. return $code;
  107. }
  108. }
  109. /**
  110. * Raw chars as part of a double quoted string.
  111. */
  112. class _smarty_dq_content extends _smarty_parsetree {
  113. public $data;
  114. function __construct($parser, $data)
  115. {
  116. $this->parser = $parser;
  117. $this->data = $data;
  118. }
  119. public function to_smarty_php()
  120. {
  121. return '"' . $this->data . '"';
  122. }
  123. }
  124. /**
  125. * Template element
  126. */
  127. class _smarty_template_buffer extends _smarty_parsetree {
  128. public $subtrees = Array();
  129. function __construct($parser)
  130. {
  131. $this->parser = $parser;
  132. }
  133. function append_subtree(_smarty_parsetree $subtree)
  134. {
  135. $this->subtrees[] = $subtree;
  136. }
  137. public function to_smarty_php()
  138. {
  139. $code = '';
  140. for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key++) {
  141. if ($key + 2 < $cnt) {
  142. if ($this->subtrees[$key] instanceof _smarty_linebreak && $this->subtrees[$key + 1] instanceof _smarty_tag && $this->subtrees[$key + 1]->data == '' && $this->subtrees[$key + 2] instanceof _smarty_linebreak) {
  143. $key = $key + 1;
  144. continue;
  145. }
  146. if (substr($this->subtrees[$key]->data, -1) == '<' && $this->subtrees[$key + 1]->data == '' && substr($this->subtrees[$key + 2]->data, -1) == '?') {
  147. $key = $key + 2;
  148. continue;
  149. }
  150. }
  151. if (substr($code, -1) == '<') {
  152. $subtree = $this->subtrees[$key]->to_smarty_php();
  153. if (substr($subtree, 0, 1) == '?') {
  154. $code = substr($code, 0, strlen($code)-1) . '<<?php ?>?' . substr($subtree, 1);
  155. } elseif ($this->parser->asp_tags && substr($subtree, 0, 1) == '%') {
  156. $code = substr($code, 0, strlen($code)-1) . '<<?php ?>%' . substr($subtree, 1);
  157. } else {
  158. $code .= $subtree;
  159. }
  160. continue;
  161. }
  162. if ($this->parser->asp_tags && substr($code, -1) == '%') {
  163. $subtree = $this->subtrees[$key]->to_smarty_php();
  164. if (substr($subtree, 0, 1) == '>') {
  165. $code = substr($code, 0, strlen($code)-1) . '%<?php ?>>' . substr($subtree, 1);
  166. } else {
  167. $code .= $subtree;
  168. }
  169. continue;
  170. }
  171. if (substr($code, -1) == '?') {
  172. $subtree = $this->subtrees[$key]->to_smarty_php();
  173. if (substr($subtree, 0, 1) == '>') {
  174. $code = substr($code, 0, strlen($code)-1) . '?<?php ?>>' . substr($subtree, 1);
  175. } else {
  176. $code .= $subtree;
  177. }
  178. continue;
  179. }
  180. $code .= $this->subtrees[$key]->to_smarty_php();
  181. }
  182. return $code;
  183. }
  184. }
  185. /**
  186. * template text
  187. */
  188. class _smarty_text extends _smarty_parsetree {
  189. public $data;
  190. function __construct($parser, $data)
  191. {
  192. $this->parser = $parser;
  193. $this->data = $data;
  194. }
  195. public function to_smarty_php()
  196. {
  197. return $this->data;
  198. }
  199. }
  200. /**
  201. * template linebreaks
  202. */
  203. class _smarty_linebreak extends _smarty_parsetree {
  204. public $data;
  205. function __construct($parser, $data)
  206. {
  207. $this->parser = $parser;
  208. $this->data = $data;
  209. }
  210. public function to_smarty_php()
  211. {
  212. return $this->data;
  213. }
  214. }
  215. ?>