- fixed merging of recursive includes

This commit is contained in:
uwe.tews
2009-10-24 09:27:53 +00:00
parent f88b868d5a
commit 6c7c18d79f
2 changed files with 160 additions and 157 deletions

View File

@@ -1,5 +1,6 @@
10/24/2009 10/24/2009
- fixed nocache vars at internal block tags - fixed nocache vars at internal block tags
- fixed merging of recursive includes
10/23/2009 10/23/2009
- fixed nocache var problem - fixed nocache var problem

View File

@@ -1,157 +1,159 @@
<?php <?php
/** /**
* Smarty Internal Plugin Compile Include * Smarty Internal Plugin Compile Include
* *
* Compiles the {include} tag * Compiles the {include} tag
* *
* @package Smarty * @package Smarty
* @subpackage Compiler * @subpackage Compiler
* @author Uwe Tews * @author Uwe Tews
*/ */
/** /**
* Smarty Internal Plugin Compile Include Class * Smarty Internal Plugin Compile Include Class
*/ */
class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase { class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
/** /**
* Compiles code for the {include} tag * Compiles code for the {include} tag
* *
* @param array $args array with attributes from parser * @param array $args array with attributes from parser
* @param object $compiler compiler object * @param object $compiler compiler object
* @return string compiled code * @return string compiled code
*/ */
public function compile($args, $compiler) public function compile($args, $compiler)
{ {
$this->compiler = $compiler; $this->compiler = $compiler;
$this->required_attributes = array('file'); $this->required_attributes = array('file');
$this->optional_attributes = array('_any'); $this->optional_attributes = array('_any');
// check and get attributes // check and get attributes
$_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; $has_compiled_template = false;
if ($compiler->smarty->merge_compiled_includes) { if ($compiler->smarty->merge_compiled_includes) {
// check if compiled code can be merged (contains no variable part) // check if compiled code can be merged (contains no variable part)
if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) and substr_count($include_file, '(') == 0) { if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2) and substr_count($include_file, '(') == 0) {
eval("\$tmp = $include_file;"); eval("\$tmp = $include_file;");
$tpl = $compiler->smarty->createTemplate ($tmp, $compiler->template->cache_id, $compiler->template->compile_id, $compiler->template); if ($this->compiler->template->template_resource != $tmp) {
if ($tpl->isExisting()) { $tpl = $compiler->smarty->createTemplate ($tmp, $compiler->template->cache_id, $compiler->template->compile_id, $compiler->template);
do { if ($tpl->isExisting()) {
$must_compile = false; do {
$prop = array(); $must_compile = false;
$compiled_tpl = $tpl->getCompiledTemplate(); $prop = array();
preg_match('/(\<\?php \$_smarty_tpl-\>decodeProperties\(\')(.*)(\'.*\?\>)/', $compiled_tpl, $matches); $compiled_tpl = $tpl->getCompiledTemplate();
$compiled_tpl = preg_replace(array('/(\<\?php \$_smarty_tpl-\>decodeProperties\(\')(.*)(\'.*\?\>.*\n)/', '/(\<\?php if\(\!defined\(\'SMARTY_DIR\'\)\))(.*)(\?\>.*\n)/'), '', $compiled_tpl); preg_match('/(\<\?php \$_smarty_tpl-\>decodeProperties\(\')(.*)(\'.*\?\>)/', $compiled_tpl, $matches);
// var_dump($matches, $compiled_tpl); $compiled_tpl = preg_replace(array('/(\<\?php \$_smarty_tpl-\>decodeProperties\(\')(.*)(\'.*\?\>.*\n)/', '/(\<\?php if\(\!defined\(\'SMARTY_DIR\'\)\))(.*)(\?\>.*\n)/'), '', $compiled_tpl);
if (isset($matches[2])) { // var_dump($matches, $compiled_tpl);
$prop = unserialize($matches[2]); if (isset($matches[2])) {
foreach ($prop['file_dependency'] as $_file_to_check) { $prop = unserialize($matches[2]);
If (is_file($_file_to_check[0])) { foreach ($prop['file_dependency'] as $_file_to_check) {
$mtime = filemtime($_file_to_check[0]); If (is_file($_file_to_check[0])) {
} else { $mtime = filemtime($_file_to_check[0]);
$tpl->parseResourceName($_file_to_check[0], $resource_type, $resource_name, $resource_handler); } else {
$mtime = $resource_handler->getTemplateTimestampTypeName($resource_type, $resource_name); $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; If ($mtime != $_file_to_check[1]) {
break; $must_compile = true;
} break;
} }
if ($must_compile) { }
// recompile if ($must_compile) {
$tpl->compileTemplateSource(); // recompile
} $tpl->compileTemplateSource();
} }
} while ($must_compile); }
if (isset($prop['file_dependency'])) { } while ($must_compile);
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $prop['file_dependency']); if (isset($prop['file_dependency'])) {
} $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $prop['file_dependency']);
$has_compiled_template = true; }
} $has_compiled_template = true;
} }
} }
}
if (isset($_attr['assign'])) { }
// output will be stored in a smarty variable instead of beind displayed
$_assign = $_attr['assign']; if (isset($_attr['assign'])) {
} // output will be stored in a smarty variable instead of beind displayed
$_assign = $_attr['assign'];
$_parent_scope = SMARTY_LOCAL_SCOPE; }
if (isset($_attr['scope'])) {
if ($_attr['scope'] == '\'parent\'') { $_parent_scope = SMARTY_LOCAL_SCOPE;
$_parent_scope = SMARTY_PARENT_SCOPE; if (isset($_attr['scope'])) {
} elseif ($_attr['scope'] == '\'root\'') { if ($_attr['scope'] == '\'parent\'') {
$_parent_scope = SMARTY_ROOT_SCOPE; $_parent_scope = SMARTY_PARENT_SCOPE;
} elseif ($_attr['scope'] == '\'global\'') { } elseif ($_attr['scope'] == '\'root\'') {
$_parent_scope = SMARTY_GLOBAL_SCOPE; $_parent_scope = SMARTY_ROOT_SCOPE;
} } elseif ($_attr['scope'] == '\'global\'') {
} $_parent_scope = SMARTY_GLOBAL_SCOPE;
// default for included templates }
if ($compiler->template->caching) { }
$_caching = SMARTY_CACHING_LIFETIME_CURRENT; // default for included templates
} else { if ($compiler->template->caching) {
$_caching = SMARTY_CACHING_OFF; $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
} } else {
/* $_caching = SMARTY_CACHING_OFF;
* if the {include} tag provides individual parameter for caching }
* it will not be included into the common cache file and treated like /*
* a nocache section * if the {include} tag provides individual parameter for caching
*/ * it will not be included into the common cache file and treated like
if (isset($_attr['cache_lifetime'])) { * a nocache section
$_cache_lifetime = $_attr['cache_lifetime']; */
$this->compiler->tag_nocache = true; if (isset($_attr['cache_lifetime'])) {
} $_cache_lifetime = $_attr['cache_lifetime'];
if (isset($_attr['nocache'])) { $this->compiler->tag_nocache = true;
if ($_attr['nocache'] == 'true') { }
$this->compiler->tag_nocache = true; if (isset($_attr['nocache'])) {
} if ($_attr['nocache'] == 'true') {
} $this->compiler->tag_nocache = true;
if (isset($_attr['caching'])) { }
if ($_attr['caching'] == 'true') { }
$_caching = SMARTY_CACHING_LIFETIME_CURRENT; if (isset($_attr['caching'])) {
} else { if ($_attr['caching'] == 'true') {
$_caching = SMARTY_CACHING_OFF; $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
} } else {
} $_caching = SMARTY_CACHING_OFF;
// create template object }
$_output = "<?php \$_template = new Smarty_Template ($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, \$_smarty_tpl->cache_id, \$_smarty_tpl->compile_id);"; }
// delete {include} standard attributes // create template object
unset($_attr['file'], $_attr['assign'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope']); $_output = "<?php \$_template = new Smarty_Template ($include_file, \$_smarty_tpl->smarty, \$_smarty_tpl, \$_smarty_tpl->cache_id, \$_smarty_tpl->compile_id);";
// remaining attributes must be assigned as smarty variable // delete {include} standard attributes
if (!empty($_attr)) { unset($_attr['file'], $_attr['assign'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope']);
if ($_parent_scope == SMARTY_LOCAL_SCOPE) { // remaining attributes must be assigned as smarty variable
// create variables if (!empty($_attr)) {
foreach ($_attr as $_key => $_value) { if ($_parent_scope == SMARTY_LOCAL_SCOPE) {
$_output .= "\$_template->assign('$_key',$_value);"; // create variables
} foreach ($_attr as $_key => $_value) {
} else { $_output .= "\$_template->assign('$_key',$_value);";
$this->compiler->trigger_template_error('variable passing not allowed in parent/global scope'); }
} } else {
} $this->compiler->trigger_template_error('variable passing not allowed in parent/global scope');
// add caching parameter if required }
if (isset($_cache_lifetime)) { }
$_output .= "\$_template->cache_lifetime = $_cache_lifetime;"; // add caching parameter if required
$_caching = SMARTY_CACHING_LIFETIME_CURRENT; if (isset($_cache_lifetime)) {
} $_output .= "\$_template->cache_lifetime = $_cache_lifetime;";
$_output .= "\$_template->caching = $_caching;"; $_caching = SMARTY_CACHING_LIFETIME_CURRENT;
// was there an assign attribute }
if (isset($_assign)) { $_output .= "\$_template->caching = $_caching;";
$_output .= "\$_smarty_tpl->assign($_assign,\$_template->fetch()); ?>"; // was there an assign attribute
} else { if (isset($_assign)) {
if ($has_compiled_template) { $_output .= "\$_smarty_tpl->assign($_assign,\$_template->fetch()); ?>";
$_output .= " \$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n"; } else {
$_output .= $compiled_tpl . "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>"; if ($has_compiled_template) {
$_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>"; $_output .= " \$_tpl_stack[] = \$_smarty_tpl; \$_smarty_tpl = \$_template;?>\n";
} else { $_output .= $compiled_tpl . "<?php /* End of included template \"" . $tpl->getTemplateFilepath() . "\" */ ?>";
$_output .= " echo \$_template->fetch(); ?>"; $_output .= "<?php \$_smarty_tpl = array_pop(\$_tpl_stack);?>";
} } else {
} $_output .= " echo \$_template->fetch(); ?>";
if ($_parent_scope != SMARTY_LOCAL_SCOPE) { }
$_output .= "<?php \$_template->updateParentVariables($_parent_scope); ?>"; }
} if ($_parent_scope != SMARTY_LOCAL_SCOPE) {
$_output .= "<?php unset(\$_template); ?>"; $_output .= "<?php \$_template->updateParentVariables($_parent_scope); ?>";
return $_output; }
} $_output .= "<?php unset(\$_template); ?>";
} return $_output;
}
?> }
?>