From a44d9a6ac9b25fe7fd90b951820c8a2014e485fc Mon Sep 17 00:00:00 2001 From: "uwe.tews@googlemail.com" Date: Sun, 18 Sep 2011 02:31:58 +0000 Subject: [PATCH] - bugfix template caching did not care about file.tpl in different template_dir - bugfix {include $file} was broken when merge_compiled_incluges = true - bugfix {include} was broken when merge_compiled_incluges = true and same indluded template was used in different main templates in one compilation run --- change_log.txt | 6 ++++++ libs/Smarty.class.php | 8 +++++++- .../smarty_cacheresource_keyvaluestore.php | 2 +- .../smarty_internal_cacheresource_file.php | 4 ++-- .../smarty_internal_compile_include.php | 19 +++++++------------ libs/sysplugins/smarty_internal_template.php | 2 +- .../smarty_internal_templatebase.php | 2 ++ libs/sysplugins/smarty_internal_utility.php | 4 ++-- 8 files changed, 28 insertions(+), 19 deletions(-) diff --git a/change_log.txt b/change_log.txt index 3c8d361a..c66927e6 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,10 @@ ===== Smarty 3.1 trunk ===== +18.09.2011 +- bugfix template caching did not care about file.tpl in different template_dir +- bugfix {include $file} was broken when merge_compiled_incluges = true +- bugfix {include} was broken when merge_compiled_incluges = true and same indluded template + was used in different main templates in one compilation run + 17.09.2011 - bugfix lock_id for file resource would create invalid filepath - bugfix resource caching did not care about file.tpl in different template_dir diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 3e3492a9..194cea12 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -533,6 +533,12 @@ class Smarty extends Smarty_Internal_TemplateBase { * @var bool */ public $_parserdebug = false; + /** + * Saved parameter of merged templates during compilation + * + * @var array + */ + public $merged_templates_func = array(); /**#@-*/ /** @@ -1126,7 +1132,7 @@ class Smarty extends Smarty_Internal_TemplateBase { $cache_id = $cache_id === null ? $this->cache_id : $cache_id; $compile_id = $compile_id === null ? $this->compile_id : $compile_id; // already in template cache? - $_templateId = sha1($template . $cache_id . $compile_id); + $_templateId = sha1(join(DIRECTORY_SEPARATOR, $this->getTemplateDir()).$template . $cache_id . $compile_id); if ($do_clone) { if (isset($this->template_objects[$_templateId])) { // return cached template object diff --git a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php index f2ab5296..b6dcf26c 100644 --- a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php +++ b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php @@ -178,7 +178,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource { $uid = $tpl->source->uid; } // remove from template cache - unset($smarty->template_objects[sha1($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); + unset($smarty->template_objects[sha1(join(DIRECTORY_SEPARATOR, $smarty->getTemplateDir()),$tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); } return $uid . '#' . $this->sanitize($resource_name) . '#' . $this->sanitize($cache_id) . '#' . $this->sanitize($compile_id); } diff --git a/libs/sysplugins/smarty_internal_cacheresource_file.php b/libs/sysplugins/smarty_internal_cacheresource_file.php index 7338469f..a5cd9f8b 100644 --- a/libs/sysplugins/smarty_internal_cacheresource_file.php +++ b/libs/sysplugins/smarty_internal_cacheresource_file.php @@ -154,10 +154,10 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource { if ($tpl->source->exists) { $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); // remove from template cache - unset($smarty->template_objects[sha1($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); + unset($smarty->template_objects[sha1(join(DIRECTORY_SEPARATOR, $smarty->getTemplateDir()).$tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); } else { // remove from template cache - unset($smarty->template_objects[sha1($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); + unset($smarty->template_objects[sha1(join(DIRECTORY_SEPARATOR, $smarty->getTemplateDir()).$tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); return 0; } } diff --git a/libs/sysplugins/smarty_internal_compile_include.php b/libs/sysplugins/smarty_internal_compile_include.php index e611d718..d45db361 100644 --- a/libs/sysplugins/smarty_internal_compile_include.php +++ b/libs/sysplugins/smarty_internal_compile_include.php @@ -49,12 +49,6 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase { * @see Smarty_Internal_CompileBase */ public $optional_attributes = array('_any'); - /** - * Saved parameter of merged templates - * - * @var array - */ - private static $merged_templates_func = array(); /** * Compiles code for the {include} tag @@ -131,15 +125,16 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase { if (($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) && !$compiler->template->source->recompiled && !($compiler->template->caching && ($compiler->tag_nocache || $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) { + if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) + and substr_count($include_file, '(') == 0 and substr_count($include_file, '$_smarty_tpl->') == 0) { $tpl_name = null; eval("\$tpl_name = $include_file;"); - if (!isset(self::$merged_templates_func[$tpl_name]) || $compiler->inheritance) { + if (!isset($compiler->smarty->merged_templates_func[$tpl_name]) || $compiler->inheritance) { $tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id); // save unique function name - self::$merged_templates_func[$tpl_name]['func'] = $tpl->properties['unifunc'] = 'content_'.uniqid(); + $compiler->smarty->merged_templates_func[$tpl_name]['func'] = $tpl->properties['unifunc'] = 'content_'.uniqid(); // use current nocache hash for inlined code - self::$merged_templates_func[$tpl_name]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash']; + $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash']; if ($compiler->template->caching) { // needs code for cached page but no cache file $tpl->caching = self::CACHING_NOCACHE_CODE; @@ -190,14 +185,14 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase { $_has_vars = false; } if ($has_compiled_template) { - $_hash = self::$merged_templates_func[$tpl_name]['nocache_hash']; + $_hash = $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash']; $_output = "setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n"; if (isset($_assign)) { $_output .= 'ob_start(); '; } - $_output .= self::$merged_templates_func[$tpl_name]['func']. "(\$_smarty_tpl);\n"; + $_output .= $compiler->smarty->merged_templates_func[$tpl_name]['func']. "(\$_smarty_tpl);\n"; $_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); "; if (isset($_assign)) { $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());"; diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 8efb95d0..ea48cb08 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -589,7 +589,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { // cache template object under a unique ID // do not cache eval resources if ($this->source->type != 'eval') { - $this->smarty->template_objects[sha1($this->template_resource . $this->cache_id . $this->compile_id)] = $this; + $this->smarty->template_objects[sha1(join(DIRECTORY_SEPARATOR, $this->smarty->getTemplateDir()).$this->template_resource . $this->cache_id . $this->compile_id)] = $this; } return $this->source; diff --git a/libs/sysplugins/smarty_internal_templatebase.php b/libs/sysplugins/smarty_internal_templatebase.php index de056fa2..c3b5f8f9 100644 --- a/libs/sysplugins/smarty_internal_templatebase.php +++ b/libs/sysplugins/smarty_internal_templatebase.php @@ -106,6 +106,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data { } } } + // must reset merge template date + $_template->smarty->merged_templates_func = array(); // get rendered template // disable caching for evaluated code if ($_template->source->recompiled) { diff --git a/libs/sysplugins/smarty_internal_utility.php b/libs/sysplugins/smarty_internal_utility.php index d52f6056..184ddea5 100644 --- a/libs/sysplugins/smarty_internal_utility.php +++ b/libs/sysplugins/smarty_internal_utility.php @@ -192,10 +192,10 @@ class Smarty_Internal_Utility { if ($tpl->source->exists) { $_resource_part_1 = basename(str_replace('^', '/', $tpl->compiled->filepath)); // remove from template cache - unset($smarty->template_objects[sha1($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); + unset($smarty->template_objects[sha1(join(DIRECTORY_SEPARATOR, $smarty->getTemplateDir()).$tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); } else { // remove from template cache - unset($smarty->template_objects[sha1($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); + unset($smarty->template_objects[sha1(join(DIRECTORY_SEPARATOR, $smarty->getTemplateDir()).$tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]); return 0; } $_resource_part_2 = str_replace('.php','.cache.php',$_resource_part_1);