- optimization move internal method decodeProperties back into template object

This commit is contained in:
uwetews
2015-12-23 00:44:51 +01:00
parent ef3da3cc0b
commit ad635471cb
7 changed files with 83 additions and 95 deletions

View File

@@ -1,4 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)
23.12.2015
- optimization move internal method decodeProperties back into template object
22.12.2015
- change $xxx_dir properties from private to protected in case Smarty class gets extended
- code optimizations

View File

@@ -118,7 +118,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.30-dev/5';
const SMARTY_VERSION = '3.1.30-dev/6';
/**
* define variable scopes

View File

@@ -14,16 +14,13 @@
* @property Smarty_Internal_Runtime_SubTemplate $_subTemplate
* @property Smarty_Internal_Runtime_TplFunction $_tplFunction
* @property Smarty_Internal_Runtime_Var $_var
* @property Smarty_Internal_Runtime_Config $_config
* @property Smarty_Internal_Runtime_Foreach $_foreach
* @property Smarty_Internal_Runtime_Hhvm $_hhvm
* @property Smarty_Internal_Runtime_WriteFile $_writeFile
* @property Smarty_Internal_Runtime_ValidateCompiled $_validateCompiled
* @property Smarty_Internal_Runtime_CodeFrame $_codeFrame
* @property Smarty_Internal_Runtime_FilterHandler $_filterHandler
* @property Smarty_Internal_Runtime_GetIncludePath $_getIncludePath
* @property Smarty_Internal_Runtime_UpdateScope $_updateScope
* @property Smarty_Internal_Runtime_IsCached $_isCached
* @property Smarty_Internal_Runtime_CacheModify $_cacheModify
* @property Smarty_Internal_Runtime_UpdateCache $_updateCache
* @property Smarty_Internal_Method_GetTemplateVars $getTemplateVars

View File

@@ -49,8 +49,8 @@ class Smarty_Internal_Runtime_CodeFrame
$output .= "/* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
"\n from \"" . $_template->source->filepath . "\" */\n\n";
$dec = "\$_smarty_tpl->smarty->ext->_validateCompiled->decodeProperties(\$_smarty_tpl, " .
var_export($properties, true) . ',' . ($cache ? 'true' : 'false') . ")";
$dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' .
($cache ? 'true' : 'false') . ")";
$output .= "if ({$dec}) {\n";
$output .= "function {$properties['unifunc']} (\$_smarty_tpl) {\n";
// include code for plugins

View File

@@ -1,86 +0,0 @@
<?php
/**
* Runtime Methods decodeProperties
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*
**/
class Smarty_Internal_Runtime_ValidateCompiled
{
/**
* This function is executed automatically when a compiled or cached template file is included
* - Decode saved properties from compiled template and cache files
* - Check if compiled or cache file is valid
*
* @param array $properties special template properties
* @param bool $cache flag if called from cache file
*
* @return bool flag if compiled or cache file is valid
*/
public function decodeProperties(Smarty_Internal_Template $tpl, $properties, $cache = false)
{
$is_valid = true;
if (Smarty::SMARTY_VERSION != $properties['version']) {
// new version must rebuild
$is_valid = false;
} elseif ($is_valid && !empty($properties['file_dependency']) &&
((!$cache && $tpl->smarty->compile_check) || $tpl->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] == 'extends' || $_file_to_check[2] == 'php') {
if ($tpl->source->filepath == $_file_to_check[0]) {
// do not recheck current template
continue;
//$mtime = $tpl->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;
}
} elseif ($_file_to_check[2] == 'string') {
continue;
} else {
$handler = Smarty_Resource::load($tpl->smarty, $_file_to_check[2]);
if ($handler->checkTimestamps()) {
$source = Smarty_Template_Source::load($tpl, $tpl->smarty, $_file_to_check[ 0 ]);
$mtime = $source->getTimeStamp();
} else {
continue;
}
}
if (!$mtime || $mtime > $_file_to_check[1]) {
$is_valid = false;
break;
}
}
}
if ($cache) {
// CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
if ($tpl->caching === Smarty::CACHING_LIFETIME_SAVED && $properties['cache_lifetime'] >= 0 &&
(time() > ($tpl->cached->timestamp + $properties['cache_lifetime']))
) {
$is_valid = false;
}
$tpl->cached->cache_lifetime = $properties['cache_lifetime'];
$tpl->cached->valid = $is_valid;
$resource = $tpl->cached;
} else {
$tpl->mustCompile = !$is_valid;
$resource = $tpl->compiled;
$resource->includes = isset($properties['includes']) ? $properties['includes'] : array();
}
if ($is_valid) {
$resource->unifunc = $properties['unifunc'];
$resource->has_nocache_code = $properties['has_nocache_code'];
// $tpl->compiled->nocache_hash = $properties['nocache_hash'];
$resource->file_dependency = $properties['file_dependency'];
if (isset($properties['tpl_function'])) {
$tpl->tpl_function = $properties['tpl_function'];
}
}
return $is_valid && !function_exists($properties['unifunc']);
}
}

View File

@@ -211,6 +211,81 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
}
}
/**
* This function is executed automatically when a compiled or cached template file is included
* - Decode saved properties from compiled template and cache files
* - Check if compiled or cache file is valid
*
* @param array $properties special template properties
* @param bool $cache flag if called from cache file
*
* @return bool flag if compiled or cache file is valid
*/
public function _decodeProperties(Smarty_Internal_Template $tpl, $properties, $cache = false)
{
$is_valid = true;
if (Smarty::SMARTY_VERSION != $properties['version']) {
// new version must rebuild
$is_valid = false;
} elseif ($is_valid && !empty($properties['file_dependency']) &&
((!$cache && $tpl->smarty->compile_check) || $tpl->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] == 'extends' || $_file_to_check[2] == 'php') {
if ($tpl->source->filepath == $_file_to_check[0]) {
// do not recheck current template
continue;
//$mtime = $tpl->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;
}
} elseif ($_file_to_check[2] == 'string') {
continue;
} else {
$handler = Smarty_Resource::load($tpl->smarty, $_file_to_check[2]);
if ($handler->checkTimestamps()) {
$source = Smarty_Template_Source::load($tpl, $tpl->smarty, $_file_to_check[ 0 ]);
$mtime = $source->getTimeStamp();
} else {
continue;
}
}
if (!$mtime || $mtime > $_file_to_check[1]) {
$is_valid = false;
break;
}
}
}
if ($cache) {
// CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
if ($tpl->caching === Smarty::CACHING_LIFETIME_SAVED && $properties['cache_lifetime'] >= 0 &&
(time() > ($tpl->cached->timestamp + $properties['cache_lifetime']))
) {
$is_valid = false;
}
$tpl->cached->cache_lifetime = $properties['cache_lifetime'];
$tpl->cached->valid = $is_valid;
$resource = $tpl->cached;
} else {
$tpl->mustCompile = !$is_valid;
$resource = $tpl->compiled;
$resource->includes = isset($properties['includes']) ? $properties['includes'] : array();
}
if ($is_valid) {
$resource->unifunc = $properties['unifunc'];
$resource->has_nocache_code = $properties['has_nocache_code'];
// $tpl->compiled->nocache_hash = $properties['nocache_hash'];
$resource->file_dependency = $properties['file_dependency'];
if (isset($properties['tpl_function'])) {
$tpl->tpl_function = $properties['tpl_function'];
}
}
return $is_valid && !function_exists($properties['unifunc']);
}
/**
* Compiles the template
* If the template is not evaluated the compiled template is saved on disk

View File

@@ -480,7 +480,6 @@ class Smarty_Internal_TestInstall
'smarty_internal_runtime_tplfunction.php' => true,
'smarty_internal_runtime_updatecache.php' => true,
'smarty_internal_runtime_updatescope.php' => true,
'smarty_internal_runtime_validatecompiled.php' => true,
'smarty_internal_runtime_var.php' => true,
'smarty_internal_runtime_writefile.php' => true,
'smarty_internal_smartytemplatecompiler.php' => true,