- bugfix individual cache_lifetime of {include} did not work correctly inside {block} tags

This commit is contained in:
uwe.tews@googlemail.com
2011-07-15 00:42:43 +00:00
parent c859f6a9ae
commit c64813beb5
2 changed files with 75 additions and 69 deletions

View File

@@ -1,4 +1,7 @@
===== SVN trunk ===== ===== SVN trunk =====
15/07/2011
- bugfix individual cache_lifetime of {include} did not work correctly inside {block} tags
12/07/2011 12/07/2011
- bugfix PHP did eat line break on nocache sections - bugfix PHP did eat line break on nocache sections

View File

@@ -2,12 +2,12 @@
/** /**
* Smarty Internal Plugin Compile Include * Smarty Internal Plugin Compile Include
* *
* Compiles the {include} tag * Compiles the {include} tag
* *
* @package Smarty * @package Smarty
* @subpackage Compiler * @subpackage Compiler
* @author Uwe Tews * @author Uwe Tews
*/ */
/** /**
@@ -20,11 +20,11 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
public $required_attributes = array('file'); public $required_attributes = array('file');
public $shorttag_order = array('file'); public $shorttag_order = array('file');
public $option_flags = array('nocache','inline','caching'); public $option_flags = array('nocache','inline','caching');
public $optional_attributes = array('_any'); public $optional_attributes = array('_any');
/** /**
* Compiles code for the {include} tag * Compiles code for the {include} tag
* *
* @param array $args array with attributes from parser * @param array $args array with attributes from parser
* @param object $compiler compiler object * @param object $compiler compiler object
* @return string compiled code * @return string compiled code
@@ -33,57 +33,15 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
{ {
$this->compiler = $compiler; $this->compiler = $compiler;
// check and get attributes // check and get attributes
$_attr = $this->_get_attributes($args); $_attr = $this->_get_attributes($args);
// save posible attributes // save posible attributes
$include_file = $_attr['file']; $include_file = $_attr['file'];
$has_compiled_template = false; $has_compiled_template = false;
if ($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) {
// check if compiled code can be merged (contains no variable part)
if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) and substr_count($include_file, '(') == 0) {
$tmp = null;
eval("\$tmp = $include_file;");
if ($this->compiler->template->template_resource != $tmp) {
$tpl = new $compiler->smarty->template_class ($tmp, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
// suppress writing of compiled file
$tpl->write_compiled_code = false;
if ($this->compiler->template->caching) {
// needs code for cached page but no cache file
$tpl->caching = self::CACHING_NOCACHE_CODE;
}
// if ($this->compiler->template->mustCompile) {
// make sure whole chain gest compiled
$tpl->mustCompile = true;
// }
if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
// get compiled code
$compiled_tpl = $tpl->getCompiledTemplate();
// merge compiled code for {function} tags
$compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
// merge filedependency by evaluating header code
preg_match_all("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", $compiled_tpl, $result);
$saved_has_nocache_code = $compiler->template->has_nocache_code;
$saved_nocache_hash = $compiler->template->properties['nocache_hash'];
$_smarty_tpl = $compiler->template;
eval($result[2][0]);
$compiler->template->properties['nocache_hash'] = $saved_nocache_hash;
$compiler->template->has_nocache_code = $saved_has_nocache_code;
// remove header code
$compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
if ($tpl->has_nocache_code) {
// replace nocache_hash
$compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
$compiler->template->has_nocache_code = true;
}
$has_compiled_template = true;
}
}
}
}
if (isset($_attr['assign'])) { if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of beind displayed // output will be stored in a smarty variable instead of beind displayed
$_assign = $_attr['assign']; $_assign = $_attr['assign'];
} }
$_parent_scope = Smarty::SCOPE_LOCAL; $_parent_scope = Smarty::SCOPE_LOCAL;
if (isset($_attr['scope'])) { if (isset($_attr['scope'])) {
@@ -94,16 +52,16 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
$_parent_scope = Smarty::SCOPE_ROOT; $_parent_scope = Smarty::SCOPE_ROOT;
} elseif ($_attr['scope'] == 'global') { } elseif ($_attr['scope'] == 'global') {
$_parent_scope = Smarty::SCOPE_GLOBAL; $_parent_scope = Smarty::SCOPE_GLOBAL;
} }
} }
$_caching = 'null'; $_caching = 'null';
if ($this->compiler->nocache || $this->compiler->tag_nocache) { if ($this->compiler->nocache || $this->compiler->tag_nocache) {
$_caching = Smarty::CACHING_OFF; $_caching = Smarty::CACHING_OFF;
} }
// default for included templates // default for included templates
if ($compiler->template->caching && !$this->compiler->nocache && !$this->compiler->tag_nocache) { if ($compiler->template->caching && !$this->compiler->nocache && !$this->compiler->tag_nocache) {
$_caching = self::CACHING_NOCACHE_CODE; $_caching = self::CACHING_NOCACHE_CODE;
} }
/* /*
* if the {include} tag provides individual parameter for caching * if the {include} tag provides individual parameter for caching
* it will not be included into the common cache file and treated like * it will not be included into the common cache file and treated like
@@ -115,56 +73,101 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
$_caching = Smarty::CACHING_LIFETIME_CURRENT; $_caching = Smarty::CACHING_LIFETIME_CURRENT;
} else { } else {
$_cache_lifetime = 'null'; $_cache_lifetime = 'null';
} }
if (isset($_attr['cache_id'])) { if (isset($_attr['cache_id'])) {
$_cache_id = $_attr['cache_id']; $_cache_id = $_attr['cache_id'];
$this->compiler->tag_nocache = true; $this->compiler->tag_nocache = true;
$_caching = Smarty::CACHING_LIFETIME_CURRENT; $_caching = Smarty::CACHING_LIFETIME_CURRENT;
} else { } else {
$_cache_id = '$_smarty_tpl->cache_id'; $_cache_id = '$_smarty_tpl->cache_id';
} }
if (isset($_attr['compile_id'])) { if (isset($_attr['compile_id'])) {
$_compile_id = $_attr['compile_id']; $_compile_id = $_attr['compile_id'];
} else { } else {
$_compile_id = '$_smarty_tpl->compile_id'; $_compile_id = '$_smarty_tpl->compile_id';
} }
if ($_attr['caching'] === true) { if ($_attr['caching'] === true) {
$_caching = Smarty::CACHING_LIFETIME_CURRENT; $_caching = Smarty::CACHING_LIFETIME_CURRENT;
} }
if ($_attr['nocache'] === true) { if ($_attr['nocache'] === true) {
$this->compiler->tag_nocache = true; $this->compiler->tag_nocache = true;
$_caching = Smarty::CACHING_OFF; $_caching = Smarty::CACHING_OFF;
} }
if (($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) && !($compiler->template->caching && ($this->compiler->tag_nocache || $this->compiler->nocache)) && $_caching != Smarty::CACHING_LIFETIME_CURRENT) {
// check if compiled code can be merged (contains no variable part)
if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) and substr_count($include_file, '(') == 0) {
$tmp = null;
eval("\$tmp = $include_file;");
if ($this->compiler->template->template_resource != $tmp) {
$tpl = new $compiler->smarty->template_class ($tmp, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
// suppress writing of compiled file
$tpl->write_compiled_code = false;
if ($this->compiler->template->caching) {
// needs code for cached page but no cache file
$tpl->caching = self::CACHING_NOCACHE_CODE;
}
// if ($this->compiler->template->mustCompile) {
// make sure whole chain gest compiled
$tpl->mustCompile = true;
// }
if ($tpl->resource_object->usesCompiler && $tpl->isExisting()) {
// get compiled code
$compiled_tpl = $tpl->getCompiledTemplate();
// merge compiled code for {function} tags
$compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
// merge filedependency by evaluating header code
preg_match_all("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", $compiled_tpl, $result);
$saved_has_nocache_code = $compiler->template->has_nocache_code;
$saved_nocache_hash = $compiler->template->properties['nocache_hash'];
$_smarty_tpl = $compiler->template;
eval($result[2][0]);
$compiler->template->properties['nocache_hash'] = $saved_nocache_hash;
$compiler->template->has_nocache_code = $saved_has_nocache_code;
// remove header code
$compiled_tpl = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_tpl);
if ($tpl->has_nocache_code) {
// replace nocache_hash
$compiled_tpl = preg_replace("/{$tpl->properties['nocache_hash']}/", $compiler->template->properties['nocache_hash'], $compiled_tpl);
$compiler->template->has_nocache_code = true;
}
$has_compiled_template = true;
}
}
}
}
// create template object // create template object
$_output = "<?php "; $_output = "<?php ";
if ($_caching != 'null' && $_caching != Smarty::CACHING_OFF) { if ($_caching != 'null' && $_caching != Smarty::CACHING_OFF) {
$_output .= "\$sha = sha1($include_file . $_cache_id . $_compile_id);\n"; $_output .= "\$sha = sha1($include_file . $_cache_id . $_compile_id);\n";
$_output .= "if (isset(\$_smarty_tpl->smarty->template_objects[\$sha])) {\n"; $_output .= "if (isset(\$_smarty_tpl->smarty->template_objects[\$sha])) {\n";
$_output .= "\$_template = \$_smarty_tpl->smarty->template_objects[\$sha]; \$_template->caching = $_caching; \$_template->cache_lifetime = $_cache_lifetime;\n"; $_output .= "\$_template = \$_smarty_tpl->smarty->template_objects[\$sha]; \$_template->caching = $_caching; \$_template->cache_lifetime = $_cache_lifetime;\n";
$_output .= "} else {\n"; $_output .= "} else {\n";
} }
$_output .= "\$_template = new {$compiler->smarty->template_class}($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, $_cache_id, $_compile_id, $_caching, $_cache_lifetime);\n"; $_output .= "\$_template = new {$compiler->smarty->template_class}($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, $_cache_id, $_compile_id, $_caching, $_cache_lifetime);\n";
if ($_caching != 'null' && $_caching != Smarty::CACHING_OFF) { if ($_caching != 'null' && $_caching != Smarty::CACHING_OFF) {
$_output .= "}\n"; $_output .= "}\n";
} }
// delete {include} standard attributes // delete {include} standard attributes
unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']); unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
// remaining attributes must be assigned as smarty variable // remaining attributes must be assigned as smarty variable
if (!empty($_attr)) { if (!empty($_attr)) {
if ($_parent_scope == Smarty::SCOPE_LOCAL) { if ($_parent_scope == Smarty::SCOPE_LOCAL) {
// create variables // create variables
foreach ($_attr as $_key => $_value) { foreach ($_attr as $_key => $_value) {
$_output .= "\$_template->assign('$_key',$_value);"; $_output .= "\$_template->assign('$_key',$_value);";
} }
} else { } else {
$this->compiler->trigger_template_error('variable passing not allowed in parent/global scope', $this->compiler->lex->taglineno); $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope', $this->compiler->lex->taglineno);
} }
} }
// was there an assign attribute // was there an assign attribute
if (isset($_assign)) { if (isset($_assign)) {
$_output .= "\$_smarty_tpl->assign($_assign,\$_template->getRenderedTemplate());?>"; $_output .= "\$_smarty_tpl->assign($_assign,\$_template->getRenderedTemplate());?>";
} else { } else {
if ($has_compiled_template && !($compiler->template->caching && ($this->compiler->tag_nocache || $this->compiler->nocache))) { if ($has_compiled_template) {
$_output .= "\$_template->properties['nocache_hash'] = '{$compiler->template->properties['nocache_hash']}';\n"; $_output .= "\$_template->properties['nocache_hash'] = '{$compiler->template->properties['nocache_hash']}';\n";
$_output .= "\$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n"; $_output .= "\$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
$_output .= $compiled_tpl; $_output .= $compiled_tpl;
@@ -176,11 +179,11 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
if ($_parent_scope != Smarty::SCOPE_LOCAL) { if ($_parent_scope != Smarty::SCOPE_LOCAL) {
$_output .= "<?php \$_template->updateParentVariables($_parent_scope);?>"; $_output .= "<?php \$_template->updateParentVariables($_parent_scope);?>";
} }
} }
} }
$_output .= "<?php unset(\$_template);?>"; $_output .= "<?php unset(\$_template);?>";
return $_output; return $_output;
} }
} }
?> ?>