From a9e7e8881e36ef59b8cf9a948a54a1a15e2b794c Mon Sep 17 00:00:00 2001 From: uwetews Date: Mon, 19 Sep 2016 14:59:49 +0200 Subject: [PATCH] - optimization clear compiled and cached folder completely on detected version change --- change_log.txt | 3 +++ libs/Smarty.class.php | 24 ++++++++++++++++++- .../smarty_internal_extension_clear.php | 13 ++++++---- ..._internal_method_clearcompiledtemplate.php | 8 ++++--- libs/sysplugins/smarty_internal_template.php | 10 ++++---- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/change_log.txt b/change_log.txt index df9d8e49..c44480e7 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@ ===== 3.1.31-dev ===== (xx.xx.xx) + 19.09.2016 + - optimization clear compiled and cached folder completely on detected version change + 15.09.2016 - bugfix assigning a variable in if condition by function like {if $value = array_shift($array)} the function got called twice https://github.com/smarty-php/smarty/issues/291 - bugfix function plugins called with assign attribute like {foo assign='bar'} did not output returned content because diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index ea670e7a..7529fead 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -114,7 +114,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.31-dev/23'; + const SMARTY_VERSION = '3.1.31-dev/24'; /** * define variable scopes @@ -1005,6 +1005,9 @@ class Smarty extends Smarty_Internal_TemplateBase if (!$this->_compileDirNormalized) { $this->_normalizeDir('compile_dir', $this->compile_dir); $this->_compileDirNormalized = true; + if ($this->_isNewRelease($this->compile_dir)) { + $this->clearCompiledTemplate(); + } } return $this->compile_dir; } @@ -1033,6 +1036,9 @@ class Smarty extends Smarty_Internal_TemplateBase if (!$this->_cacheDirNormalized) { $this->_normalizeDir('cache_dir', $this->cache_dir); $this->_cacheDirNormalized = true; + if ($this->_isNewRelease($this->cache_dir)) { + $this->clearAllCache(); + } } return $this->cache_dir; } @@ -1246,6 +1252,22 @@ class Smarty extends Smarty_Internal_TemplateBase $this->_cache[ 'tplObjects' ] = array(); } + /** + * check if new release was installed + * + * @param string $dir compiled oder cache dir path + * + * @return bool + */ + public function _isNewRelease ($dir) { + if (!is_file($file = $dir. 'version.txt') || file_get_contents($file) !== Smarty::SMARTY_VERSION) { + file_put_contents($file, Smarty::SMARTY_VERSION); + return true; + } else { + return false; + } + } + /** * Get Smarty object * diff --git a/libs/sysplugins/smarty_internal_extension_clear.php b/libs/sysplugins/smarty_internal_extension_clear.php index 5d0dab94..96a62d71 100644 --- a/libs/sysplugins/smarty_internal_extension_clear.php +++ b/libs/sysplugins/smarty_internal_extension_clear.php @@ -63,9 +63,10 @@ class Smarty_Internal_Extension_Clear $_cacheDirs = new RecursiveDirectoryIterator($_dir); $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST); foreach ($_cache as $_file) { - if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { + if (substr(basename($_file->getPathname()), 0, 1) == '.') { continue; } + $_filepath = (string) $_file; // directory ? if ($_file->isDir()) { if (!$_cache->isDot()) { @@ -73,7 +74,11 @@ class Smarty_Internal_Extension_Clear @rmdir($_file->getPathname()); } } else { - $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string) $_file, $_dir_length))); + // delete only php files + if (substr($_filepath, -4) !== '.php') { + continue; + } + $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length))); $_parts_count = count($_parts); // check name if (isset($resource_name)) { @@ -114,9 +119,9 @@ class Smarty_Internal_Extension_Clear } } } - $_count += @unlink((string) $_file) ? 1 : 0; + $_count += @unlink($_filepath) ? 1 : 0; if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) { - opcache_invalidate((string) $_file, true); + opcache_invalidate($_filepath, true); } } } diff --git a/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php b/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php index b73a2de3..a1f1a80b 100644 --- a/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php +++ b/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php @@ -75,18 +75,20 @@ class Smarty_Internal_Method_ClearCompiledTemplate } $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); foreach ($_compile as $_file) { - if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { + if (substr(basename($_file->getPathname()), 0, 1) == '.') { continue; } - $_filepath = (string) $_file; - if ($_file->isDir()) { if (!$_compile->isDot()) { // delete folder if empty @rmdir($_file->getPathname()); } } else { + // delete only php files + if (substr($_filepath, -4) !== '.php') { + continue; + } $unlink = false; if ((!isset($_compile_id) || (isset($_filepath[ $_compile_id_part_length ]) && $a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) && diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 9776016f..5096eee3 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -419,11 +419,13 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase */ public function _decodeProperties(Smarty_Internal_Template $tpl, $properties, $cache = false) { + // on cache resources other than file check version stored in cache code + if ($cache && $tpl->smarty->caching_type !== 'file' && Smarty::SMARTY_VERSION !== $properties[ 'version' ]) { + $tpl->smarty->clearAllCache(); + return false; + } $is_valid = true; - if (Smarty::SMARTY_VERSION != $properties[ 'version' ]) { - // new version must rebuild - $is_valid = false; - } elseif ($is_valid && !empty($properties[ 'file_dependency' ]) && + if (!empty($properties[ 'file_dependency' ]) && ((!$cache && $tpl->smarty->compile_check) || $tpl->smarty->compile_check == 1) ) { // check file dependencies at compiled code