From 62bf9eeddcf4c82d16c862c7723e4960adc598c3 Mon Sep 17 00:00:00 2001 From: uwetews Date: Fri, 11 Mar 2016 01:07:26 +0100 Subject: [PATCH] - optimization of resource processing --- libs/Smarty.class.php | 2 +- .../smarty_cacheresource_custom.php | 14 ++--- .../smarty_cacheresource_keyvaluestore.php | 14 ++--- .../smarty_internal_cacheresource_file.php | 24 ++++----- libs/sysplugins/smarty_template_compiled.php | 52 +++++++++---------- .../smarty_template_resource_base.php | 2 +- 6 files changed, 48 insertions(+), 60 deletions(-) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 0b0d377b..795c11c4 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/54'; + const SMARTY_VERSION = '3.1.30-dev/55'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_cacheresource_custom.php b/libs/sysplugins/smarty_cacheresource_custom.php index 89197fa9..6a62f26f 100644 --- a/libs/sysplugins/smarty_cacheresource_custom.php +++ b/libs/sysplugins/smarty_cacheresource_custom.php @@ -119,28 +119,24 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource /** * Read the cached template and process the header * - * @param Smarty_Internal_Template $_template template object + * @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 $_template, 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 = $_template->cached; + $cached = $_smarty_tpl->cached; } $content = $cached->content ? $cached->content : null; $timestamp = $cached->timestamp ? $cached->timestamp : null; if ($content === null || !$timestamp) { - $this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, - $_template->compile_id, $content, $timestamp); + $this->fetch($_smarty_tpl->cached->filepath, $_smarty_tpl->source->name, $_smarty_tpl->cache_id, + $_smarty_tpl->compile_id, $content, $timestamp); } if (isset($content)) { - /** @var Smarty_Internal_Template $_smarty_tpl - * used in evaluated code - */ - $_smarty_tpl = $_template; eval("?>" . $content); $cached->content = null; return true; diff --git a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php index d6caf3dc..125e9e8c 100644 --- a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php +++ b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php @@ -82,31 +82,27 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource /** * Read the cached template and process the header * - * @param Smarty_Internal_Template $_template template object + * @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 $_template, 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 = $_template->cached; + $cached = $_smarty_tpl->cached; } $content = $cached->content ? $cached->content : null; $timestamp = $cached->timestamp ? $cached->timestamp : null; if ($content === null || !$timestamp) { - if (!$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, - $_template->compile_id, $content, $timestamp, $_template->source->uid) + if (!$this->fetch($_smarty_tpl->cached->filepath, $_smarty_tpl->source->name, $_smarty_tpl->cache_id, + $_smarty_tpl->compile_id, $content, $timestamp, $_smarty_tpl->source->uid) ) { return false; } } if (isset($content)) { - /** @var Smarty_Internal_Template $_smarty_tpl - * used in evaluated code - */ - $_smarty_tpl = $_template; eval("?>" . $content); return true; diff --git a/libs/sysplugins/smarty_internal_cacheresource_file.php b/libs/sysplugins/smarty_internal_cacheresource_file.php index 30053c04..6550b617 100644 --- a/libs/sysplugins/smarty_internal_cacheresource_file.php +++ b/libs/sysplugins/smarty_internal_cacheresource_file.php @@ -27,7 +27,6 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource */ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) { - $_source_file_path = str_replace(':', '.', $_template->source->filepath); $_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); @@ -58,8 +57,13 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource } $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($_source_file_path) . '.php'; + $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . $_basename . '.php'; $cached->timestamp = $cached->exists = is_file($cached->filepath); if ($cached->exists) { $cached->timestamp = filemtime($cached->filepath); @@ -84,24 +88,20 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource /** * Read the cached template and process its header * - * @param Smarty_Internal_Template $_template template object + * @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 $_template, Smarty_Template_Cached $cached = null, $update = false) + public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null, $update = false) { - /** @var Smarty_Internal_Template $_smarty_tpl - * used in included file - */ - $_smarty_tpl = $_template; - $_template->cached->valid = false; + $_smarty_tpl->cached->valid = false; if ($update && defined('HHVM_VERSION')) { - eval("?>" . file_get_contents($_template->cached->filepath)); + eval("?>" . file_get_contents($_smarty_tpl->cached->filepath)); return true; } else { - return @include $_template->cached->filepath; + return @include $_smarty_tpl->cached->filepath; } } @@ -193,7 +193,7 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource clearstatcache(); } if (is_file($cached->lock_id)) { - $t = @filemtime($cached->lock_id); + $t = filemtime($cached->lock_id); return $t && (time() - $t < $smarty->locking_timeout); } else { return false; diff --git a/libs/sysplugins/smarty_template_compiled.php b/libs/sysplugins/smarty_template_compiled.php index f1f3cb7c..5f702d6b 100644 --- a/libs/sysplugins/smarty_template_compiled.php +++ b/libs/sysplugins/smarty_template_compiled.php @@ -59,9 +59,8 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base $_filepath = substr($_filepath, 0, 2) . DS . substr($_filepath, 2, 2) . DS . substr($_filepath, 4, 2) . DS . $_filepath; } - $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^'; if (isset($_compile_id)) { - $_filepath = $_compile_id . $_compile_dir_sep . $_filepath; + $_filepath = $_compile_id . ($_template->smarty->use_sub_dirs ? DS : '^') . $_filepath; } // caching token if ($_template->caching) { @@ -69,8 +68,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base } else { $_cache = ''; } - $_compile_dir = $_template->smarty->getCompileDir(); - // set basename if not specified + // set basename $_basename = $_template->source->handler->getBasename($_template->source); if ($_basename === null) { $_basename = basename(preg_replace('![^\w]+!', '_', $_template->source->name)); @@ -80,7 +78,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base $_basename = '.' . $_basename; } - $this->filepath = $_compile_dir . $_filepath . '.' . $_template->source->type . $_basename . $_cache . '.php'; + $this->filepath = $_template->smarty->getCompileDir() . $_filepath . '.' . $_template->source->type . $_basename . $_cache . '.php'; $this->exists = is_file($this->filepath); if (!$this->exists) { $this->timestamp = false; @@ -90,21 +88,20 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base /** * load compiled template or compile from source * - * @param Smarty_Internal_Template $_template + * @param Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template * * @throws Exception */ - public function process(Smarty_Internal_Template $_template) + public function process(Smarty_Internal_Template $_smarty_tpl) { - if (!$_template->source->handler->uncompiled) { - $_smarty_tpl = $_template; - if ($_template->source->handler->recompiled || !$this->exists || $_template->smarty->force_compile || - ($_template->smarty->compile_check && $_template->source->getTimeStamp() > $this->getTimeStamp()) + 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()) ) { - $this->compileTemplateSource($_template); - $compileCheck = $_template->smarty->compile_check; - $_template->smarty->compile_check = false; - if ($_template->source->handler->recompiled) { + $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 { @@ -119,21 +116,21 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base ob_get_clean(); $this->content = null; } else { - $this->loadCompiledTemplate($_template); + $this->loadCompiledTemplate($_smarty_tpl); } - $_template->smarty->compile_check = $compileCheck; + $_smarty_tpl->smarty->compile_check = $compileCheck; } else { - $_template->mustCompile = true; + $_smarty_tpl->mustCompile = true; @include($this->filepath); - if ($_template->mustCompile) { - $this->compileTemplateSource($_template); - $compileCheck = $_template->smarty->compile_check; - $_template->smarty->compile_check = false; - $this->loadCompiledTemplate($_template); - $_template->smarty->compile_check = $compileCheck; + if ($_smarty_tpl->mustCompile) { + $this->compileTemplateSource($_smarty_tpl); + $compileCheck = $_smarty_tpl->smarty->compile_check; + $_smarty_tpl->smarty->compile_check = false; + $this->loadCompiledTemplate($_smarty_tpl); + $_smarty_tpl->smarty->compile_check = $compileCheck; } } - $_template->_subTemplateRegister(); + $_smarty_tpl->_subTemplateRegister(); $this->processed = true; } } @@ -142,16 +139,15 @@ 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 $_template + * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template */ - private function loadCompiledTemplate(Smarty_Internal_Template $_template) + private function loadCompiledTemplate(Smarty_Internal_Template $_smarty_tpl) { if (function_exists('opcache_invalidate')) { opcache_invalidate($this->filepath, true); } elseif (function_exists('apc_compile_file')) { apc_compile_file($this->filepath); } - $_smarty_tpl = $_template; if (defined('HHVM_VERSION')) { eval("?>" . file_get_contents($this->filepath)); } else { diff --git a/libs/sysplugins/smarty_template_resource_base.php b/libs/sysplugins/smarty_template_resource_base.php index 498cd34e..b9b7dee7 100644 --- a/libs/sysplugins/smarty_template_resource_base.php +++ b/libs/sysplugins/smarty_template_resource_base.php @@ -162,7 +162,7 @@ abstract class Smarty_Template_Resource_Base public function getTimeStamp() { if ($this->exists && !isset($this->timestamp)) { - $this->timestamp = @filemtime($this->filepath); + $this->timestamp = filemtime($this->filepath); } return $this->timestamp; }