diff --git a/change_log.txt b/change_log.txt index 564118dc..88bb40df 100644 --- a/change_log.txt +++ b/change_log.txt @@ -7,6 +7,7 @@ - move $smarty->loadPlugin() into extension - minor compiler optimizations - bugfix allow function plugins with name ending with 'close' https://github.com/smarty-php/smarty/issues/52 + - rework of $smarty->clearCompiledTemplate() and move it to its own extension 19.06.2015 - improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59 diff --git a/libs/Autoloader.php b/libs/Autoloader.php index 7e4370d3..a7356b2b 100644 --- a/libs/Autoloader.php +++ b/libs/Autoloader.php @@ -25,65 +25,65 @@ class Smarty_Autoloader * @var string */ public static $SMARTY_DIR = ''; + /** * Filepath to Smarty internal plugins * * @var string */ public static $SMARTY_SYSPLUGINS_DIR = ''; + /** * Array of not existing classes to avoid is_file calls for already tested classes * * @var array */ public static $unknown = array(); + /** * Array with Smarty core classes and their filename * * @var array */ - public static $rootClasses = array('Smarty' => 'Smarty.class.php', - 'SmartyBC' => 'SmartyBC.class.php', - ); + public static $rootClasses = array('Smarty' => 'Smarty.class.php', 'SmartyBC' => 'SmartyBC.class.php',); - private static $syspluginsClasses = array( - 'smarty_config_source' => true, - 'smarty_security' => true, - 'smarty_cacheresource' => true, - 'smarty_compiledresource' => true, - 'smarty_cacheresource_custom' => true, - 'smarty_cacheresource_keyvaluestore' => true, - 'smarty_resource' => true, - 'smarty_resource_custom' => true, - 'smarty_resource_uncompiled' => true, - 'smarty_resource_recompiled' => true, - 'smarty_template_source' => true, - 'smarty_template_compiled' => true, - 'smarty_template_cached' => true, - 'smarty_template_config' => true, - 'smarty_data' => true, - 'smarty_variable' => true, - 'smarty_undefined_variable' => true, - 'smartyexception' => true, - 'smartycompilerexception' => true, - 'smarty_internal_data' => true, - 'smarty_internal_template' => true, - 'smarty_internal_templatebase' => true, - 'smarty_internal_resource_file' => true, - 'smarty_internal_resource_extends' => true, - 'smarty_internal_resource_eval' => true, - 'smarty_internal_resource_string' => true, - 'smarty_internal_resource_registered' => true, - 'smarty_internal_extension_codeframe' => true, - 'smarty_internal_extension_config' => true, - 'smarty_internal_extension_filter' => true, - 'smarty_internal_extension_object' => true, - 'smarty_internal_extension_loadplugin' => true, - 'smarty_internal_filter_handler' => true, - 'smarty_internal_function_call_handler' => true, - 'smarty_internal_cacheresource_file' => true, - 'smarty_internal_write_file' => true, - ); + private static $syspluginsClasses = array('smarty_config_source' => true, + 'smarty_security' => true, + 'smarty_cacheresource' => true, + 'smarty_compiledresource' => true, + 'smarty_cacheresource_custom' => true, + 'smarty_cacheresource_keyvaluestore' => true, + 'smarty_resource' => true, + 'smarty_resource_custom' => true, + 'smarty_resource_uncompiled' => true, + 'smarty_resource_recompiled' => true, + 'smarty_template_source' => true, + 'smarty_template_compiled' => true, + 'smarty_template_cached' => true, + 'smarty_template_config' => true, + 'smarty_data' => true, + 'smarty_variable' => true, + 'smarty_undefined_variable' => true, + 'smartyexception' => true, + 'smartycompilerexception' => true, + 'smarty_internal_data' => true, + 'smarty_internal_template' => true, + 'smarty_internal_templatebase' => true, + 'smarty_internal_resource_file' => true, + 'smarty_internal_resource_extends' => true, + 'smarty_internal_resource_eval' => true, + 'smarty_internal_resource_string' => true, + 'smarty_internal_resource_registered' => true, + 'smarty_internal_extension_codeframe' => true, + 'smarty_internal_extension_config' => true, + 'smarty_internal_extension_filter' => true, + 'smarty_internal_extension_object' => true, + 'smarty_internal_extension_loadplugin' => true, + 'smarty_internal_extension_clearcompiled' => true, + 'smarty_internal_filter_handler' => true, + 'smarty_internal_function_call_handler' => true, + 'smarty_internal_cacheresource_file' => true, + 'smarty_internal_write_file' => true,); /** * Registers Smarty_Autoloader backward compatible to older installations. diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index c8b4f18f..44c2d737 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/10'; + const SMARTY_VERSION = '3.1.28-dev/11'; /** * define variable scopes @@ -1483,7 +1483,7 @@ class Smarty extends Smarty_Internal_TemplateBase */ public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null) { - return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this); + return Smarty_Internal_Extension_ClearCompiled::clearCompiledTemplate($this, $resource_name, $compile_id, $exp_time); } /** diff --git a/libs/sysplugins/smarty_internal_extension_clearcompiled.php b/libs/sysplugins/smarty_internal_extension_clearcompiled.php new file mode 100644 index 00000000..9f6b3e57 --- /dev/null +++ b/libs/sysplugins/smarty_internal_extension_clearcompiled.php @@ -0,0 +1,111 @@ +clearCompiledTemplate() method + * + * @package Smarty + * @subpackage PluginsInternal + * @author Uwe Tews + */ +class Smarty_Internal_Extension_ClearCompiled +{ + /** + * Delete compiled template file + * + * @param Smarty $smarty Smarty instance + * @param string $resource_name template name + * @param string $compile_id compile id + * @param integer $exp_time expiration time + * + * @return integer number of template files deleted + */ + public static function clearCompiledTemplate(Smarty $smarty, $resource_name, $compile_id, $exp_time) + { + $_compile_dir = $smarty->getCompileDir(); + if ($_compile_dir == '/') { //We should never want to delete this! + return 0; + } + $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; + $_dir_sep = $smarty->use_sub_dirs ? DS : '^'; + if (isset($resource_name)) { + $_save_stat = $smarty->caching; + $smarty->caching = false; + $tpl = new $smarty->template_class($resource_name, $smarty); + $smarty->caching = $_save_stat; + $tpl->loadSource(); // have the template registered before unset() + if ($tpl->source->exists) { + // remove from template cache + $_templateId = $tpl->getTemplateId($resource_name); + if (isset($smarty->template_objects[$_templateId])) { + unset($smarty->template_objects[$_templateId]); + } + $_resource_part_1 = basename(str_replace('^', DS, $tpl->compiled->filepath)); + $_resource_part_1_length = strlen($_resource_part_1); + } else { + return 0; + } + + $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1); + $_resource_part_2_length = strlen($_resource_part_2); + } + $_dir = $_compile_dir; + if ($smarty->use_sub_dirs && isset($_compile_id)) { + $_dir .= $_compile_id . $_dir_sep; + } + if (isset($_compile_id)) { + $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep; + $_compile_id_part_length = strlen($_compile_id_part); + } + $_count = 0; + try { + $_compileDirs = new RecursiveDirectoryIterator($_dir); + // NOTE: UnexpectedValueException thrown for PHP >= 5.3 + } + catch (Exception $e) { + return 0; + } + $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); + foreach ($_compile as $_file) { + if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { + continue; + } + + $_filepath = (string) $_file; + + if ($_file->isDir()) { + if (!$_compile->isDot()) { + // delete folder if empty + @rmdir($_file->getPathname()); + } + } else { + $unlink = false; + if ((!isset($_compile_id) || (isset($_filepath[$_compile_id_part_length]) && $a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) + && (!isset($resource_name) + || (isset($_filepath[$_resource_part_1_length]) + && substr_compare($_filepath, $_resource_part_1, - $_resource_part_1_length, $_resource_part_1_length) == 0) + || (isset($_filepath[$_resource_part_2_length]) + && substr_compare($_filepath, $_resource_part_2, - $_resource_part_2_length, $_resource_part_2_length) == 0)) + ) { + if (isset($exp_time)) { + if (time() - @filemtime($_filepath) >= $exp_time) { + $unlink = true; + } + } else { + $unlink = true; + } + } + + if ($unlink && @unlink($_filepath)) { + $_count ++; + } + } + } + // clear compiled cache + Smarty_Resource::$sources = array(); + Smarty_Resource::$compileds = array(); + + return $_count; + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_utility.php b/libs/sysplugins/smarty_internal_utility.php index cd027dec..a6405722 100644 --- a/libs/sysplugins/smarty_internal_utility.php +++ b/libs/sysplugins/smarty_internal_utility.php @@ -179,111 +179,6 @@ class Smarty_Internal_Utility return $_count; } - /** - * Delete compiled template file - * - * @param string $resource_name template name - * @param string $compile_id compile id - * @param integer $exp_time expiration time - * @param Smarty $smarty Smarty instance - * - * @return integer number of template files deleted - */ - public static function clearCompiledTemplate($resource_name, $compile_id, $exp_time, Smarty $smarty) - { - $_compile_dir = realpath($smarty->getCompileDir()) . '/'; - if ($_compile_dir == '/') { //We should never want to delete this! - return 0; - } - $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; - $_dir_sep = $smarty->use_sub_dirs ? '/' : '^'; - if (isset($resource_name)) { - $_save_stat = $smarty->caching; - $smarty->caching = false; - $tpl = new $smarty->template_class($resource_name, $smarty); - $smarty->caching = $_save_stat; - - // remove from template cache - $tpl->source; // have the template registered before unset() - if ($smarty->allow_ambiguous_resources) { - $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; - } else { - $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; - } - if (isset($_templateId[150])) { - $_templateId = sha1($_templateId); - } - unset($smarty->template_objects[$_templateId]); - - if ($tpl->source->exists) { - $_resource_part_1 = basename(str_replace('^', '/', $tpl->compiled->filepath)); - $_resource_part_1_length = strlen($_resource_part_1); - } else { - return 0; - } - - $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1); - $_resource_part_2_length = strlen($_resource_part_2); - } - $_dir = $_compile_dir; - if ($smarty->use_sub_dirs && isset($_compile_id)) { - $_dir .= $_compile_id . $_dir_sep; - } - if (isset($_compile_id)) { - $_compile_id_part = str_replace('\\', '/', $_compile_dir . $_compile_id . $_dir_sep); - $_compile_id_part_length = strlen($_compile_id_part); - } - $_count = 0; - try { - $_compileDirs = new RecursiveDirectoryIterator($_dir); - // NOTE: UnexpectedValueException thrown for PHP >= 5.3 - } - catch (Exception $e) { - return 0; - } - $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); - foreach ($_compile as $_file) { - if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { - continue; - } - - $_filepath = str_replace('\\', '/', (string) $_file); - - if ($_file->isDir()) { - if (!$_compile->isDot()) { - // delete folder if empty - @rmdir($_file->getPathname()); - } - } else { - $unlink = false; - if ((!isset($_compile_id) || (isset($_filepath[$_compile_id_part_length]) && $a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) - && (!isset($resource_name) - || (isset($_filepath[$_resource_part_1_length]) - && substr_compare($_filepath, $_resource_part_1, - $_resource_part_1_length, $_resource_part_1_length) == 0) - || (isset($_filepath[$_resource_part_2_length]) - && substr_compare($_filepath, $_resource_part_2, - $_resource_part_2_length, $_resource_part_2_length) == 0)) - ) { - if (isset($exp_time)) { - if (time() - @filemtime($_filepath) >= $exp_time) { - $unlink = true; - } - } else { - $unlink = true; - } - } - - if ($unlink && @unlink($_filepath)) { - $_count ++; - } - } - } - // clear compiled cache - Smarty_Resource::$sources = array(); - Smarty_Resource::$compileds = array(); - - return $_count; - } - /** * Return array of tag/attributes of all tags used by an template *