- 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 * smarty version
*/ */
const SMARTY_VERSION = '3.1.30-dev/54'; const SMARTY_VERSION = '3.1.30-dev/55';
/** /**
* define variable scopes * 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 * 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 Smarty_Template_Cached $cached cached object
* @param bool $update flag if called because cache update * @param bool $update flag if called because cache update
* *
* @return boolean true or false if the cached content does not exist * @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) { if (!$cached) {
$cached = $_template->cached; $cached = $_smarty_tpl->cached;
} }
$content = $cached->content ? $cached->content : null; $content = $cached->content ? $cached->content : null;
$timestamp = $cached->timestamp ? $cached->timestamp : null; $timestamp = $cached->timestamp ? $cached->timestamp : null;
if ($content === null || !$timestamp) { if ($content === null || !$timestamp) {
$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $this->fetch($_smarty_tpl->cached->filepath, $_smarty_tpl->source->name, $_smarty_tpl->cache_id,
$_template->compile_id, $content, $timestamp); $_smarty_tpl->compile_id, $content, $timestamp);
} }
if (isset($content)) { if (isset($content)) {
/** @var Smarty_Internal_Template $_smarty_tpl
* used in evaluated code
*/
$_smarty_tpl = $_template;
eval("?>" . $content); eval("?>" . $content);
$cached->content = null; $cached->content = null;
return true; return true;

View File

@@ -82,31 +82,27 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
/** /**
* Read the cached template and process the header * 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 Smarty_Template_Cached $cached cached object
* @param bool $update flag if called because cache update * @param bool $update flag if called because cache update
* *
* @return boolean true or false if the cached content does not exist * @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) { if (!$cached) {
$cached = $_template->cached; $cached = $_smarty_tpl->cached;
} }
$content = $cached->content ? $cached->content : null; $content = $cached->content ? $cached->content : null;
$timestamp = $cached->timestamp ? $cached->timestamp : null; $timestamp = $cached->timestamp ? $cached->timestamp : null;
if ($content === null || !$timestamp) { if ($content === null || !$timestamp) {
if (!$this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, if (!$this->fetch($_smarty_tpl->cached->filepath, $_smarty_tpl->source->name, $_smarty_tpl->cache_id,
$_template->compile_id, $content, $timestamp, $_template->source->uid) $_smarty_tpl->compile_id, $content, $timestamp, $_smarty_tpl->source->uid)
) { ) {
return false; return false;
} }
} }
if (isset($content)) { if (isset($content)) {
/** @var Smarty_Internal_Template $_smarty_tpl
* used in evaluated code
*/
$_smarty_tpl = $_template;
eval("?>" . $content); eval("?>" . $content);
return true; 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) 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; $_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; $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w]+!', '_', $_template->compile_id) : null;
$_filepath = sha1($_template->source->uid . $_template->smarty->_joined_template_dir); $_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'; $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 = $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); $cached->timestamp = $cached->exists = is_file($cached->filepath);
if ($cached->exists) { if ($cached->exists) {
$cached->timestamp = filemtime($cached->filepath); $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 * 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 Smarty_Template_Cached $cached cached object
* @param bool $update flag if called because cache update * @param bool $update flag if called because cache update
* *
* @return boolean true or false if the cached content does not exist * @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 $_smarty_tpl->cached->valid = false;
* used in included file
*/
$_smarty_tpl = $_template;
$_template->cached->valid = false;
if ($update && defined('HHVM_VERSION')) { if ($update && defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($_template->cached->filepath)); eval("?>" . file_get_contents($_smarty_tpl->cached->filepath));
return true; return true;
} else { } 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(); clearstatcache();
} }
if (is_file($cached->lock_id)) { if (is_file($cached->lock_id)) {
$t = @filemtime($cached->lock_id); $t = filemtime($cached->lock_id);
return $t && (time() - $t < $smarty->locking_timeout); return $t && (time() - $t < $smarty->locking_timeout);
} else { } else {
return false; 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 = substr($_filepath, 0, 2) . DS . substr($_filepath, 2, 2) . DS . substr($_filepath, 4, 2) . DS .
$_filepath; $_filepath;
} }
$_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
if (isset($_compile_id)) { if (isset($_compile_id)) {
$_filepath = $_compile_id . $_compile_dir_sep . $_filepath; $_filepath = $_compile_id . ($_template->smarty->use_sub_dirs ? DS : '^') . $_filepath;
} }
// caching token // caching token
if ($_template->caching) { if ($_template->caching) {
@@ -69,8 +68,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
} else { } else {
$_cache = ''; $_cache = '';
} }
$_compile_dir = $_template->smarty->getCompileDir(); // set basename
// set basename if not specified
$_basename = $_template->source->handler->getBasename($_template->source); $_basename = $_template->source->handler->getBasename($_template->source);
if ($_basename === null) { if ($_basename === null) {
$_basename = basename(preg_replace('![^\w]+!', '_', $_template->source->name)); $_basename = basename(preg_replace('![^\w]+!', '_', $_template->source->name));
@@ -80,7 +78,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$_basename = '.' . $_basename; $_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); $this->exists = is_file($this->filepath);
if (!$this->exists) { if (!$this->exists) {
$this->timestamp = false; $this->timestamp = false;
@@ -90,21 +88,20 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
/** /**
* load compiled template or compile from source * 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 * @throws Exception
*/ */
public function process(Smarty_Internal_Template $_template) public function process(Smarty_Internal_Template $_smarty_tpl)
{ {
if (!$_template->source->handler->uncompiled) { if (!$_smarty_tpl->source->handler->uncompiled) {
$_smarty_tpl = $_template; if ($_smarty_tpl->source->handler->recompiled || !$this->exists || $_smarty_tpl->smarty->force_compile ||
if ($_template->source->handler->recompiled || !$this->exists || $_template->smarty->force_compile || ($_smarty_tpl->smarty->compile_check && $_smarty_tpl->source->getTimeStamp() > $this->getTimeStamp())
($_template->smarty->compile_check && $_template->source->getTimeStamp() > $this->getTimeStamp())
) { ) {
$this->compileTemplateSource($_template); $this->compileTemplateSource($_smarty_tpl);
$compileCheck = $_template->smarty->compile_check; $compileCheck = $_smarty_tpl->smarty->compile_check;
$_template->smarty->compile_check = false; $_smarty_tpl->smarty->compile_check = false;
if ($_template->source->handler->recompiled) { if ($_smarty_tpl->source->handler->recompiled) {
$level = ob_get_level(); $level = ob_get_level();
ob_start(); ob_start();
try { try {
@@ -119,21 +116,21 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
ob_get_clean(); ob_get_clean();
$this->content = null; $this->content = null;
} else { } else {
$this->loadCompiledTemplate($_template); $this->loadCompiledTemplate($_smarty_tpl);
} }
$_template->smarty->compile_check = $compileCheck; $_smarty_tpl->smarty->compile_check = $compileCheck;
} else { } else {
$_template->mustCompile = true; $_smarty_tpl->mustCompile = true;
@include($this->filepath); @include($this->filepath);
if ($_template->mustCompile) { if ($_smarty_tpl->mustCompile) {
$this->compileTemplateSource($_template); $this->compileTemplateSource($_smarty_tpl);
$compileCheck = $_template->smarty->compile_check; $compileCheck = $_smarty_tpl->smarty->compile_check;
$_template->smarty->compile_check = false; $_smarty_tpl->smarty->compile_check = false;
$this->loadCompiledTemplate($_template); $this->loadCompiledTemplate($_smarty_tpl);
$_template->smarty->compile_check = $compileCheck; $_smarty_tpl->smarty->compile_check = $compileCheck;
} }
} }
$_template->_subTemplateRegister(); $_smarty_tpl->_subTemplateRegister();
$this->processed = true; $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 * Load fresh compiled template by including the PHP file
* HHVM requires a work around because of a PHP incompatibility * 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')) { if (function_exists('opcache_invalidate')) {
opcache_invalidate($this->filepath, true); opcache_invalidate($this->filepath, true);
} elseif (function_exists('apc_compile_file')) { } elseif (function_exists('apc_compile_file')) {
apc_compile_file($this->filepath); apc_compile_file($this->filepath);
} }
$_smarty_tpl = $_template;
if (defined('HHVM_VERSION')) { if (defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($this->filepath)); eval("?>" . file_get_contents($this->filepath));
} else { } else {

View File

@@ -162,7 +162,7 @@ abstract class Smarty_Template_Resource_Base
public function getTimeStamp() public function getTimeStamp()
{ {
if ($this->exists && !isset($this->timestamp)) { if ($this->exists && !isset($this->timestamp)) {
$this->timestamp = @filemtime($this->filepath); $this->timestamp = filemtime($this->filepath);
} }
return $this->timestamp; return $this->timestamp;
} }