PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Smarty/sysplugins/smarty_internal_compile_include.php

https://gitlab.com/webbroteam/satisfaction-mvc
PHP | 361 lines | 252 code | 16 blank | 93 comment | 59 complexity | ab3ee3d93217148e8c5cb18875b8cff9 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Include
  4. * Compiles the {include} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Include Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * caching mode to create nocache code but no cache file
  20. */
  21. const CACHING_NOCACHE_CODE = 9999;
  22. /**
  23. * Attribute definition: Overwrites base class.
  24. *
  25. * @var array
  26. * @see Smarty_Internal_CompileBase
  27. */
  28. public $required_attributes = array('file');
  29. /**
  30. * Attribute definition: Overwrites base class.
  31. *
  32. * @var array
  33. * @see Smarty_Internal_CompileBase
  34. */
  35. public $shorttag_order = array('file');
  36. /**
  37. * Attribute definition: Overwrites base class.
  38. *
  39. * @var array
  40. * @see Smarty_Internal_CompileBase
  41. */
  42. public $option_flags = array('nocache', 'inline', 'caching', 'bubble_up');
  43. /**
  44. * Attribute definition: Overwrites base class.
  45. *
  46. * @var array
  47. * @see Smarty_Internal_CompileBase
  48. */
  49. public $optional_attributes = array('_any');
  50. /**
  51. * Valid scope names
  52. *
  53. * @var array
  54. */
  55. public $valid_scopes = array('local' => true, 'parent' => true, 'root' => true,
  56. 'tpl_root' => true);
  57. /**
  58. * Compiles code for the {include} tag
  59. *
  60. * @param array $args array with attributes from parser
  61. * @param Smarty_Internal_SmartyTemplateCompiler $compiler compiler object
  62. * @param array $parameter array with compilation parameter
  63. *
  64. * @throws SmartyCompilerException
  65. * @return string compiled code
  66. */
  67. public function compile($args, Smarty_Internal_SmartyTemplateCompiler $compiler, $parameter)
  68. {
  69. // check and get attributes
  70. $_attr = $this->getAttributes($compiler, $args);
  71. $hashResourceName = $fullResourceName = $source_resource = $_attr['file'];
  72. $variable_template = false;
  73. $cache_tpl = false;
  74. // parse resource_name
  75. if (preg_match('/^([\'"])(([A-Za-z0-9_\-]{2,})[:])?(([^$()]+)|(.+))\1$/', $source_resource, $match)) {
  76. $type = !empty($match[3]) ? $match[3] : $compiler->template->smarty->default_resource_type;
  77. $name = !empty($match[5]) ? $match[5] : $match[6];
  78. $handler = Smarty_Resource::load($compiler->smarty, $type);
  79. if ($handler->recompiled || $handler->uncompiled) {
  80. $variable_template = true;
  81. }
  82. if (!$variable_template) {
  83. if ($type != 'string') {
  84. $fullResourceName = "{$type}:{$name}";
  85. $compiled = $compiler->parent_compiler->template->compiled;
  86. if (isset($compiled->includes[$fullResourceName])) {
  87. $compiled->includes[$fullResourceName] ++;
  88. $cache_tpl = true;
  89. } else {
  90. $compiled->includes[$fullResourceName] = 1;
  91. }
  92. $fullResourceName = '"' . $fullResourceName . '"';
  93. }
  94. }
  95. if (empty($match[5])) {
  96. $variable_template = true;
  97. }
  98. } else {
  99. $variable_template = true;
  100. }
  101. if (isset($_attr['assign'])) {
  102. // output will be stored in a smarty variable instead of being displayed
  103. $_assign = $_attr['assign'];
  104. }
  105. // scope setup
  106. $_scope = Smarty::SCOPE_LOCAL;
  107. if (isset($_attr['scope'])) {
  108. $_attr['scope'] = trim($_attr['scope'], "'\"");
  109. if (!isset($this->valid_scopes[$_attr['scope']])) {
  110. $compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null, true);
  111. }
  112. if ($_attr['scope'] != 'local') {
  113. if ($_attr['scope'] == 'parent') {
  114. $_scope = Smarty::SCOPE_PARENT;
  115. } elseif ($_attr['scope'] == 'root') {
  116. $_scope = Smarty::SCOPE_ROOT;
  117. } elseif ($_attr['scope'] == 'tpl_root') {
  118. $_scope = Smarty::SCOPE_TPL_ROOT;
  119. }
  120. $_scope += (isset($_attr[ 'bubble_up' ]) && $_attr[ 'bubble_up' ] == 'false') ? 0 :
  121. Smarty::SCOPE_BUBBLE_UP;
  122. }
  123. }
  124. // set flag to cache subtemplate object when called within loop or template name is variable.
  125. if ($cache_tpl || $variable_template || $compiler->loopNesting > 0) {
  126. $_cache_tpl = 'true';
  127. } else {
  128. $_cache_tpl = 'false';
  129. }
  130. // assume caching is off
  131. $_caching = Smarty::CACHING_OFF;
  132. if ($_attr['nocache'] === true) {
  133. $compiler->tag_nocache = true;
  134. }
  135. $call_nocache = $compiler->tag_nocache || $compiler->nocache;
  136. // caching was on and {include} is not in nocache mode
  137. if ($compiler->template->caching && !$compiler->nocache && !$compiler->tag_nocache) {
  138. $_caching = self::CACHING_NOCACHE_CODE;
  139. }
  140. // flag if included template code should be merged into caller
  141. $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) &&
  142. !$compiler->template->source->handler->recompiled;
  143. if ($merge_compiled_includes && $_attr['inline'] !== true) {
  144. // variable template name ?
  145. if ($variable_template) {
  146. $merge_compiled_includes = false;
  147. if ($compiler->template->caching) {
  148. // must use individual cache file
  149. //$_attr['caching'] = 1;
  150. }
  151. }
  152. // variable compile_id?
  153. if (isset($_attr['compile_id'])) {
  154. if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2 ||
  155. is_numeric($_attr['compile_id']))) || substr_count($_attr['compile_id'], '(') != 0 ||
  156. substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0
  157. ) {
  158. $merge_compiled_includes = false;
  159. if ($compiler->template->caching) {
  160. // must use individual cache file
  161. //$_attr['caching'] = 1;
  162. }
  163. }
  164. }
  165. }
  166. /*
  167. * if the {include} tag provides individual parameter for caching or compile_id
  168. * the subtemplate must not be included into the common cache file and is treated like
  169. * a call in nocache mode.
  170. *
  171. */
  172. if ($_attr['nocache'] !== true && $_attr['caching']) {
  173. $_caching = $_new_caching = (int) $_attr['caching'];
  174. $call_nocache = true;
  175. } else {
  176. $_new_caching = Smarty::CACHING_LIFETIME_CURRENT;
  177. }
  178. if (isset($_attr['cache_lifetime'])) {
  179. $_cache_lifetime = $_attr['cache_lifetime'];
  180. $call_nocache = true;
  181. $_caching = $_new_caching;
  182. } else {
  183. $_cache_lifetime = '$_smarty_tpl->cache_lifetime';
  184. }
  185. if (isset($_attr['cache_id'])) {
  186. $_cache_id = $_attr['cache_id'];
  187. $call_nocache = true;
  188. $_caching = $_new_caching;
  189. } else {
  190. $_cache_id = '$_smarty_tpl->cache_id';
  191. }
  192. if (isset($_attr['compile_id'])) {
  193. $_compile_id = $_attr['compile_id'];
  194. } else {
  195. $_compile_id = '$_smarty_tpl->compile_id';
  196. }
  197. // if subtemplate will be called in nocache mode do not merge
  198. if ($compiler->template->caching && $call_nocache) {
  199. $merge_compiled_includes = false;
  200. }
  201. $has_compiled_template = false;
  202. if ($merge_compiled_includes) {
  203. $c_id = isset($_attr['compile_id']) ? $_attr['compile_id'] : $compiler->template->compile_id;
  204. // we must observe different compile_id and caching
  205. $t_hash = sha1($c_id . ($_caching ? '--caching' : '--nocaching'));
  206. if (!isset($compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash])) {
  207. $has_compiled_template =
  208. $this->compileInlineTemplate($compiler, $fullResourceName, $_caching, $hashResourceName, $t_hash,
  209. $c_id);
  210. } else {
  211. $has_compiled_template = true;
  212. }
  213. }
  214. // delete {include} standard attributes
  215. unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline'], $_attr['bubble_up']);
  216. // remaining attributes must be assigned as smarty variable
  217. $_vars_nc = '';
  218. if (!empty($_attr)) {
  219. if ($_scope == Smarty::SCOPE_LOCAL) {
  220. $_pairs = array();
  221. // create variables
  222. foreach ($_attr as $key => $value) {
  223. $_pairs[] = "'$key'=>$value";
  224. $_vars_nc .= "\$_smarty_tpl->tpl_vars['$key'] = new Smarty_Variable($value);\n";
  225. }
  226. $_vars = 'array(' . join(',', $_pairs) . ')';
  227. } else {
  228. $compiler->trigger_template_error('variable passing not allowed in parent/global scope', null, true);
  229. }
  230. } else {
  231. $_vars = 'array()';
  232. }
  233. $update_compile_id = $compiler->template->caching && !$compiler->tag_nocache && !$compiler->nocache &&
  234. $_compile_id != '$_smarty_tpl->compile_id';
  235. if ($has_compiled_template && !$call_nocache) {
  236. $_output = "<?php\n";
  237. if ($update_compile_id) {
  238. $_output .= $compiler->makeNocacheCode("\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n");
  239. }
  240. if (!empty($_vars_nc) && $_caching == 9999 && $compiler->template->caching) {
  241. //$compiler->suppressNocacheProcessing = false;
  242. $_output .= substr($compiler->processNocacheCode('<?php ' . $_vars_nc . "?>\n", true), 6, - 3);
  243. //$compiler->suppressNocacheProcessing = true;
  244. }
  245. if (isset($_assign)) {
  246. $_output .= "ob_start();\n";
  247. }
  248. $_output .= "\$_smarty_tpl->_subTemplateRender({$fullResourceName}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_scope}, {$_cache_tpl}, '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid']}', '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func']}');\n";
  249. if (isset($_assign)) {
  250. $_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
  251. }
  252. if ($update_compile_id) {
  253. $_output .= $compiler->makeNocacheCode("\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n");
  254. }
  255. $_output .= "?>\n";
  256. return $_output;
  257. }
  258. if ($call_nocache) {
  259. $compiler->tag_nocache = true;
  260. }
  261. $_output = "<?php ";
  262. if ($update_compile_id) {
  263. $_output .= "\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n";
  264. }
  265. // was there an assign attribute
  266. if (isset($_assign)) {
  267. $_output .= "ob_start();\n";
  268. }
  269. $_output .= "\$_smarty_tpl->_subTemplateRender({$fullResourceName}, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_scope, {$_cache_tpl});\n";
  270. if (isset($_assign)) {
  271. $_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
  272. }
  273. if ($update_compile_id) {
  274. $_output .= "\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n";
  275. }
  276. $_output .= "?>\n";
  277. return $_output;
  278. }
  279. /**
  280. * Compile inline sub template
  281. *
  282. * @param \Smarty_Internal_SmartyTemplateCompiler $compiler
  283. * @param $fullResourceName
  284. * @param $_caching
  285. * @param $hashResourceName
  286. * @param $t_hash
  287. * @param $c_id
  288. *
  289. * @return bool
  290. */
  291. public function compileInlineTemplate(Smarty_Internal_SmartyTemplateCompiler $compiler, $fullResourceName,
  292. $_caching, $hashResourceName, $t_hash, $c_id)
  293. {
  294. $compiler->smarty->allow_ambiguous_resources = true;
  295. /* @var Smarty_Internal_Template $tpl */
  296. $tpl =
  297. new $compiler->smarty->template_class (trim($fullResourceName, '"\''), $compiler->smarty, $compiler->template,
  298. $compiler->template->cache_id, $c_id, $_caching);
  299. if (!($tpl->source->handler->uncompiled) && $tpl->source->exists) {
  300. $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid'] = $tpl->source->uid;
  301. if (isset($compiler->template->ext->_inheritance)) {
  302. $tpl->ext->_inheritance = clone $compiler->template->ext->_inheritance;
  303. }
  304. $tpl->compiled = new Smarty_Template_Compiled();
  305. $tpl->compiled->nocache_hash = $compiler->parent_compiler->template->compiled->nocache_hash;
  306. $tpl->loadCompiler();
  307. // save unique function name
  308. $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func'] =
  309. $tpl->compiled->unifunc = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
  310. // make sure whole chain gets compiled
  311. $tpl->mustCompile = true;
  312. $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['nocache_hash'] =
  313. $tpl->compiled->nocache_hash;
  314. // get compiled code
  315. $compiled_code = "<?php\n\n";
  316. $compiled_code .= "/* Start inline template \"{$tpl->source->type}:{$tpl->source->name}\" =============================*/\n";
  317. $compiled_code .= "function {$tpl->compiled->unifunc} (\$_smarty_tpl) {\n";
  318. $compiled_code .= "?>\n" . $tpl->compiler->compileTemplateSource($tpl, null, $compiler->parent_compiler);
  319. $compiled_code .= "<?php\n";
  320. $compiled_code .= "}\n?>\n";
  321. $compiled_code .= $tpl->compiler->postFilter($tpl->compiler->blockOrFunctionCode);
  322. $compiled_code .= "<?php\n\n";
  323. $compiled_code .= "/* End inline template \"{$tpl->source->type}:{$tpl->source->name}\" =============================*/\n";
  324. $compiled_code .= "?>";
  325. unset($tpl->compiler);
  326. if ($tpl->compiled->has_nocache_code) {
  327. // replace nocache_hash
  328. $compiled_code =
  329. str_replace("{$tpl->compiled->nocache_hash}", $compiler->template->compiled->nocache_hash,
  330. $compiled_code);
  331. $compiler->template->compiled->has_nocache_code = true;
  332. }
  333. $compiler->parent_compiler->mergedSubTemplatesCode[$tpl->compiled->unifunc] = $compiled_code;
  334. return true;
  335. } else {
  336. return false;
  337. }
  338. }
  339. }