- bugfix sha1() calculations at extends resource and some general improvments on sha1() handling

This commit is contained in:
Uwe.Tews
2010-01-05 21:10:25 +00:00
parent a6c2f5f8d4
commit d287c6b1d9
8 changed files with 122 additions and 117 deletions

View File

@@ -1,3 +1,7 @@
01/05/2009
- bugfix sha1() calculations at extends resource and some general improvments on sha1() handling
01/03/2009 01/03/2009
- internal change on building cache files - internal change on building cache files

View File

@@ -22,51 +22,77 @@ class Smarty_Internal_CacheResource_File {
/** /**
* Returns the filepath of the cached template output * Returns the filepath of the cached template output
* *
* @param object $template current template * @param object $_template current template
* @return string the cache filepath * @return string the cache filepath
*/ */
public function getCachedFilepath($template) public function getCachedFilepath($_template)
{ {
return $this->buildCachedFilepath ($template->getTemplateFilepath(), $template->cache_id, $template->compile_id); $_source_file_path = str_replace(':', '.', $_template->getTemplateFilepath());
$_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 = $_template->templateUid;
// if use_sub_dirs, break file into directories
if ($this->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS
. substr($_filepath, 2, 2) . DS
. substr($_filepath, 4, 2) . DS
. $_filepath;
}
$_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
if (isset($_cache_id)) {
$_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
} else {
$_cache_id = '';
}
if (isset($_compile_id)) {
$_compile_id = $_compile_id . $_compile_dir_sep;
} else {
$_compile_id = '';
}
$_cache_dir = $this->smarty->cache_dir;
if (strpos('/\\', substr($_cache_dir, -1)) === false) {
$_cache_dir .= DS;
}
return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
} }
/** /**
* Returns the timpestamp of the cached template output * Returns the timpestamp of the cached template output
* *
* @param object $template current template * @param object $_template current template
* @return integer |booelan the template timestamp or false if the file does not exist * @return integer |booelan the template timestamp or false if the file does not exist
*/ */
public function getCachedTimestamp($template) public function getCachedTimestamp($_template)
{ {
// return @filemtime ($template->getCachedFilepath()); // return @filemtime ($_template->getCachedFilepath());
return ($template->getCachedFilepath() && file_exists($template->getCachedFilepath())) ? filemtime($template->getCachedFilepath()) : false ; return ($_template->getCachedFilepath() && file_exists($_template->getCachedFilepath())) ? filemtime($_template->getCachedFilepath()) : false ;
} }
/** /**
* Returns the cached template output * Returns the cached template output
* *
* @param object $template current template * @param object $_template current template
* @return string |booelan the template content or false if the file does not exist * @return string |booelan the template content or false if the file does not exist
*/ */
public function getCachedContents($template) public function getCachedContents($_template)
{ {
ob_start(); ob_start();
$_smarty_tpl = $template; $_smarty_tpl = $_template;
include $template->getCachedFilepath(); include $_template->getCachedFilepath();
return ob_get_clean(); return ob_get_clean();
} }
/** /**
* Writes the rendered template output to cache file * Writes the rendered template output to cache file
* *
* @param object $template current template * @param object $_template current template
* @return boolean status * @return boolean status
*/ */
public function writeCachedContent($template, $content) public function writeCachedContent($_template, $content)
{ {
if (!$template->resource_object->isEvaluated) { if (!$_template->resource_object->isEvaluated) {
if (Smarty_Internal_Write_File::writeFile($template->getCachedFilepath(), $content, $this->smarty) === true) { if (Smarty_Internal_Write_File::writeFile($_template->getCachedFilepath(), $content, $this->smarty) === true) {
$template->cached_timestamp = filemtime($template->getCachedFilepath()); $_template->cached_timestamp = filemtime($_template->getCachedFilepath());
return true; return true;
} }
} }
@@ -153,44 +179,6 @@ class Smarty_Internal_CacheResource_File {
} }
return $_count; return $_count;
} }
/**
* Get system filepath to cached file
*
* @param string $source_file_path template source file path
* @param string $cache_id cache id
* @param string $compile_id compile id
* @return string filepath of cache file
*/
private function buildCachedFilepath ($source_file_path, $cache_id, $compile_id)
{
$_source_file_path = str_replace(':', '.', $source_file_path);
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
$_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
$_filepath = sha1($_source_file_path);
// if use_sub_dirs, break file into directories
if ($this->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS
. substr($_filepath, 2, 2) . DS
. substr($_filepath, 4, 2) . DS
. $_filepath;
}
$_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
if (isset($_cache_id)) {
$_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
} else {
$_cache_id = '';
}
if (isset($_compile_id)) {
$_compile_id = $_compile_id . $_compile_dir_sep;
} else {
$_compile_id = '';
}
$_cache_dir = $this->smarty->cache_dir;
if (strpos('/\\', substr($_cache_dir, -1)) === false) {
$_cache_dir .= DS;
}
return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
}
} }
?> ?>

View File

@@ -97,7 +97,11 @@ class Smarty_Internal_Debug extends Smarty_Internal_Data {
*/ */
static function get_key($template) static function get_key($template)
{ {
$key = sha1($template->getTemplateFilepath()); // calculate Uid if not already done
if ($template->templateUid == '') {
$template->getTemplateFilepath();
}
$key = $template->templateUid;
if (isset(self::$template_data[$key])) { if (isset(self::$template_data[$key])) {
return $key; return $key;
} else { } else {

View File

@@ -24,16 +24,17 @@ class Smarty_Internal_Resource_Extends {
// properties // properties
public $usesCompiler = true; public $usesCompiler = true;
public $isEvaluated = false; public $isEvaluated = false;
public $allFilepaths = array();
/** /**
* Return flag if template source is existing * Return flag if template source is existing
* *
* @param object $template template object * @param object $_template template object
* @return boolean result * @return boolean result
*/ */
public function isExisting($template) public function isExisting($_template)
{ {
if ($template->getTemplateFilepath() === false) { if ($_template->getTemplateFilepath() === false) {
return false; return false;
} else { } else {
return true; return true;
@@ -42,56 +43,59 @@ class Smarty_Internal_Resource_Extends {
/** /**
* Get filepath to template source * Get filepath to template source
* *
* @param object $template template object * @param object $_template template object
* @return string filepath to template source file * @return string filepath to template source file
*/ */
public function getTemplateFilepath($template) public function getTemplateFilepath($_template)
{ {
$_files = explode('|', $template->resource_name); $sha1String = '';
$_filepath = $template->buildTemplateFilepath ($_files[count($_files)-1]); $_files = explode('|', $_template->resource_name);
foreach ($_files as $_file) {
$_filepath = $_template->buildTemplateFilepath ($_file);
if ($_filepath !== false) { if ($_filepath !== false) {
if ($template->security) { if ($_template->security) {
$template->smarty->security_handler->isTrustedResourceDir($_filepath); $_template->smarty->security_handler->isTrustedResourceDir($_filepath);
} }
} }
$sha1String .= $_filepath;
$this->allFilepaths[] = $_filepath;
}
$_template->templateUid = sha1($sha1String);
return $_filepath; return $_filepath;
} }
/** /**
* Get timestamp to template source * Get timestamp to template source
* *
* @param object $template template object * @param object $_template template object
* @return integer timestamp of template source file * @return integer timestamp of template source file
*/ */
public function getTemplateTimestamp($template) public function getTemplateTimestamp($_template)
{ {
return filemtime($template->getTemplateFilepath()); return filemtime($_template->getTemplateFilepath());
} }
/** /**
* Read template source from file * Read template source from file
* *
* @param object $template template object * @param object $_template template object
* @return string content of template source file * @return string content of template source file
*/ */
public function getTemplateSource($template) public function getTemplateSource($_template)
{ {
$this->template = $template; $this->template = $_template;
// $saved_filepath = $template->getTemplateFilepath(); $_files = array_reverse($this->allFilepaths);
$_files = explode('|', $template->resource_name); foreach ($_files as $_filepath) {
$_files = array_reverse($_files);
foreach ($_files as $_file) {
$_filepath = $template->buildTemplateFilepath ($_file);
// read template file // read template file
if ($_filepath === false) { if ($_filepath === false) {
throw new Exception("Unable to load template \"file : {$_file}\""); throw new Exception("Unable to load template \"file : {$_file}\"");
} }
if ($_file != $_files[0]) { if ($_filepath != $_files[0]) {
$template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath)); $_template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath));
} }
$template->template_filepath = $_filepath; $_template->template_filepath = $_filepath;
$_content = file_get_contents($_filepath); $_content = file_get_contents($_filepath);
if ($_file != $_files[count($_files)-1]) { if ($_filepath != $_files[count($_files)-1]) {
if (preg_match_all('/(' . $this->smarty->left_delimiter . 'block(.+?)' . $this->smarty->right_delimiter . ')/', $_content, $_open, PREG_OFFSET_CAPTURE) != if (preg_match_all('/(' . $this->smarty->left_delimiter . 'block(.+?)' . $this->smarty->right_delimiter . ')/', $_content, $_open, PREG_OFFSET_CAPTURE) !=
preg_match_all('/(' . $this->smarty->left_delimiter . '\/block(.*?)' . $this->smarty->right_delimiter . ')/', $_content, $_close, PREG_OFFSET_CAPTURE)) { preg_match_all('/(' . $this->smarty->left_delimiter . '\/block(.*?)' . $this->smarty->right_delimiter . ')/', $_content, $_close, PREG_OFFSET_CAPTURE)) {
$this->smarty->trigger_error(" unmatched {block} {/block} pairs"); $this->smarty->trigger_error(" unmatched {block} {/block} pairs");
@@ -103,11 +107,11 @@ class Smarty_Internal_Resource_Extends {
$this->saveBlockData($_block_content, $_open[0][$_i][0], $_filepath); $this->saveBlockData($_block_content, $_open[0][$_i][0], $_filepath);
} }
} else { } else {
$template->template_source = $_content; $_template->template_source = $_content;
return true; return true;
} }
} }
// $template->template_filepath = $saved_filepath; // $_template->template_filepath = $saved_filepath;
} }
protected function saveBlockData($block_content, $block_tag, $_filepath) protected function saveBlockData($block_content, $block_tag, $_filepath)
{ {
@@ -141,35 +145,39 @@ class Smarty_Internal_Resource_Extends {
/** /**
* Get filepath to compiled template * Get filepath to compiled template
* *
* @param object $template template object * @param object $_template template object
* @return string return path to compiled template * @return string return path to compiled template
*/ */
public function getCompiledFilepath($template) public function getCompiledFilepath($_template)
{ {
$_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;
$_files = explode('|', $template->resource_name); $_files = explode('|', $_template->resource_name);
$_filepath = sha1($template->getTemplateFilepath()); // calculate Uid if not already done
if ($_template->templateUid == '') {
$_template->getTemplateFilepath();
}
$_filepath = $_template->templateUid;
// if use_sub_dirs, break file into directories // if use_sub_dirs, break file into directories
if ($template->smarty->use_sub_dirs) { if ($_template->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS $_filepath = substr($_filepath, 0, 2) . DS
. substr($_filepath, 2, 2) . DS . substr($_filepath, 2, 2) . DS
. substr($_filepath, 4, 2) . DS . substr($_filepath, 4, 2) . DS
. $_filepath; . $_filepath;
} }
$_compile_dir_sep = $template->smarty->use_sub_dirs ? DS : '^'; $_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 . $_compile_dir_sep . $_filepath;
} }
if ($template->caching) { if ($_template->caching) {
$_cache = '.cache'; $_cache = '.cache';
} else { } else {
$_cache = ''; $_cache = '';
} }
$_compile_dir = $template->smarty->compile_dir; $_compile_dir = $_template->smarty->compile_dir;
if (substr($_compile_dir, -1) != DS) { if (substr($_compile_dir, -1) != DS) {
$_compile_dir .= DS; $_compile_dir .= DS;
} }
return $_compile_dir . $_filepath . '.' . $template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php'; return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';
} }
} }

View File

@@ -54,6 +54,7 @@ class Smarty_Internal_Resource_File {
$_template->smarty->security_handler->isTrustedResourceDir($_filepath); $_template->smarty->security_handler->isTrustedResourceDir($_filepath);
} }
} }
$_template->templateUid = sha1($_filepath);
return $_filepath; return $_filepath;
} }
@@ -94,7 +95,11 @@ class Smarty_Internal_Resource_File {
public function getCompiledFilepath($_template) public function getCompiledFilepath($_template)
{ {
$_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->getTemplateFilepath()); // calculate Uid if not already done
if ($_template->templateUid == '') {
$_template->getTemplateFilepath();
}
$_filepath = $_template->templateUid;
// if use_sub_dirs, break file into directories // if use_sub_dirs, break file into directories
if ($_template->smarty->use_sub_dirs) { if ($_template->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS $_filepath = substr($_filepath, 0, 2) . DS

View File

@@ -52,7 +52,7 @@ class Smarty_Internal_Resource_PHP {
if ($_template->security) { if ($_template->security) {
$_template->smarty->security_handler->isTrustedResourceDir($_filepath); $_template->smarty->security_handler->isTrustedResourceDir($_filepath);
} }
$_template->templateUid = sha1($_filepath);
return $_filepath; return $_filepath;
} }

View File

@@ -50,7 +50,7 @@ class Smarty_Internal_Resource_Registered {
// no filepath for strings // no filepath for strings
// return "string" for compiler error messages // return "string" for compiler error messages
$_filepath = $_template->resource_type .':'.$_template->resource_name; $_filepath = $_template->resource_type .':'.$_template->resource_name;
$_template->templateUid = sha1($_filepath);
return $_filepath; return $_filepath;
} }
@@ -105,7 +105,11 @@ class Smarty_Internal_Resource_Registered {
public function getCompiledFilepath($_template) public function getCompiledFilepath($_template)
{ {
$_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->template_resource); // calculate Uid if not already done
if ($_template->templateUid == '') {
$_template->getTemplateFilepath();
}
$_filepath = $_template->$templateUid;
// if use_sub_dirs, break file into directories // if use_sub_dirs, break file into directories
if ($_template->smarty->use_sub_dirs) { if ($_template->smarty->use_sub_dirs) {
$_filepath = substr($_filepath, 0, 2) . DS $_filepath = substr($_filepath, 0, 2) . DS

View File

@@ -32,6 +32,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
public $resource_name = null; public $resource_name = null;
public $resource_object = null; public $resource_object = null;
private $isExisting = null; private $isExisting = null;
public $templateUid = '';
// Template source // Template source
public $template_filepath = null; public $template_filepath = null;
public $template_source = null; public $template_source = null;
@@ -243,7 +244,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
{ {
if (!$this->resource_object->isEvaluated) { if (!$this->resource_object->isEvaluated) {
$this->properties['file_dependency'] = array(); $this->properties['file_dependency'] = array();
$this->properties['file_dependency'][sha1($this->getTemplateFilepath())] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp()); $this->properties['file_dependency'][$this->templateUid] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
} }
if ($this->smarty->debugging) { if ($this->smarty->debugging) {
Smarty_Internal_Debug::start_compile($this); Smarty_Internal_Debug::start_compile($this);
@@ -260,15 +261,6 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
if (!$this->resource_object->isEvaluated) { if (!$this->resource_object->isEvaluated) {
// write compiled template // write compiled template
Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->compiled_template, $this->smarty); Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->compiled_template, $this->smarty);
// make template and compiled file timestamp match
/**
* $this->compiled_timestamp = null;
* touch($this->getCompiledFilepath(), $this->getTemplateTimestamp());
* // daylight saving time problem on windows
* if ($this->template_timestamp != $this->getCompiledTimestamp()) {
* touch($this->getCompiledFilepath(), 2 * $this->template_timestamp - $this->compiled_timestamp);
* }
*/
} }
} else { } else {
// error compiling template // error compiling template
@@ -452,7 +444,7 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
} }
$this->rendered_content = ob_get_clean(); $this->rendered_content = ob_get_clean();
if (!$this->resource_object->isEvaluated) { if (!$this->resource_object->isEvaluated) {
$this->properties['file_dependency'][sha1($this->getTemplateFilepath())] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp()); $this->properties['file_dependency'][$this->templateUid] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
} }
if ($this->parent instanceof Smarty_Template or $this->parent instanceof Smarty_Internal_Template) { if ($this->parent instanceof Smarty_Template or $this->parent instanceof Smarty_Internal_Template) {
$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']);