From a9f0b8ad1f19bb9dc188356136421c023d38f8a7 Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Wed, 1 Jul 2015 03:27:06 +0200 Subject: [PATCH] - optimize compile check handling --- change_log.txt | 3 ++ .../smarty_internal_config_file_compiler.php | 2 +- .../smarty_internal_extension_config.php | 2 +- .../smarty_internal_resource_extends.php | 2 +- .../smarty_internal_resource_file.php | 15 ++++---- libs/sysplugins/smarty_internal_template.php | 17 ++++----- libs/sysplugins/smarty_template_cached.php | 2 +- libs/sysplugins/smarty_template_compiled.php | 23 ++++++++---- libs/sysplugins/smarty_template_source.php | 35 ++++++++++++++----- 9 files changed, 68 insertions(+), 33 deletions(-) diff --git a/change_log.txt b/change_log.txt index 8991516d..f530dc97 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@  ===== 3.1.28-dev===== (xx.xx.2015) + 01.07.2015 + - optimize compile check handling + 28.06.2015 - move $smarty->enableSecurity() into Smarty_Security class - optimize security isTrustedResourceDir() diff --git a/libs/sysplugins/smarty_internal_config_file_compiler.php b/libs/sysplugins/smarty_internal_config_file_compiler.php index 50779c7b..397cac55 100644 --- a/libs/sysplugins/smarty_internal_config_file_compiler.php +++ b/libs/sysplugins/smarty_internal_config_file_compiler.php @@ -100,7 +100,7 @@ class Smarty_Internal_Config_File_Compiler public function compileTemplate(Smarty_Internal_Template $template) { $this->template = $template; - $this->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->name, $this->template->source->timestamp, $this->template->source->type); + $this->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->name, $this->template->source->getTimeStamp(), $this->template->source->type); // on empty config just return if ($template->source->content == '') { return true; diff --git a/libs/sysplugins/smarty_internal_extension_config.php b/libs/sysplugins/smarty_internal_extension_config.php index a30bb22d..55663be8 100644 --- a/libs/sysplugins/smarty_internal_extension_config.php +++ b/libs/sysplugins/smarty_internal_extension_config.php @@ -29,7 +29,7 @@ class Smarty_Internal_Extension_Config Smarty_Internal_Debug::end_render($confObj); } if ($obj instanceof Smarty_Internal_Template) { - $obj->properties['file_dependency'][$confObj->source->uid] = array($confObj->source->filepath, $confObj->source->timestamp, $confObj->source->type); + $obj->properties['file_dependency'][$confObj->source->uid] = array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type); } } diff --git a/libs/sysplugins/smarty_internal_resource_extends.php b/libs/sysplugins/smarty_internal_resource_extends.php index c6bdc202..4b3181a1 100644 --- a/libs/sysplugins/smarty_internal_resource_extends.php +++ b/libs/sysplugins/smarty_internal_resource_extends.php @@ -52,9 +52,9 @@ class Smarty_Internal_Resource_Extends extends Smarty_Resource $source->components = $sources; $source->filepath = $s->filepath; $source->uid = sha1($uid); + $source->exists = $exists; if ($_template && $_template->smarty->compile_check) { $source->timestamp = $s->timestamp; - $source->exists = $exists; } // need the template at getContent() $source->template = $_template; diff --git a/libs/sysplugins/smarty_internal_resource_file.php b/libs/sysplugins/smarty_internal_resource_file.php index 6d7abb3e..e259a79d 100644 --- a/libs/sysplugins/smarty_internal_resource_file.php +++ b/libs/sysplugins/smarty_internal_resource_file.php @@ -106,8 +106,9 @@ class Smarty_Internal_Resource_File extends Smarty_Resource */ protected function fileExists(Smarty_Template_Source $source, $file) { - $source->timestamp = is_file($file) ? @filemtime($file) : false; - return $source->exists = !!$source->timestamp; + $source->timestamp = $source->exists = is_file($file); + $source->timestamp = $source->exists ? filemtime($file) : false; + return $source->exists; } /** @@ -126,8 +127,8 @@ class Smarty_Internal_Resource_File extends Smarty_Resource } $source->exists = true; $source->uid = sha1($source->filepath); - if ($source->smarty->compile_check && !isset($source->timestamp)) { - $source->timestamp = @filemtime($source->filepath); + if ($source->smarty->compile_check == 1) { + $source->timestamp = filemtime($source->filepath); } } else { $source->timestamp = false; @@ -142,9 +143,11 @@ class Smarty_Internal_Resource_File extends Smarty_Resource */ public function populateTimestamp(Smarty_Template_Source $source) { - $source->timestamp = $source->exists = is_file($source->filepath); + if (!$source->exists) { + $source->timestamp = $source->exists = is_file($source->filepath); + } if ($source->exists) { - $source->timestamp = @filemtime($source->filepath); + $source->timestamp = filemtime($source->filepath); } } diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 08471c16..11666468 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -248,7 +248,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase $content = $this->source->renderUncompiled($this); } if (!$this->source->recompiled && empty($this->properties['file_dependency'][$this->source->uid])) { - $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $this->source->type); + $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->getTimeStamp(), $this->source->type); } if ($parentIsTpl) { $this->parent->properties['file_dependency'] = array_merge($this->parent->properties['file_dependency'], $this->properties['file_dependency']); @@ -406,8 +406,8 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}"); } if ($this->mustCompile === null) { - $this->mustCompile = (!$this->source->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || $this->compiled->timestamp === false || - ($this->smarty->compile_check && $this->compiled->timestamp < $this->source->timestamp))); + $this->mustCompile = (!$this->source->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || !$this->compiled->exists || + ($this->smarty->compile_check && $this->compiled->getTimeStamp() < $this->source->getTimeStamp()))); } return $this->mustCompile; @@ -598,22 +598,23 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase if (Smarty::SMARTY_VERSION != $properties['version']) { // new version must rebuild $is_valid = false; - } elseif ((!$cache && $this->smarty->compile_check || $cache && ($this->smarty->compile_check === true || $this->smarty->compile_check === Smarty::COMPILECHECK_ON)) && !empty($properties['file_dependency'])) { + } elseif (!empty($properties['file_dependency']) && ((!$cache && $this->smarty->compile_check) || $this->smarty->compile_check == 1)) { // check file dependencies at compiled code foreach ($properties['file_dependency'] as $_file_to_check) { if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'php') { - if ($this->source->filepath == $_file_to_check[0] && isset($this->source->timestamp)) { + if ($this->source->filepath == $_file_to_check[0]) { // do not recheck current template - $mtime = $this->source->timestamp; + continue; + //$mtime = $this->source->getTimeStamp(); } else { // 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; + $mtime = is_file($_file_to_check[0]) ? filemtime($_file_to_check[0]) : false; } } elseif ($_file_to_check[2] == 'string') { continue; } else { $source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]); - $mtime = $source->timestamp; + $mtime = $source->getTimeStamp(); } if (!$mtime || $mtime > $_file_to_check[1]) { $is_valid = false; diff --git a/libs/sysplugins/smarty_template_cached.php b/libs/sysplugins/smarty_template_cached.php index cafe1eeb..2a467111 100644 --- a/libs/sysplugins/smarty_template_cached.php +++ b/libs/sysplugins/smarty_template_cached.php @@ -158,7 +158,7 @@ class Smarty_Template_Cached // lifetime expired $this->valid = false; } - if ($this->valid && $_template->smarty->compile_check && $_template->source->timestamp > $this->timestamp) { + if ($this->valid && $_template->smarty->compile_check == 1 && $_template->source->getTimeStamp() > $this->timestamp) { $this->valid = false; } if ($this->valid || !$_template->smarty->cache_locking) { diff --git a/libs/sysplugins/smarty_template_compiled.php b/libs/sysplugins/smarty_template_compiled.php index 1c13c6b7..29dd6121 100644 --- a/libs/sysplugins/smarty_template_compiled.php +++ b/libs/sysplugins/smarty_template_compiled.php @@ -134,9 +134,9 @@ class Smarty_Template_Compiled } $this->filepath = $_compile_dir . $_filepath . '.' . $_template->source->type . $_basename . $_cache . '.php'; - $this->timestamp = $this->exists = is_file($this->filepath); - if ($this->exists) { - $this->timestamp = @filemtime($this->filepath); + $this->exists = is_file($this->filepath); + if (!$this->exists) { + $this->timestamp = false; } } @@ -150,7 +150,7 @@ class Smarty_Template_Compiled public function process(Smarty_Internal_Template $_template) { $_smarty_tpl = $_template; - if ($_template->source->recompiled || !$_template->compiled->exists || $_template->smarty->force_compile || ($_template->smarty->compile_check && $_template->source->timestamp > $_template->compiled->timestamp)) { + if ($_template->source->recompiled || !$_template->compiled->exists || $_template->smarty->force_compile || ($_template->smarty->compile_check && $_template->source->getTimeStamp() > $_template->compiled->getTimeStamp())) { $this->compileTemplateSource($_template); $compileCheck = $_template->smarty->compile_check; $_template->smarty->compile_check = false; @@ -219,7 +219,7 @@ class Smarty_Template_Compiled } // compile locking if (!$_template->source->recompiled) { - if ($saved_timestamp = $_template->compiled->timestamp) { + if ($saved_timestamp = $_template->compiled->getTimeStamp()) { touch($_template->compiled->filepath); } } @@ -260,7 +260,7 @@ class Smarty_Template_Compiled if ($obj->writeFile($this->filepath, $code, $_template->smarty) === true) { $this->timestamp = $this->exists = is_file($this->filepath); if ($this->exists) { - $this->timestamp = @filemtime($this->filepath); + $this->timestamp = filemtime($this->filepath); return true; } } @@ -287,4 +287,15 @@ class Smarty_Template_Compiled } return isset($this->content) ? $this->content : false; } + /** + * Get compiled time stamp + * + * @return int + */ + public function getTimeStamp() { + if ($this->exists && !isset($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 dd537f0e..f6ae8842 100644 --- a/libs/sysplugins/smarty_template_source.php +++ b/libs/sysplugins/smarty_template_source.php @@ -76,6 +76,19 @@ class Smarty_Template_Source * @var string */ public $filepath = null; + /** + * Source Timestamp + * + * @var integer + */ + public $timestamp = null; + + /** + * Source Existence + * + * @var boolean + */ + public $exists = false; /** * Source File Base name * @@ -233,6 +246,18 @@ class Smarty_Template_Source } } + /** + * Get source time stamp + * + * @return int + */ + public function getTimeStamp() { + if (!isset($this->timestamp)) { + $this->handler->populateTimestamp($this); + } + return $this->timestamp; + } + /** * <> Generic Setter. * @@ -245,9 +270,7 @@ class Smarty_Template_Source { switch ($property_name) { // regular attributes - case 'timestamp': - case 'exists': - case 'content': + case 'content': // required for extends: only case 'template': $this->$property_name = $value; @@ -269,12 +292,6 @@ class Smarty_Template_Source public function __get($property_name) { switch ($property_name) { - case 'timestamp': - case 'exists': - $this->handler->populateTimestamp($this); - - return $this->$property_name; - case 'content': return $this->content = $this->handler->getContent($this);