- 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)  ===== 3.1.28-dev===== (xx.xx.2015)
01.07.2015
- optimize compile check handling
28.06.2015 28.06.2015
- move $smarty->enableSecurity() into Smarty_Security class - move $smarty->enableSecurity() into Smarty_Security class
- optimize security isTrustedResourceDir() - optimize security isTrustedResourceDir()

View File

@@ -100,7 +100,7 @@ class Smarty_Internal_Config_File_Compiler
public function compileTemplate(Smarty_Internal_Template $template) public function compileTemplate(Smarty_Internal_Template $template)
{ {
$this->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 // on empty config just return
if ($template->source->content == '') { if ($template->source->content == '') {
return true; return true;

View File

@@ -29,7 +29,7 @@ class Smarty_Internal_Extension_Config
Smarty_Internal_Debug::end_render($confObj); Smarty_Internal_Debug::end_render($confObj);
} }
if ($obj instanceof Smarty_Internal_Template) { 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->components = $sources;
$source->filepath = $s->filepath; $source->filepath = $s->filepath;
$source->uid = sha1($uid); $source->uid = sha1($uid);
$source->exists = $exists;
if ($_template && $_template->smarty->compile_check) { if ($_template && $_template->smarty->compile_check) {
$source->timestamp = $s->timestamp; $source->timestamp = $s->timestamp;
$source->exists = $exists;
} }
// need the template at getContent() // need the template at getContent()
$source->template = $_template; $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) protected function fileExists(Smarty_Template_Source $source, $file)
{ {
$source->timestamp = is_file($file) ? @filemtime($file) : false; $source->timestamp = $source->exists = is_file($file);
return $source->exists = !!$source->timestamp; $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->exists = true;
$source->uid = sha1($source->filepath); $source->uid = sha1($source->filepath);
if ($source->smarty->compile_check && !isset($source->timestamp)) { if ($source->smarty->compile_check == 1) {
$source->timestamp = @filemtime($source->filepath); $source->timestamp = filemtime($source->filepath);
} }
} else { } else {
$source->timestamp = false; $source->timestamp = false;
@@ -142,9 +143,11 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
*/ */
public function populateTimestamp(Smarty_Template_Source $source) 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) { 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); $content = $this->source->renderUncompiled($this);
} }
if (!$this->source->recompiled && empty($this->properties['file_dependency'][$this->source->uid])) { 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) { if ($parentIsTpl) {
$this->parent->properties['file_dependency'] = array_merge($this->parent->properties['file_dependency'], $this->properties['file_dependency']); $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}"); throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
} }
if ($this->mustCompile === null) { if ($this->mustCompile === null) {
$this->mustCompile = (!$this->source->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || $this->compiled->timestamp === false || $this->mustCompile = (!$this->source->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || !$this->compiled->exists ||
($this->smarty->compile_check && $this->compiled->timestamp < $this->source->timestamp))); ($this->smarty->compile_check && $this->compiled->getTimeStamp() < $this->source->getTimeStamp())));
} }
return $this->mustCompile; return $this->mustCompile;
@@ -598,22 +598,23 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
if (Smarty::SMARTY_VERSION != $properties['version']) { if (Smarty::SMARTY_VERSION != $properties['version']) {
// new version must rebuild // new version must rebuild
$is_valid = false; $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 // check file dependencies at compiled code
foreach ($properties['file_dependency'] as $_file_to_check) { foreach ($properties['file_dependency'] as $_file_to_check) {
if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'php') { 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 // do not recheck current template
$mtime = $this->source->timestamp; continue;
//$mtime = $this->source->getTimeStamp();
} else { } else {
// file and php types can be checked without loading the respective resource handlers // 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') { } elseif ($_file_to_check[2] == 'string') {
continue; continue;
} else { } else {
$source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]); $source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]);
$mtime = $source->timestamp; $mtime = $source->getTimeStamp();
} }
if (!$mtime || $mtime > $_file_to_check[1]) { if (!$mtime || $mtime > $_file_to_check[1]) {
$is_valid = false; $is_valid = false;

View File

@@ -158,7 +158,7 @@ class Smarty_Template_Cached
// lifetime expired // lifetime expired
$this->valid = false; $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; $this->valid = false;
} }
if ($this->valid || !$_template->smarty->cache_locking) { 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->filepath = $_compile_dir . $_filepath . '.' . $_template->source->type . $_basename . $_cache . '.php';
$this->timestamp = $this->exists = is_file($this->filepath); $this->exists = is_file($this->filepath);
if ($this->exists) { if (!$this->exists) {
$this->timestamp = @filemtime($this->filepath); $this->timestamp = false;
} }
} }
@@ -150,7 +150,7 @@ class Smarty_Template_Compiled
public function process(Smarty_Internal_Template $_template) public function process(Smarty_Internal_Template $_template)
{ {
$_smarty_tpl = $_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); $this->compileTemplateSource($_template);
$compileCheck = $_template->smarty->compile_check; $compileCheck = $_template->smarty->compile_check;
$_template->smarty->compile_check = false; $_template->smarty->compile_check = false;
@@ -219,7 +219,7 @@ class Smarty_Template_Compiled
} }
// compile locking // compile locking
if (!$_template->source->recompiled) { if (!$_template->source->recompiled) {
if ($saved_timestamp = $_template->compiled->timestamp) { if ($saved_timestamp = $_template->compiled->getTimeStamp()) {
touch($_template->compiled->filepath); touch($_template->compiled->filepath);
} }
} }
@@ -260,7 +260,7 @@ class Smarty_Template_Compiled
if ($obj->writeFile($this->filepath, $code, $_template->smarty) === true) { if ($obj->writeFile($this->filepath, $code, $_template->smarty) === true) {
$this->timestamp = $this->exists = is_file($this->filepath); $this->timestamp = $this->exists = is_file($this->filepath);
if ($this->exists) { if ($this->exists) {
$this->timestamp = @filemtime($this->filepath); $this->timestamp = filemtime($this->filepath);
return true; return true;
} }
} }
@@ -287,4 +287,15 @@ class Smarty_Template_Compiled
} }
return isset($this->content) ? $this->content : false; 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 * @var string
*/ */
public $filepath = null; public $filepath = null;
/**
* Source Timestamp
*
* @var integer
*/
public $timestamp = null;
/**
* Source Existence
*
* @var boolean
*/
public $exists = false;
/** /**
* Source File Base name * 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. * <<magic>> Generic Setter.
* *
@@ -245,9 +270,7 @@ class Smarty_Template_Source
{ {
switch ($property_name) { switch ($property_name) {
// regular attributes // regular attributes
case 'timestamp': case 'content':
case 'exists':
case 'content':
// required for extends: only // required for extends: only
case 'template': case 'template':
$this->$property_name = $value; $this->$property_name = $value;
@@ -269,12 +292,6 @@ class Smarty_Template_Source
public function __get($property_name) public function __get($property_name)
{ {
switch ($property_name) { switch ($property_name) {
case 'timestamp':
case 'exists':
$this->handler->populateTimestamp($this);
return $this->$property_name;
case 'content': case 'content':
return $this->content = $this->handler->getContent($this); return $this->content = $this->handler->getContent($this);