Skip to content

Commit

Permalink
- bugfix internal template function data got stored in wrong compiled…
Browse files Browse the repository at this point in the history
… file #114
  • Loading branch information
uwetews committed Dec 8, 2015
1 parent be8340b commit 29e2eb0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
 ===== 3.1.28-dev===== (xx.xx.2015)
08.12.2015
- bugfix internal template function data got stored in wrong compiled file https://github.com/smarty-php/smarty/issues/114

05.12.2015
-bugfix {strip} should insert a single space https://github.com/smarty-php/smarty/issues/111

Expand Down
12 changes: 6 additions & 6 deletions libs/sysplugins/smarty_internal_compile_function.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
}
unset($_attr['nocache']);
$_name = trim($_attr['name'], "'\"");
$compiler->parent_compiler->template->tpl_function[$_name] = array();
$compiler->parent_compiler->tpl_function[$_name] = $compiler->parent_compiler->template->tpl_function[$_name] = array();
$save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
$compiler->template->caching);
$this->openTag($compiler, 'function', $save);
Expand Down Expand Up @@ -105,9 +105,9 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
$saved_data = $this->closeTag($compiler, array('function'));
$_attr = $saved_data[0];
$_name = trim($_attr['name'], "'\"");
$compiler->parent_compiler->template->tpl_function[$_name]['called_functions'] = $compiler->called_functions;
$compiler->parent_compiler->template->tpl_function[$_name]['compiled_filepath'] = $compiler->parent_compiler->template->compiled->filepath;
$compiler->parent_compiler->template->tpl_function[$_name]['uid'] = $compiler->template->source->uid;
$compiler->parent_compiler->tpl_function[$_name]['called_functions'] = $compiler->parent_compiler->template->tpl_function[$_name]['called_functions'] = $compiler->called_functions;
$compiler->parent_compiler->tpl_function[$_name]['compiled_filepath'] = $compiler->parent_compiler->template->tpl_function[$_name]['compiled_filepath'] = $compiler->parent_compiler->template->compiled->filepath;
$compiler->parent_compiler->tpl_function[$_name]['uid'] = $compiler->parent_compiler->template->tpl_function[$_name]['uid'] = $compiler->template->source->uid;
$compiler->called_functions = array();
$_parameter = $_attr;
unset($_parameter['name']);
Expand All @@ -133,7 +133,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
$_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
$_funcNameCaching = $_funcName . '_nocache';
if ($compiler->template->compiled->has_nocache_code) {
$compiler->parent_compiler->template->tpl_function[$_name]['call_name_caching'] = $_funcNameCaching;
$compiler->parent_compiler->tpl_function[$_name]['call_name_caching'] = $compiler->parent_compiler->template->tpl_function[$_name]['call_name_caching'] = $_funcNameCaching;
$output = "<?php\n";
$output .= "/* {$_funcNameCaching} */\n";
$output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
Expand Down Expand Up @@ -162,7 +162,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
$_functionCode = new Smarty_Internal_ParseTree_Tag($compiler->parser, preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/", array($this,
'removeNocache'), $_functionCode->to_smarty_php($compiler->parser)));
}
$compiler->parent_compiler->template->tpl_function[$_name]['call_name'] = $_funcName;
$compiler->parent_compiler->tpl_function[$_name]['call_name'] = $compiler->parent_compiler->template->tpl_function[$_name]['call_name'] = $_funcName;
$output = "<?php\n";
$output .= "/* {$_funcName} */\n";
$output .= "if (!function_exists('{$_funcName}')) {\n";
Expand Down
54 changes: 30 additions & 24 deletions libs/sysplugins/smarty_internal_runtime_codeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,63 @@ class Smarty_Internal_Runtime_CodeFrame
/**
* Create code frame for compiled and cached templates
*
* @param Smarty_Internal_Template $_template
* @param string $content optional template content
* @param bool $cache flag for cache file
* @param Smarty_Internal_Template $_template
* @param string $content optional template content
* @param string $functions compiled template function and block code
* @param bool $cache flag for cache file
* @param \Smarty_Internal_TemplateCompilerBase $compiler
*
* @return string
*/
public function create(Smarty_Internal_Template $_template, $content = '', $functions = '', $cache = false)
public function create(Smarty_Internal_Template $_template, $content = '', $functions = '', $cache = false,
Smarty_Internal_TemplateCompilerBase $compiler = null)
{
// build property code
$properties['has_nocache_code'] = $_template->compiled->has_nocache_code;
$properties['version'] = Smarty::SMARTY_VERSION;
$properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
if (!empty($_template->tpl_function)) {
$properties['tpl_function'] = $_template->tpl_function;
}
$properties[ 'has_nocache_code' ] = $_template->compiled->has_nocache_code;
$properties[ 'version' ] = Smarty::SMARTY_VERSION;
$properties[ 'unifunc' ] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
if (!$cache) {
$properties['file_dependency'] = $_template->compiled->file_dependency;
$properties['includes'] = $_template->compiled->includes;
$properties[ 'file_dependency' ] = $_template->compiled->file_dependency;
$properties[ 'includes' ] = $_template->compiled->includes;
if (!empty($compiler->tpl_function)) {
$properties[ 'tpl_function' ] = $compiler->tpl_function;
}
} else {
$properties['file_dependency'] = $_template->cached->file_dependency;
$properties['cache_lifetime'] = $_template->cache_lifetime;
$properties[ 'file_dependency' ] = $_template->cached->file_dependency;
$properties[ 'cache_lifetime' ] = $_template->cache_lifetime;
if (!empty($_template->tpl_function)) {
$properties[ 'tpl_function' ] = $_template->tpl_function;
}
}
$output = "<?php\n";
$output .= "/* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
"\n from \"" . $_template->source->filepath . "\" */\n\n";

$dec = "\$_smarty_tpl->smarty->ext->_validateCompiled->decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' .
($cache ? 'true' : 'false') . ")";
$dec = "\$_smarty_tpl->smarty->ext->_validateCompiled->decodeProperties(\$_smarty_tpl, " .
var_export($properties, true) . ',' . ($cache ? 'true' : 'false') . ")";
$output .= "if ({$dec}) {\n";
$output .= "function {$properties['unifunc']} (\$_smarty_tpl) {\n";
// include code for plugins
if (!$cache) {
if (!empty($_template->compiled->required_plugins['compiled'])) {
foreach ($_template->compiled->required_plugins['compiled'] as $tmp) {
if (!empty($_template->compiled->required_plugins[ 'compiled' ])) {
foreach ($_template->compiled->required_plugins[ 'compiled' ] as $tmp) {
foreach ($tmp as $data) {
$file = addslashes($data['file']);
if (is_array($data['function'])) {
$file = addslashes($data[ 'file' ]);
if (is_array($data[ 'function' ])) {
$output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n";
} else {
$output .= "if (!is_callable('{$data['function']}')) require_once '{$file}';\n";
}
}
}
}
if ($_template->caching && !empty($_template->compiled->required_plugins['nocache'])) {
if ($_template->caching && !empty($_template->compiled->required_plugins[ 'nocache' ])) {
$_template->compiled->has_nocache_code = true;
$output .= "echo '/*%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
foreach ($_template->compiled->required_plugins['nocache'] as $tmp) {
foreach ($_template->compiled->required_plugins[ 'nocache' ] as $tmp) {
foreach ($tmp as $data) {
$file = addslashes($data['file']);
if (is_Array($data['function'])) {
$file = addslashes($data[ 'file' ]);
if (is_Array($data[ 'function' ])) {
$output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n");
} else {
$output .= addslashes("if (!is_callable('{$data['function']}')) require_once '{$file}';\n");
Expand Down
9 changes: 8 additions & 1 deletion libs/sysplugins/smarty_internal_templatecompilerbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ abstract class Smarty_Internal_TemplateCompilerBase
*/
public $write_compiled_code = true;

/**
* Template functions
*
* @var array
*/
public $tpl_function = array();

/**
* called sub functions from template function
*
Expand Down Expand Up @@ -321,7 +328,7 @@ public function compileTemplate(Smarty_Internal_Template $template, $nocache = n
$this->compileTemplateSource($template, $nocache,
$parent_compiler),
$this->postFilter($this->blockOrFunctionCode) .
join('', $this->mergedSubTemplatesCode));
join('', $this->mergedSubTemplatesCode), false, $this);
return $_compiled_code;
}

Expand Down

0 comments on commit 29e2eb0

Please sign in to comment.