- optimize compile check handling

This commit is contained in:
Uwe Tews
2015-07-01 03:27:06 +02:00
parent 86783c857d
commit a9f0b8ad1f
9 changed files with 68 additions and 33 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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;
}
/**
* <<magic>> 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);