From 6ef0d0eaa0dc55e68de315c013ec255f227dc5e7 Mon Sep 17 00:00:00 2001 From: uwetews Date: Sun, 15 May 2016 11:13:31 +0200 Subject: [PATCH] - optimization and cleanup of resource code --- change_log.txt | 5 +- libs/Smarty.class.php | 4 +- libs/sysplugins/smarty_cacheresource.php | 13 +- .../smarty_cacheresource_custom.php | 19 +-- .../smarty_cacheresource_keyvaluestore.php | 21 +-- .../smarty_internal_cacheresource_file.php | 63 ++++----- ..._method_registerdefaulttemplatehandler.php | 5 +- .../smarty_internal_resource_eval.php | 3 +- .../smarty_internal_resource_file.php | 7 +- .../smarty_internal_resource_php.php | 2 +- .../smarty_internal_resource_registered.php | 6 +- .../smarty_internal_resource_stream.php | 3 +- .../smarty_internal_resource_string.php | 14 +- libs/sysplugins/smarty_internal_template.php | 8 +- .../smarty_internal_templatebase.php | 7 +- .../smarty_internal_templatecompilerbase.php | 8 +- libs/sysplugins/smarty_resource.php | 8 +- .../sysplugins/smarty_resource_recompiled.php | 45 +++++++ .../sysplugins/smarty_resource_uncompiled.php | 18 +-- libs/sysplugins/smarty_template_cached.php | 2 +- libs/sysplugins/smarty_template_compiled.php | 127 ++++++------------ libs/sysplugins/smarty_template_config.php | 2 - .../smarty_template_resource_base.php | 8 +- libs/sysplugins/smarty_template_source.php | 7 +- 24 files changed, 202 insertions(+), 203 deletions(-) diff --git a/change_log.txt b/change_log.txt index 5352df0c..d9559eed 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,7 +1,10 @@  ===== 3.1.30-dev ===== (xx.xx.xx) + 15.05.2016 + - optimization and cleanup of resource code + 10.05.2016 - optimization of inheritance processing - + 07.05.2016 -bugfix Only variables should be assigned by reference https://github.com/smarty-php/smarty/issues/227 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index d29fe841..ebe320ec 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.30-dev/68'; + const SMARTY_VERSION = '3.1.30-dev/69'; /** * define variable scopes @@ -1138,7 +1138,7 @@ class Smarty extends Smarty_Internal_TemplateBase */ public function _realpath($path, $realpath = null) { - $nds = DS == '/' ? '\\' : '/'; + $nds = DS == '/' ? '\\' : '/'; // normalize DS $path = str_replace($nds, DS, $path); preg_match('%^(?(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?(?:[[:print:]]*))$%', diff --git a/libs/sysplugins/smarty_cacheresource.php b/libs/sysplugins/smarty_cacheresource.php index 4d668838..2a4552cb 100644 --- a/libs/sysplugins/smarty_cacheresource.php +++ b/libs/sysplugins/smarty_cacheresource.php @@ -46,9 +46,9 @@ abstract class Smarty_CacheResource * * @param Smarty_Internal_Template $_template template object * @param Smarty_Template_Cached $cached cached object - * @param bool $update flag if called because cache update + * @param boolean $update flag if called because cache update * - * @return bool true or false if the cached content does not exist + * @return boolean true or false if the cached content does not exist */ abstract public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null, $update = false); @@ -63,6 +63,15 @@ abstract class Smarty_CacheResource */ abstract public function writeCachedContent(Smarty_Internal_Template $_template, $content); + /** + * Read cached template from cache + * + * @param Smarty_Internal_Template $_template template object + * + * @return string content + */ + abstract function readCachedContent(Smarty_Internal_Template $_template); + /** * Return cached content * diff --git a/libs/sysplugins/smarty_cacheresource_custom.php b/libs/sysplugins/smarty_cacheresource_custom.php index 6a62f26f..8f1290e4 100644 --- a/libs/sysplugins/smarty_cacheresource_custom.php +++ b/libs/sysplugins/smarty_cacheresource_custom.php @@ -43,7 +43,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource */ protected function fetchTimestamp($id, $name, $cache_id, $compile_id) { - return null; + return false; } /** @@ -63,9 +63,9 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource /** * Delete content from cache * - * @param string $name template name - * @param string $cache_id cache id - * @param string $compile_id compile id + * @param string|null $name template name + * @param string|null $cache_id cache id + * @param string|null $compile_id compile id * @param integer|null $exp_time seconds till expiration time in seconds or null * * @return integer number of deleted caches @@ -119,13 +119,14 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource /** * Read the cached template and process the header * - * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template - * @param Smarty_Template_Cached $cached cached object - * @param bool $update flag if called because cache update + * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template + * @param Smarty_Template_Cached $cached cached object + * @param boolean $update flag if called because cache update * * @return boolean true or false if the cached content does not exist */ - public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null, $update = false) + public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null, + $update = false) { if (!$cached) { $cached = $_smarty_tpl->cached; @@ -164,7 +165,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource * * @param Smarty_Internal_Template $_template template object * - * @return string content + * @return string|boolean content */ public function readCachedContent(Smarty_Internal_Template $_template) { diff --git a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php index 125e9e8c..bab1b5c0 100644 --- a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php +++ b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php @@ -54,7 +54,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource */ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) { - $cached->filepath = sha1($_template->source->uid) . '#' . $this->sanitize($cached->source->resource) . '#' . + $cached->filepath = $_template->source->uid . '#' . $this->sanitize($cached->source->resource) . '#' . $this->sanitize($cached->cache_id) . '#' . $this->sanitize($cached->compile_id); $this->populateTimestamp($cached); @@ -76,19 +76,20 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource } $cached->content = $content; $cached->timestamp = (int) $timestamp; - $cached->exists = $cached->timestamp; + $cached->exists = !!$cached->timestamp; } /** * Read the cached template and process the header * - * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template - * @param Smarty_Template_Cached $cached cached object - * @param bool $update flag if called because cache update + * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template + * @param Smarty_Template_Cached $cached cached object + * @param boolean $update flag if called because cache update * * @return boolean true or false if the cached content does not exist */ - public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null, $update = false) + public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null, + $update = false) { if (!$cached) { $cached = $_smarty_tpl->cached; @@ -131,7 +132,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource * * @param Smarty_Internal_Template $_template template object * - * @return string content + * @return string|false content */ public function readCachedContent(Smarty_Internal_Template $_template) { @@ -226,7 +227,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource { $string = trim($string, '|'); if (!$string) { - return null; + return ''; } return preg_replace('#[^\w\|]+#S', '_', $string); } @@ -286,6 +287,10 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource protected function getMetaTimestamp(&$content) { extract(unpack('N1s/N1m/a*content', $content)); + /** + * @var int $s + * @var int $m + */ return $s + ($m / 100000000); } diff --git a/libs/sysplugins/smarty_internal_cacheresource_file.php b/libs/sysplugins/smarty_internal_cacheresource_file.php index 6550b617..cfab6614 100644 --- a/libs/sysplugins/smarty_internal_cacheresource_file.php +++ b/libs/sysplugins/smarty_internal_cacheresource_file.php @@ -27,43 +27,31 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource */ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) { - $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null; - $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w]+!', '_', $_template->compile_id) : null; - $_filepath = sha1($_template->source->uid . $_template->smarty->_joined_template_dir); + $source = &$_template->source; + $smarty = &$_template->smarty; + $_compile_dir_sep = $smarty->use_sub_dirs ? DS : '^'; + $_filepath = sha1($source->uid . $smarty->_joined_template_dir); + $cached->filepath = $smarty->getCacheDir(); + if (isset($_template->cache_id)) { + $cached->filepath .= preg_replace(array('![^\w|]+!', '![|]+!'), array('_', $_compile_dir_sep), + $_template->cache_id) . $_compile_dir_sep; + } + if (isset($_template->compile_id)) { + $cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep; + } // if use_sub_dirs, break file into directories - if ($_template->smarty->use_sub_dirs) { - $_filepath = substr($_filepath, 0, 2) . DS . substr($_filepath, 2, 2) . DS . substr($_filepath, 4, 2) . DS . - $_filepath; + if ($smarty->use_sub_dirs) { + $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DS . $_filepath[ 2 ] . $_filepath[ 3 ] . DS . + $_filepath[ 4 ] . $_filepath[ 5 ] . DS; } - $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^'; - if (isset($_cache_id)) { - $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep; - } else { - $_cache_id = ''; + $cached->filepath .= $_filepath; + if (!empty($basename = $source->handler->getBasename($source))) { + $cached->filepath .= '.' . $basename; } - if (isset($_compile_id)) { - $_compile_id = $_compile_id . $_compile_dir_sep; - } else { - $_compile_id = ''; + if ($smarty->cache_locking) { + $cached->lock_id = $cached->filepath . '.lock'; } - $_cache_dir = $_template->smarty->getCacheDir(); - if ($_template->smarty->cache_locking) { - // create locking file name - // relative file name? - if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_cache_dir)) { - $_lock_dir = rtrim(getcwd(), '/\\') . DS . $_cache_dir; - } else { - $_lock_dir = $_cache_dir; - } - $cached->lock_id = $_lock_dir . sha1($_cache_id . $_compile_id . $_template->source->uid) . '.lock'; - } - // set basename - $_basename = $_template->source->handler->getBasename($_template->source); - if ($_basename === null) { - $_basename = basename(preg_replace('![^\w]+!', '_', $_template->source->name)); - } - $cached->filepath = - $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . $_basename . '.php'; + $cached->filepath .= '.php'; $cached->timestamp = $cached->exists = is_file($cached->filepath); if ($cached->exists) { $cached->timestamp = filemtime($cached->filepath); @@ -88,13 +76,14 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource /** * Read the cached template and process its header * - * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template - * @param Smarty_Template_Cached $cached cached object - * @param bool $update flag if called because cache update + * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template + * @param Smarty_Template_Cached $cached cached object + * @param bool $update flag if called because cache update * * @return boolean true or false if the cached content does not exist */ - public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null, $update = false) + public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null, + $update = false) { $_smarty_tpl->cached->valid = false; if ($update && defined('HHVM_VERSION')) { diff --git a/libs/sysplugins/smarty_internal_method_registerdefaulttemplatehandler.php b/libs/sysplugins/smarty_internal_method_registerdefaulttemplatehandler.php index 0e47b56b..17a579b9 100644 --- a/libs/sysplugins/smarty_internal_method_registerdefaulttemplatehandler.php +++ b/libs/sysplugins/smarty_internal_method_registerdefaulttemplatehandler.php @@ -64,10 +64,7 @@ class Smarty_Internal_Method_RegisterDefaultTemplateHandler $source->uid = sha1($source->filepath); } elseif ($_return === true) { $source->content = $_content; - $source->timestamp = $_timestamp; - $source->exists = true; - $source->handler->recompiled = true; - $source->filepath = false; + $source->handler = Smarty_Resource::load($source->smarty, 'eval'); } } } \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_resource_eval.php b/libs/sysplugins/smarty_internal_resource_eval.php index aca71035..d3088bdd 100644 --- a/libs/sysplugins/smarty_internal_resource_eval.php +++ b/libs/sysplugins/smarty_internal_resource_eval.php @@ -29,8 +29,7 @@ class Smarty_Internal_Resource_Eval extends Smarty_Resource_Recompiled public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $source->uid = $source->filepath = sha1($source->name); - $source->timestamp = false; - $source->exists = true; + $source->timestamp = $source->exists = true; } /** diff --git a/libs/sysplugins/smarty_internal_resource_file.php b/libs/sysplugins/smarty_internal_resource_file.php index adb3b90b..2901b87a 100644 --- a/libs/sysplugins/smarty_internal_resource_file.php +++ b/libs/sysplugins/smarty_internal_resource_file.php @@ -129,12 +129,9 @@ class Smarty_Internal_Resource_File extends Smarty_Resource $source->exists = true; $source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir : $source->smarty->_joined_template_dir)); - if ($source->smarty->compile_check == 1) { - $source->timestamp = filemtime($source->filepath); - } + $source->timestamp = filemtime($source->filepath); } else { - $source->timestamp = false; - $source->exists = false; + $source->timestamp = $source->exists = false; } } diff --git a/libs/sysplugins/smarty_internal_resource_php.php b/libs/sysplugins/smarty_internal_resource_php.php index 614011dd..3cff7813 100644 --- a/libs/sysplugins/smarty_internal_resource_php.php +++ b/libs/sysplugins/smarty_internal_resource_php.php @@ -101,7 +101,7 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File $compiled->timestamp = $_template->source->timestamp; $compiled->exists = $_template->source->exists; $compiled->file_dependency[ $_template->source->uid ] = - array($compiled->filepath, $_template->source->getTimeStamp(), + array($compiled->filepath, $compiled->timestamp, $_template->source->type,); } } diff --git a/libs/sysplugins/smarty_internal_resource_registered.php b/libs/sysplugins/smarty_internal_resource_registered.php index 186fcddd..8535257a 100644 --- a/libs/sysplugins/smarty_internal_resource_registered.php +++ b/libs/sysplugins/smarty_internal_resource_registered.php @@ -30,10 +30,8 @@ class Smarty_Internal_Resource_Registered extends Smarty_Resource { $source->filepath = $source->type . ':' . $source->name; $source->uid = sha1($source->filepath . $source->smarty->_joined_template_dir); - if ($source->smarty->compile_check) { - $source->timestamp = $this->getTemplateTimestamp($source); - $source->exists = !!$source->timestamp; - } + $source->timestamp = $this->getTemplateTimestamp($source); + $source->exists = !!$source->timestamp; } /** diff --git a/libs/sysplugins/smarty_internal_resource_stream.php b/libs/sysplugins/smarty_internal_resource_stream.php index 8a902268..b4248fbb 100644 --- a/libs/sysplugins/smarty_internal_resource_stream.php +++ b/libs/sysplugins/smarty_internal_resource_stream.php @@ -36,8 +36,7 @@ class Smarty_Internal_Resource_Stream extends Smarty_Resource_Recompiled } $source->uid = false; $source->content = $this->getContent($source); - $source->timestamp = false; - $source->exists = !!$source->content; + $source->timestamp = $source->exists = !!$source->content; } /** diff --git a/libs/sysplugins/smarty_internal_resource_string.php b/libs/sysplugins/smarty_internal_resource_string.php index 37e66be3..f2610d9d 100644 --- a/libs/sysplugins/smarty_internal_resource_string.php +++ b/libs/sysplugins/smarty_internal_resource_string.php @@ -29,8 +29,7 @@ class Smarty_Internal_Resource_String extends Smarty_Resource public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $source->uid = $source->filepath = sha1($source->name . $source->smarty->_joined_template_dir); - $source->timestamp = 0; - $source->exists = true; + $source->timestamp = $source->exists = true; } /** @@ -94,4 +93,15 @@ class Smarty_Internal_Resource_String extends Smarty_Resource { return ''; } + + /* + * Disable timestamp checks for string resource. + * + * @return bool + */ + public function checkTimestamps() + { + return false; + } } + diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 8c9d92ac..35d49d17 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -431,9 +431,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase ) { // check file dependencies at compiled code foreach ($properties[ 'file_dependency' ] as $_file_to_check) { - if ($_file_to_check[ 2 ] == 'file' || $_file_to_check[ 2 ] == 'extends' || - $_file_to_check[ 2 ] == 'php' - ) { + if ($_file_to_check[ 2 ] == 'file' || $_file_to_check[ 2 ] == 'php') { if ($tpl->source->filepath == $_file_to_check[ 0 ]) { // do not recheck current template continue; @@ -442,8 +440,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase // file and php types can be checked without loading the respective resource handlers $mtime = is_file($_file_to_check[ 0 ]) ? filemtime($_file_to_check[ 0 ]) : false; } - } elseif ($_file_to_check[ 2 ] == 'string') { - continue; } else { $handler = Smarty_Resource::load($tpl->smarty, $_file_to_check[ 2 ]); if ($handler->checkTimestamps()) { @@ -453,7 +449,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase continue; } } - if (!$mtime || $mtime > $_file_to_check[ 1 ]) { + if ($mtime === false || $mtime > $_file_to_check[ 1 ]) { $is_valid = false; break; } diff --git a/libs/sysplugins/smarty_internal_templatebase.php b/libs/sysplugins/smarty_internal_templatebase.php index 525091e0..0b123f6c 100644 --- a/libs/sysplugins/smarty_internal_templatebase.php +++ b/libs/sysplugins/smarty_internal_templatebase.php @@ -160,12 +160,11 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data throw new SmartyException($function . '():Template object expected'); } } else { - // get template object + // get template object /* @var Smarty_Internal_Template $template */ $saveVars = false; - - $template = - $smarty->createTemplate($template, $cache_id, $compile_id, $parent ? $parent : $this, false); + + $template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent ? $parent : $this, false); if ($this->_objType == 1) { // set caching in template object $template->caching = $this->caching; diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 04ad5d6e..2a5a3478 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -382,9 +382,11 @@ abstract class Smarty_Internal_TemplateCompilerBase $this->has_variable_string = false; $this->prefix_code = array(); // add file dependency - $this->parent_compiler->template->compiled->file_dependency[ $this->template->source->uid ] = - array($this->template->source->filepath, $this->template->source->getTimeStamp(), - $this->template->source->type,); + if ($this->smarty->merge_compiled_includes || $this->template->source->handler->checkTimestamps()) { + $this->parent_compiler->template->compiled->file_dependency[ $this->template->source->uid ] = + array($this->template->source->filepath, $this->template->source->getTimeStamp(), + $this->template->source->type,); + } $this->smarty->_current_file = $this->template->source->filepath; // get template source if (!empty($this->template->source->components)) { diff --git a/libs/sysplugins/smarty_resource.php b/libs/sysplugins/smarty_resource.php index b66b1530..a9253e12 100644 --- a/libs/sysplugins/smarty_resource.php +++ b/libs/sysplugins/smarty_resource.php @@ -13,6 +13,10 @@ * * @package Smarty * @subpackage TemplateResources + * + * @method renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template) + * @method populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template) + * @method process(Smarty_Internal_Template $_smarty_tpl) */ abstract class Smarty_Resource { @@ -48,7 +52,7 @@ abstract class Smarty_Resource * @var bool */ public $hasCompiledHandler = false; - + /** * Load template's source into current template object * @@ -110,7 +114,7 @@ abstract class Smarty_Resource */ public function getBasename(Smarty_Template_Source $source) { - return null; + return basename(preg_replace('![^\w]+!', '_', $source->name)); } /** diff --git a/libs/sysplugins/smarty_resource_recompiled.php b/libs/sysplugins/smarty_resource_recompiled.php index cfd73401..7e51d1b1 100644 --- a/libs/sysplugins/smarty_resource_recompiled.php +++ b/libs/sysplugins/smarty_resource_recompiled.php @@ -30,6 +30,41 @@ abstract class Smarty_Resource_Recompiled extends Smarty_Resource */ public $hasCompiledHandler = true; + /** + * compile template from source + * + * @param Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template + * + * @throws Exception + */ + public function process(Smarty_Internal_Template $_smarty_tpl) + { + $compiled = &$_smarty_tpl->compiled; + $compiled->file_dependency = array(); + $compiled->includes = array(); + $compiled->nocache_hash = null; + $compiled->unifunc = null; + $level = ob_get_level(); + ob_start(); + $_smarty_tpl->loadCompiler(); + // call compiler + try { + eval("?>" . $_smarty_tpl->compiler->compileTemplate($_smarty_tpl)); + } + catch (Exception $e) { + unset($_smarty_tpl->compiler); + while (ob_get_level() > $level) { + ob_end_clean(); + } + throw $e; + } + // release compiler object to free memory + unset($_smarty_tpl->compiler); + ob_get_clean(); + $compiled->timestamp = time(); + $compiled->exists = true; + } + /** * populate Compiled Object with compiled filepath * @@ -44,4 +79,14 @@ abstract class Smarty_Resource_Recompiled extends Smarty_Resource $compiled->timestamp = false; $compiled->exists = false; } + + /* + * Disable timestamp checks for recompiled resource. + * + * @return bool + */ + public function checkTimestamps() + { + return false; + } } diff --git a/libs/sysplugins/smarty_resource_uncompiled.php b/libs/sysplugins/smarty_resource_uncompiled.php index 9bf7355f..554f3aea 100644 --- a/libs/sysplugins/smarty_resource_uncompiled.php +++ b/libs/sysplugins/smarty_resource_uncompiled.php @@ -29,17 +29,7 @@ abstract class Smarty_Resource_Uncompiled extends Smarty_Resource * @var bool */ public $hasCompiledHandler = true; - - /** - * Render and output the template (without using the compiler) - * - * @param Smarty_Template_Source $source source object - * @param Smarty_Internal_Template $_template template object - * - * @throws SmartyException on failure - */ - abstract public function renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template); - + /** * populate compiled object with compiled filepath * @@ -51,7 +41,9 @@ abstract class Smarty_Resource_Uncompiled extends Smarty_Resource $compiled->filepath = $_template->source->filepath; $compiled->timestamp = $_template->source->timestamp; $compiled->exists = $_template->source->exists; - $compiled->file_dependency[ $_template->source->uid ] = - array($compiled->filepath, $compiled->timestamp, $_template->source->type,); + if ($_template->smarty->merge_compiled_includes || $_template->source->handler->checkTimestamps()) { + $compiled->file_dependency[ $_template->source->uid ] = + array($compiled->filepath, $compiled->timestamp, $_template->source->type,); + } } } diff --git a/libs/sysplugins/smarty_template_cached.php b/libs/sysplugins/smarty_template_cached.php index 297a4148..d44c2c43 100644 --- a/libs/sysplugins/smarty_template_cached.php +++ b/libs/sysplugins/smarty_template_cached.php @@ -241,7 +241,7 @@ class Smarty_Template_Cached extends Smarty_Template_Resource_Base * * @param Smarty_Internal_Template $_template template object * - * @return string content + * @return string|false content */ public function read(Smarty_Internal_Template $_template) { diff --git a/libs/sysplugins/smarty_template_compiled.php b/libs/sysplugins/smarty_template_compiled.php index 5f702d6b..04235ab8 100644 --- a/libs/sysplugins/smarty_template_compiled.php +++ b/libs/sysplugins/smarty_template_compiled.php @@ -44,44 +44,36 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base **/ public function populateCompiledFilepath(Smarty_Internal_Template $_template) { - $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w]+!', '_', $_template->compile_id) : null; - if ($_template->source->isConfig) { - $_flag = '_' . - ((int) $_template->smarty->config_read_hidden + (int) $_template->smarty->config_booleanize * 2 + - (int) $_template->smarty->config_overwrite * 4); - } else { - $_flag = - '_' . ((int) $_template->smarty->merge_compiled_includes + (int) $_template->smarty->escape_html * 2); + $source = &$_template->source; + $smarty = &$_template->smarty; + $this->filepath = $smarty->getCompileDir(); + if (isset($_template->compile_id)) { + $this->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . + ($smarty->use_sub_dirs ? DS : '^'); } - $_filepath = $_template->source->uid . $_flag; // if use_sub_dirs, break file into directories - if ($_template->smarty->use_sub_dirs) { - $_filepath = substr($_filepath, 0, 2) . DS . substr($_filepath, 2, 2) . DS . substr($_filepath, 4, 2) . DS . - $_filepath; + if ($smarty->use_sub_dirs) { + $this->filepath .= $source->uid[ 0 ] . $source->uid[ 1 ] . DS . $source->uid[ 2 ] . $source->uid[ 3 ] . DS . + $source->uid[ 4 ] . $source->uid[ 5 ] . DS; } - if (isset($_compile_id)) { - $_filepath = $_compile_id . ($_template->smarty->use_sub_dirs ? DS : '^') . $_filepath; - } - // caching token - if ($_template->caching) { - $_cache = '.cache'; + $this->filepath .= $source->uid . '_'; + if ($source->isConfig) { + $this->filepath .= (int) $smarty->config_read_hidden + (int) $smarty->config_booleanize * 2 + + (int) $smarty->config_overwrite * 4; } else { - $_cache = ''; + $this->filepath .= (int) $smarty->merge_compiled_includes + (int) $smarty->escape_html * 2; } - // set basename - $_basename = $_template->source->handler->getBasename($_template->source); - if ($_basename === null) { - $_basename = basename(preg_replace('![^\w]+!', '_', $_template->source->name)); + $this->filepath .= '.' . $source->type; + if (!empty($basename = $source->handler->getBasename($source))) { + $this->filepath .= '.' . $basename; } - // separate (optional) basename by dot - if ($_basename) { - $_basename = '.' . $_basename; + if ($_template->caching) { + $this->filepath .= '.cache'; } - - $this->filepath = $_template->smarty->getCompileDir() . $_filepath . '.' . $_template->source->type . $_basename . $_cache . '.php'; - $this->exists = is_file($this->filepath); - if (!$this->exists) { - $this->timestamp = false; + $this->filepath .= '.php'; + $this->timestamp = $this->exists = is_file($this->filepath); + if ($this->exists) { + $this->timestamp = filemtime($this->filepath); } } @@ -94,40 +86,28 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base */ public function process(Smarty_Internal_Template $_smarty_tpl) { - if (!$_smarty_tpl->source->handler->uncompiled) { - if ($_smarty_tpl->source->handler->recompiled || !$this->exists || $_smarty_tpl->smarty->force_compile || - ($_smarty_tpl->smarty->compile_check && $_smarty_tpl->source->getTimeStamp() > $this->getTimeStamp()) + $source = &$_smarty_tpl->source; + $smarty = &$_smarty_tpl->smarty; + if ($source->handler->recompiled) { + $source->handler->process($_smarty_tpl); + } elseif (!$source->handler->uncompiled) { + if (!$this->exists || $smarty->force_compile || + ($smarty->compile_check && $source->getTimeStamp() > $this->getTimeStamp()) ) { $this->compileTemplateSource($_smarty_tpl); - $compileCheck = $_smarty_tpl->smarty->compile_check; - $_smarty_tpl->smarty->compile_check = false; - if ($_smarty_tpl->source->handler->recompiled) { - $level = ob_get_level(); - ob_start(); - try { - eval("?>" . $this->content); - } - catch (Exception $e) { - while (ob_get_level() > $level) { - ob_end_clean(); - } - throw $e; - } - ob_get_clean(); - $this->content = null; - } else { - $this->loadCompiledTemplate($_smarty_tpl); - } - $_smarty_tpl->smarty->compile_check = $compileCheck; + $compileCheck = $smarty->compile_check; + $smarty->compile_check = false; + $this->loadCompiledTemplate($_smarty_tpl); + $smarty->compile_check = $compileCheck; } else { $_smarty_tpl->mustCompile = true; @include($this->filepath); if ($_smarty_tpl->mustCompile) { $this->compileTemplateSource($_smarty_tpl); - $compileCheck = $_smarty_tpl->smarty->compile_check; - $_smarty_tpl->smarty->compile_check = false; + $compileCheck = $smarty->compile_check; + $smarty->compile_check = false; $this->loadCompiledTemplate($_smarty_tpl); - $_smarty_tpl->smarty->compile_check = $compileCheck; + $smarty->compile_check = $compileCheck; } } $_smarty_tpl->_subTemplateRegister(); @@ -139,7 +119,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base * Load fresh compiled template by including the PHP file * HHVM requires a work around because of a PHP incompatibility * - * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template + * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template */ private function loadCompiledTemplate(Smarty_Internal_Template $_smarty_tpl) { @@ -196,7 +176,6 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base * * @param Smarty_Internal_Template $_template * - * @return string * @throws Exception */ public function compileTemplateSource(Smarty_Internal_Template $_template) @@ -206,32 +185,14 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base $this->nocache_hash = null; $this->unifunc = null; // compile locking - if (!$_template->source->handler->recompiled) { - if ($saved_timestamp = $this->getTimeStamp()) { - touch($this->filepath); - } + if ($saved_timestamp = $this->getTimeStamp()) { + touch($this->filepath); } // call compiler - try { - $_template->loadCompiler(); - $code = $_template->compiler->compileTemplate($_template); - } - catch (Exception $e) { - // restore old timestamp in case of error - if (!$_template->source->handler->recompiled && $saved_timestamp) { - touch($this->filepath, $saved_timestamp); - } - throw $e; - } - // compiling succeeded - if ($_template->compiler->write_compiled_code) { - // write compiled template - $this->write($_template, $code); - $code = ''; - } + $_template->loadCompiler(); + $this->write($_template, $_template->compiler->compileTemplate($_template)); // release compiler object to free memory unset($_template->compiler); - return $code; } /** @@ -253,11 +214,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base } } return false; - } else { - $this->content = $code; } - $this->timestamp = time(); - $this->exists = true; return true; } diff --git a/libs/sysplugins/smarty_template_config.php b/libs/sysplugins/smarty_template_config.php index a6189283..57d30e3a 100644 --- a/libs/sysplugins/smarty_template_config.php +++ b/libs/sysplugins/smarty_template_config.php @@ -60,8 +60,6 @@ class Smarty_Template_Config extends Smarty_Template_Source */ public $template_parser_class = 'Smarty_Internal_Configfileparser'; - - /** * initialize Source Object for given resource * Either [$_template] or [$smarty, $template_resource] must be specified diff --git a/libs/sysplugins/smarty_template_resource_base.php b/libs/sysplugins/smarty_template_resource_base.php index 6b8aaaaf..6cc008a1 100644 --- a/libs/sysplugins/smarty_template_resource_base.php +++ b/libs/sysplugins/smarty_template_resource_base.php @@ -19,9 +19,9 @@ abstract class Smarty_Template_Resource_Base /** * Compiled Timestamp * - * @var integer + * @var integer|bool */ - public $timestamp = null; + public $timestamp = false; /** * Compiled Existence @@ -106,7 +106,6 @@ abstract class Smarty_Template_Resource_Base * @param \Smarty_Internal_Template $_template * @param string $unifunc function with template code * - * @return string * @throws \Exception */ public function getRenderedTemplateCode(Smarty_Internal_Template $_template, $unifunc = null) @@ -131,7 +130,6 @@ abstract class Smarty_Template_Resource_Base call_user_func($callback, $_template); } $_template->isRenderingCache = false; - return null; } catch (Exception $e) { $_template->isRenderingCache = false; @@ -152,7 +150,7 @@ abstract class Smarty_Template_Resource_Base */ public function getTimeStamp() { - if ($this->exists && !isset($this->timestamp)) { + if ($this->exists && !$this->timestamp) { $this->timestamp = filemtime($this->filepath); } return $this->timestamp; diff --git a/libs/sysplugins/smarty_template_source.php b/libs/sysplugins/smarty_template_source.php index fc5c3ec2..d066e0c3 100644 --- a/libs/sysplugins/smarty_template_source.php +++ b/libs/sysplugins/smarty_template_source.php @@ -135,8 +135,9 @@ class Smarty_Template_Source */ public function __construct(Smarty $smarty, $resource, $type, $name) { - $this->handler = isset($smarty->_cache[ 'resource_handlers' ][ $type ]) ? $smarty->_cache[ 'resource_handlers' ][ $type ] : - Smarty_Resource::load($smarty, $type); + $this->handler = + isset($smarty->_cache[ 'resource_handlers' ][ $type ]) ? $smarty->_cache[ 'resource_handlers' ][ $type ] : + Smarty_Resource::load($smarty, $type); $this->smarty = $smarty; $this->resource = $resource; $this->type = $type; @@ -182,7 +183,7 @@ class Smarty_Template_Source } return $source; } - + /** * Get source time stamp *