- improvements and bugfix on {include} tag handling

******************************************************************
NOTICE: existing compiled template and cache files must be deleted
******************************************************************
This commit is contained in:
Uwe.Tews
2009-09-23 16:50:16 +00:00
parent 5bf063d2cc
commit 324276f7fa
4 changed files with 69 additions and 34 deletions

View File

@@ -1,3 +1,7 @@
09/23/2009
- improvements and bugfix on {include} tag handling
NOTICE: existing compiled template and cache files must be deleted
09/19/2009 09/19/2009
- replace internal "eval()" calls by "include" during rendering process - replace internal "eval()" calls by "include" during rendering process
- speed improvment for templates which have included subtemplates - speed improvment for templates which have included subtemplates

View File

@@ -29,15 +29,41 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
$_attr = $this->_get_attributes($args); $_attr = $this->_get_attributes($args);
// save posible attributes // save posible attributes
$include_file = $_attr['file']; $include_file = $_attr['file'];
$has_compiled_template = false;
if (true) {
// check if compiled code can be merged // check if compiled code can be merged
if (strpos($include_file, '$_smarty_tpl') === false) { if (strpos($include_file, '$_smarty_tpl') === false) {
$tpl = new Smarty_Template (trim($include_file, "'\""), $compiler->smarty, $compiler->template); $tpl = $compiler->smarty->createTemplate (trim($include_file, "'\""), $compiler->template->cache_id, $compiler->template->compile_id, $compiler->template);
$compiled_tpl = $tpl->getCompiledTemplate() . "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>"; do {
$compiler->template->properties['file_dependency']['F' . abs(crc32($tpl->getTemplateFilepath()))] = array($tpl->getTemplateFilepath(), $tpl->getTemplateTimestamp()); $must_compile = false;
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']); $prop = array();
$has_compiled_template = true; $compiled_tpl = $tpl->getCompiledTemplate();
preg_match('/(\<\?php \$_smarty_tpl-\>decodeProperties\(\')(.*)(\'.*\?\>)/', $compiled_tpl, $matches); //var_dump($matches, $compiled_tpl);
if (isset($matches[2])) {
$prop = unserialize($matches[2]);
foreach ($prop['file_dependency'] as $_file_to_check) {
If (is_file($_file_to_check[0])) {
$mtime = filemtime($_file_to_check[0]);
} else { } else {
$has_compiled_template = false; $tpl->parseResourceName($_file_to_check[0], $resource_type, $resource_name, $resource_handler);
$mtime = $resource_handler->getTemplateTimestampTypeName($resource_type, $resource_name);
}
If ($mtime != $_file_to_check[1]) {
$must_compile = true;
break;
}
}
if ($must_compile) {
// recompile
$tpl->compileTemplateSource();
}
}
} while ($must_compile);
if (isset($prop['file_dependency'])) {
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $prop['file_dependency']);
}
$has_compiled_template = true;
}
} }
if (isset($_attr['assign'])) { if (isset($_attr['assign'])) {
@@ -56,11 +82,11 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
} }
} }
// default for included templates // default for included templates
// if ($compiler->template->caching) { if ($compiler->template->caching) {
// $_caching = SMARTY_CACHING_LIFETIME_CURRENT; $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
// } else { } else {
$_caching = SMARTY_CACHING_OFF; $_caching = SMARTY_CACHING_OFF;
// } }
/* /*
* if the {include} tag provides individual parameter for caching * if the {include} tag provides individual parameter for caching
* it will not be included into the common cache file and treated like * it will not be included into the common cache file and treated like
@@ -108,9 +134,9 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
$_output .= "\$_smarty_tpl->assign($_assign,\$_smarty_tpl->smarty->fetch(\$_template)); ?>"; $_output .= "\$_smarty_tpl->assign($_assign,\$_smarty_tpl->smarty->fetch(\$_template)); ?>";
} else { } else {
if ($has_compiled_template) { if ($has_compiled_template) {
$_output .= " \$_tmp = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n"; $_output .= " \$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
$_output .= $compiled_tpl; $_output .= $compiled_tpl . "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>";;
$_output .= "<?php \$_smarty_tpl = \$_tmp;?>"; $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
} else { } else {
$_output .= " echo \$_smarty_tpl->smarty->fetch(\$_template); ?>"; $_output .= " echo \$_smarty_tpl->smarty->fetch(\$_template); ?>";
} }

View File

@@ -100,11 +100,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
if (!$this->parseResourceName ($template_resource, $this->resource_type, $this->resource_name, $dummy)) { if (!$this->parseResourceName ($template_resource, $this->resource_type, $this->resource_name, $dummy)) {
throw new Exception ("Unable to parse resource name \"{$template_resource}\""); throw new Exception ("Unable to parse resource name \"{$template_resource}\"");
} }
// load cacher
if ($this->caching) {
$this->smarty->loadPlugin($this->cacher_class);
$this->cacher_object = new $this->cacher_class($this->smarty);
}
// load cache resource // load cache resource
if (!$this->isEvaluated() && $this->caching && !isset($this->smarty->cache_resource_objects[$this->caching_type])) { if (!$this->isEvaluated() && $this->caching && !isset($this->smarty->cache_resource_objects[$this->caching_type])) {
$this->smarty->loadPlugin($this->cache_resource_class); $this->smarty->loadPlugin($this->cache_resource_class);
@@ -262,6 +257,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
public function compileTemplateSource () public function compileTemplateSource ()
{ {
$_start_time = $this->_get_time(); $_start_time = $this->_get_time();
if (!$this->isEvaluated) {
$this->properties['file_dependency']['F' . abs(crc32($this->getTemplateFilepath()))] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp());
}
// compile template // compile template
if (!is_object($this->compiler_object)) { if (!is_object($this->compiler_object)) {
// load compiler // load compiler
@@ -271,6 +269,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
// $this->smarty->loadPlugin('Smarty_Internal_TemplateCompilerBase'); // $this->smarty->loadPlugin('Smarty_Internal_TemplateCompilerBase');
$this->smarty->loadPlugin($this->resource_objects[$this->resource_type]->compiler_class); $this->smarty->loadPlugin($this->resource_objects[$this->resource_type]->compiler_class);
$this->compiler_object = new $this->resource_objects[$this->resource_type]->compiler_class($this->resource_objects[$this->resource_type]->template_lexer_class, $this->resource_objects[$this->resource_type]->template_parser_class, $this->smarty); $this->compiler_object = new $this->resource_objects[$this->resource_type]->compiler_class($this->resource_objects[$this->resource_type]->template_lexer_class, $this->resource_objects[$this->resource_type]->template_parser_class, $this->smarty);
// load cacher
if ($this->caching) {
$this->smarty->loadPlugin($this->cacher_class);
$this->cacher_object = new $this->cacher_class($this->smarty);
}
} }
if (!is_object($this->smarty->write_file_object)) { if (!is_object($this->smarty->write_file_object)) {
require_once(SMARTY_SYSPLUGINS_DIR . 'internal.write_file.php'); require_once(SMARTY_SYSPLUGINS_DIR . 'internal.write_file.php');
@@ -283,8 +286,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
if (!$this->isEvaluated()) { if (!$this->isEvaluated()) {
// build template property string // build template property string
$this->properties_string = "<?php \$_smarty_tpl->decodeProperties('" . str_replace("'", '"', (serialize($this->properties))) . "'); ?>\n"; $this->properties_string = "<?php \$_smarty_tpl->decodeProperties('" . str_replace("'", '"', (serialize($this->properties))) . "'); ?>\n";
$this->compiled_template = $this->dir_acc_sec_string . $this->properties_string . $this->compiled_template;
// write compiled template // write compiled template
$this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->dir_acc_sec_string . $this->properties_string . $this->getCompiledTemplate()); $this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->compiled_template);
// make template and compiled file timestamp match // make template and compiled file timestamp match
touch($this->getCompiledFilepath(), $this->getTemplateTimestamp()); touch($this->getCompiledFilepath(), $this->getTemplateTimestamp());
} }
@@ -464,14 +468,15 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
$this->render_time += $this->_get_time() - $_start_time; $this->render_time += $this->_get_time() - $_start_time;
$this->rendered_content = ob_get_clean(); $this->rendered_content = ob_get_clean();
if (!$this->isEvaluated) { if (!$this->isEvaluated) {
$this->properties['file_dependency']['F'.abs(crc32($this->getTemplateFilepath()))] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp()); $this->properties['file_dependency']['F' . abs(crc32($this->getTemplateFilepath()))] = 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) {
// var_dump('merge ', $this->parent->getTemplateFilepath(), $this->parent->properties['file_dependency'], $this->getTemplateFilepath(), $this->properties['file_dependency']);
$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']);
} }
// write to cache when nessecary // write to cache when nessecary
if (!$this->isEvaluated() && $this->caching) { if (!$this->isEvaluated() && $this->caching) {
//$this->properties['file_dependency'] = array_unique($this->properties['file_dependency']); // $this->properties['file_dependency'] = array_unique($this->properties['file_dependency']);
// write rendered template // write rendered template
$this->writeCachedContent($this); $this->writeCachedContent($this);
if ($this->usesCompiler()) { if ($this->usesCompiler()) {
@@ -625,7 +630,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
} }
if (isset($prop['file_dependency'])) { if (isset($prop['file_dependency'])) {
$this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $prop['file_dependency']); $this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $prop['file_dependency']);
//$this->properties['file_dependency'] = array_unique($this->properties['file_dependency']); // $this->properties['file_dependency'] = array_unique($this->properties['file_dependency']);
} }
if (!empty($prop['function'])) { if (!empty($prop['function'])) {
foreach ($prop['function'] as $_name => $_data) { foreach ($prop['function'] as $_name => $_data) {

View File

@@ -296,12 +296,12 @@ class Smarty_Internal_TemplateBase {
// we got a template resource // we got a template resource
$_templateId = $this->buildTemplateId ($template, $cache_id, $compile_id); $_templateId = $this->buildTemplateId ($template, $cache_id, $compile_id);
// already in template cache? // already in template cache?
if (isset($this->template_objects[$_templateId])) { if (isset($this->smarty->template_objects[$_templateId])) {
// return cached template object // return cached template object
return $this->template_objects[$_templateId]; return $this->smarty->template_objects[$_templateId];
} else { } else {
// create and cache new template object // create and cache new template object
return new $this->template_class($template, $this, $parent, $cache_id, $compile_id); return new $this->template_class($template, $this->smarty, $parent, $cache_id, $compile_id);
} }
} else { } else {
// just return a copy of template class // just return a copy of template class