PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Smarty/libs/sysplugins/smarty_internal_compile_include.php

https://github.com/ywl890227/longphp
PHP | 258 lines | 200 code | 4 blank | 54 comment | 13 complexity | 7b179cacbf3342e4c265b5e59f24b633 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Include
  4. *
  5. * Compiles the {include} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Include Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
  18. {
  19. /**
  20. * caching mode to create nocache code but no cache file
  21. */
  22. const CACHING_NOCACHE_CODE = 9999;
  23. /**
  24. * Attribute definition: Overwrites base class.
  25. *
  26. * @var array
  27. * @see Smarty_Internal_CompileBase
  28. */
  29. public $required_attributes = array('file');
  30. /**
  31. * Attribute definition: Overwrites base class.
  32. *
  33. * @var array
  34. * @see Smarty_Internal_CompileBase
  35. */
  36. public $shorttag_order = array('file');
  37. /**
  38. * Attribute definition: Overwrites base class.
  39. *
  40. * @var array
  41. * @see Smarty_Internal_CompileBase
  42. */
  43. public $option_flags = array('nocache', 'inline', 'caching');
  44. /**
  45. * Attribute definition: Overwrites base class.
  46. *
  47. * @var array
  48. * @see Smarty_Internal_CompileBase
  49. */
  50. public $optional_attributes = array('_any');
  51. /**
  52. * Compiles code for the {include} tag
  53. *
  54. * @param array $args array with attributes from parser
  55. * @param object $compiler compiler object
  56. * @param array $parameter array with compilation parameter
  57. * @return string compiled code
  58. */
  59. public function compile($args, $compiler, $parameter)
  60. {
  61. // check and get attributes
  62. $_attr = $this->getAttributes($compiler, $args);
  63. // save posible attributes
  64. $include_file = $_attr['file'];
  65. if (isset($_attr['assign'])) {
  66. // output will be stored in a smarty variable instead of beind displayed
  67. $_assign = $_attr['assign'];
  68. }
  69. $_parent_scope = Smarty::SCOPE_LOCAL;
  70. if (isset($_attr['scope'])) {
  71. $_attr['scope'] = trim($_attr['scope'], "'\"");
  72. if ($_attr['scope'] == 'parent') {
  73. $_parent_scope = Smarty::SCOPE_PARENT;
  74. } elseif ($_attr['scope'] == 'root') {
  75. $_parent_scope = Smarty::SCOPE_ROOT;
  76. } elseif ($_attr['scope'] == 'global') {
  77. $_parent_scope = Smarty::SCOPE_GLOBAL;
  78. }
  79. }
  80. $_caching = Smarty::CACHING_OFF;
  81. // flag if included template code should be merged into caller
  82. $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes ||($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes)|| $_attr['inline'] === true) && !$compiler->template->source->recompiled;
  83. // set default when in nocache mode
  84. // if ($compiler->template->caching && ($compiler->nocache || $compiler->tag_nocache || $compiler->forceNocache == 2)) {
  85. if ($compiler->template->caching && ((!$compiler->inheritance && !$compiler->nocache && !$compiler->tag_nocache) || ($compiler->inheritance && ($compiler->nocache ||$compiler->tag_nocache)))) {
  86. $_caching = self::CACHING_NOCACHE_CODE;
  87. }
  88. /*
  89. * if the {include} tag provides individual parameter for caching
  90. * it will not be included into the common cache file and treated like
  91. * a nocache section
  92. */
  93. if (isset($_attr['cache_lifetime'])) {
  94. $_cache_lifetime = $_attr['cache_lifetime'];
  95. $compiler->tag_nocache = true;
  96. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  97. } else {
  98. $_cache_lifetime = 'null';
  99. }
  100. if (isset($_attr['cache_id'])) {
  101. $_cache_id = $_attr['cache_id'];
  102. $compiler->tag_nocache = true;
  103. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  104. } else {
  105. $_cache_id = '$_smarty_tpl->cache_id';
  106. }
  107. if (isset($_attr['compile_id'])) {
  108. $_compile_id = $_attr['compile_id'];
  109. } else {
  110. $_compile_id = '$_smarty_tpl->compile_id';
  111. }
  112. if ($_attr['caching'] === true) {
  113. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  114. }
  115. if ($_attr['nocache'] === true) {
  116. $compiler->tag_nocache = true;
  117. if ($merge_compiled_includes) {
  118. $_caching = self::CACHING_NOCACHE_CODE;
  119. } else {
  120. $_caching = Smarty::CACHING_OFF;
  121. }
  122. }
  123. $has_compiled_template = false;
  124. if ($merge_compiled_includes && $_attr['inline'] !== true) {
  125. // variable template name ?
  126. if ($compiler->has_variable_string || !((substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2))
  127. || substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0
  128. ) {
  129. $merge_compiled_includes = false;
  130. if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
  131. $compiler->trigger_template_error(' variable template file names not allow within {block} tags');
  132. }
  133. }
  134. // variable compile_id?
  135. if (isset($_attr['compile_id'])) {
  136. if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2))
  137. || substr_count($_attr['compile_id'], '(') != 0 || substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0
  138. ) {
  139. $merge_compiled_includes = false;
  140. if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
  141. $compiler->trigger_template_error(' variable compile_id not allow within {block} tags');
  142. }
  143. }
  144. }
  145. }
  146. if ($merge_compiled_includes) {
  147. if ($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache) && $_caching != self::CACHING_NOCACHE_CODE) {
  148. $merge_compiled_includes = false;
  149. if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
  150. $compiler->trigger_template_error(' invalid caching mode of subtemplate within {block} tags');
  151. }
  152. }
  153. }
  154. if ($merge_compiled_includes) {
  155. // we must observe different compile_id
  156. $uid = sha1($_compile_id);
  157. $tpl_name = null;
  158. $nocache = false;
  159. $_smarty_tpl = $compiler->template;
  160. eval("\$tpl_name = $include_file;");
  161. if (!isset($compiler->smarty->merged_templates_func[$tpl_name][$uid]) || $compiler->inheritance) {
  162. $tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
  163. // save unique function name
  164. $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] = $tpl->properties['unifunc'] = 'content_' . str_replace('.', '_', uniqid('', true));
  165. // use current nocache hash for inlined code
  166. $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  167. if ($compiler->template->caching && $_caching == self::CACHING_NOCACHE_CODE) {
  168. // all code must be nocache
  169. $nocache = true;
  170. }
  171. if ($compiler->inheritance) {
  172. $tpl->compiler->inheritance = true;
  173. }
  174. // make sure whole chain gets compiled
  175. $tpl->mustCompile = true;
  176. if (!($tpl->source->uncompiled) && $tpl->source->exists) {
  177. // get compiled code
  178. $compiled_code = $tpl->compiler->compileTemplate($tpl, $nocache);
  179. // release compiler object to free memory
  180. unset($tpl->compiler);
  181. // merge compiled code for {function} tags
  182. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
  183. // merge filedependency
  184. $tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp, $tpl->source->type);
  185. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
  186. // remove header code
  187. $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
  188. if ($tpl->has_nocache_code) {
  189. // replace nocache_hash
  190. $compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
  191. $compiler->template->has_nocache_code = true;
  192. }
  193. $compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
  194. $has_compiled_template = true;
  195. unset ($tpl);
  196. }
  197. } else {
  198. $has_compiled_template = true;
  199. }
  200. }
  201. // delete {include} standard attributes
  202. unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
  203. // remaining attributes must be assigned as smarty variable
  204. if (!empty($_attr)) {
  205. if ($_parent_scope == Smarty::SCOPE_LOCAL) {
  206. // create variables
  207. foreach ($_attr as $key => $value) {
  208. $_pairs[] = "'$key'=>$value";
  209. }
  210. $_vars = 'array(' . join(',', $_pairs) . ')';
  211. $_has_vars = true;
  212. } else {
  213. $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
  214. }
  215. } else {
  216. $_vars = 'array()';
  217. $_has_vars = false;
  218. }
  219. if ($has_compiled_template) {
  220. // never call inline templates in nocache mode
  221. $compiler->suppressNocacheProcessing = true;
  222. $_hash = $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'];
  223. $_output = "<?php /* Call merged included template \"" . $tpl_name . "\" */\n";
  224. $_output .= "\$_tpl_stack[] = \$_smarty_tpl;\n";
  225. $_output .= " \$_smarty_tpl = \$_smarty_tpl->setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n";
  226. if (isset($_assign)) {
  227. $_output .= 'ob_start(); ';
  228. }
  229. $_output .= $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] . "(\$_smarty_tpl);\n";
  230. $_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); ";
  231. if (isset($_assign)) {
  232. $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());";
  233. }
  234. $_output .= "\n/* End of included template \"" . $tpl_name . "\" */?>";
  235. return $_output;
  236. }
  237. // was there an assign attribute
  238. if (isset($_assign)) {
  239. $_output = "<?php \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(\$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope));?>\n";;
  240. } else {
  241. $_output = "<?php echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);?>\n";
  242. }
  243. return $_output;
  244. }
  245. }