- optimization of resource processing

This commit is contained in:
uwetews
2016-03-11 01:07:26 +01:00
parent 880967aff3
commit 62bf9eeddc
6 changed files with 48 additions and 60 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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;
}