PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/_lib/smarty/sysplugins/smarty_internal_compile_include.php

https://github.com/raphaelbastide/berta
PHP | 155 lines | 132 code | 1 blank | 22 comment | 5 complexity | ead4ce6c3407823e517ad9085b84d509 MD5 | raw file
  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. class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {include} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return string compiled code
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('file');
  26. $this->optional_attributes = array('_any');
  27. // check and get attributes
  28. $_attr = $this->_get_attributes($args);
  29. // save posible attributes
  30. $include_file = $_attr['file'];
  31. $has_compiled_template = false;
  32. if ($compiler->smarty->merge_compiled_includes || isset($_attr['inline'])) {
  33. // check if compiled code can be merged (contains no variable part)
  34. if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) and substr_count($include_file, '(') == 0) {
  35. eval("\$tmp = $include_file;");
  36. if ($this->compiler->template->template_resource != $tmp) {
  37. $tpl = $compiler->smarty->createTemplate ($tmp, $compiler->template->cache_id, $compiler->template->compile_id, $compiler->template);
  38. if ($this->compiler->template->caching) {
  39. // needs code for cached page but no cache file
  40. $tpl->caching = 9999;
  41. }
  42. if ($this->compiler->template->mustCompile) {
  43. // make sure whole chain gest compiled
  44. $tpl->mustCompile = true;
  45. }
  46. if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
  47. // get compiled code
  48. $compiled_tpl = $tpl->getCompiledTemplate();
  49. // merge compiled code for {function} tags
  50. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
  51. // merge filedependency by evaluating header code
  52. preg_match_all("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", $compiled_tpl, $result);
  53. $saved_has_nocache_code = $compiler->template->has_nocache_code;
  54. $saved_nocache_hash = $compiler->template->properties['nocache_hash'];
  55. $_smarty_tpl = $compiler->template;
  56. eval($result[2][0]);
  57. $compiler->template->properties['nocache_hash'] = $saved_nocache_hash;
  58. $compiler->template->has_nocache_code = $saved_has_nocache_code;
  59. // remove header code
  60. $compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
  61. if ($tpl->has_nocache_code) {
  62. // replace nocache_hash
  63. $compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
  64. $compiler->template->has_nocache_code = true;
  65. }
  66. $has_compiled_template = true;
  67. }
  68. }
  69. }
  70. }
  71. if (isset($_attr['assign'])) {
  72. // output will be stored in a smarty variable instead of beind displayed
  73. $_assign = $_attr['assign'];
  74. }
  75. $_parent_scope = SMARTY_LOCAL_SCOPE;
  76. if (isset($_attr['scope'])) {
  77. if ($_attr['scope'] == '\'parent\'') {
  78. $_parent_scope = SMARTY_PARENT_SCOPE;
  79. } elseif ($_attr['scope'] == '\'root\'') {
  80. $_parent_scope = SMARTY_ROOT_SCOPE;
  81. } elseif ($_attr['scope'] == '\'global\'') {
  82. $_parent_scope = SMARTY_GLOBAL_SCOPE;
  83. }
  84. }
  85. $_caching = 'null';
  86. // default for included templates
  87. if ($this->compiler->template->caching && !$this->compiler->nocache) {
  88. $_caching = 9999;
  89. }
  90. /*
  91. * if the {include} tag provides individual parameter for caching
  92. * it will not be included into the common cache file and treated like
  93. * a nocache section
  94. */
  95. if (isset($_attr['cache_lifetime'])) {
  96. $_cache_lifetime = $_attr['cache_lifetime'];
  97. $this->compiler->tag_nocache = true;
  98. $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
  99. } else {
  100. $_cache_lifetime = 'null';
  101. }
  102. if (isset($_attr['nocache'])) {
  103. if ($_attr['nocache'] == 'true') {
  104. $this->compiler->tag_nocache = true;
  105. $_caching = SMARTY_CACHING_OFF;
  106. }
  107. }
  108. if (isset($_attr['caching'])) {
  109. if ($_attr['caching'] == 'true') {
  110. $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
  111. } else {
  112. $this->compiler->tag_nocache = true;
  113. $_caching = SMARTY_CACHING_OFF;
  114. }
  115. }
  116. // create template object
  117. $_output = "<?php \$_template = new {$compiler->smarty->template_class}($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, \$_smarty_tpl->cache_id, \$_smarty_tpl->compile_id, $_caching, $_cache_lifetime);\n";
  118. // delete {include} standard attributes
  119. unset($_attr['file'], $_attr['assign'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
  120. // remaining attributes must be assigned as smarty variable
  121. if (!empty($_attr)) {
  122. if ($_parent_scope == SMARTY_LOCAL_SCOPE) {
  123. // create variables
  124. foreach ($_attr as $_key => $_value) {
  125. $_output .= "\$_template->assign('$_key',$_value);";
  126. }
  127. } else {
  128. $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope');
  129. }
  130. }
  131. // was there an assign attribute
  132. if (isset($_assign)) {
  133. $_output .= "\$_smarty_tpl->assign($_assign,\$_template->getRenderedTemplate());?>";
  134. } else {
  135. if ($has_compiled_template && !($compiler->template->caching && ($this->compiler->tag_nocache || $this->compiler->nocache))) {
  136. $_output .= "\$_template->properties['nocache_hash'] = '{$compiler->template->properties['nocache_hash']}';\n";
  137. $_output .= "\$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
  138. $_output .= $compiled_tpl;
  139. $_output .= "<?php \$_smarty_tpl->updateParentVariables($_parent_scope);?>\n";
  140. $_output .= "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>\n";
  141. $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
  142. } else {
  143. $_output .= " echo \$_template->getRenderedTemplate();?>";
  144. $_output .= "<?php \$_template->updateParentVariables($_parent_scope);?>";
  145. }
  146. }
  147. $_output .= "<?php unset(\$_template);?>";
  148. return $_output;
  149. }
  150. }
  151. ?>