04.08.2015

- improvement move template function source parameter into extension
This commit is contained in:
uwetews
2016-08-04 23:10:26 +02:00
parent 335f199cbc
commit ffa270c395
7 changed files with 64 additions and 49 deletions

View File

@@ -1,4 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)  ===== 3.1.30-dev ===== (xx.xx.xx)
04.08.2015
- improvement move template function source parameter into extension
26.07.2016 26.07.2016
- optimization unneeded loading of compiled resource - optimization unneeded loading of compiled resource

View File

@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.30-dev/(88)'; const SMARTY_VERSION = '3.1.30-dev/89';
/** /**
* define variable scopes * define variable scopes

View File

@@ -53,7 +53,6 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
*/ */
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{ {
$compiler->loopNesting ++;
// check and get attributes // check and get attributes
$_attr = $this->getAttributes($compiler, $args); $_attr = $this->getAttributes($compiler, $args);
@@ -62,8 +61,7 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
} }
unset($_attr[ 'nocache' ]); unset($_attr[ 'nocache' ]);
$_name = trim($_attr[ 'name' ], "'\""); $_name = trim($_attr[ 'name' ], "'\"");
$compiler->parent_compiler->tpl_function[ $_name ] = $compiler->parent_compiler->tpl_function[ $_name ] = array();
$compiler->parent_compiler->template->tpl_function[ $_name ] = array();
$save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code, $save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
$compiler->template->caching); $compiler->template->caching);
$this->openTag($compiler, 'function', $save); $this->openTag($compiler, 'function', $save);
@@ -101,20 +99,13 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
*/ */
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{ {
$compiler->loopNesting --;
$this->compiler = $compiler; $this->compiler = $compiler;
$saved_data = $this->closeTag($compiler, array('function')); $saved_data = $this->closeTag($compiler, array('function'));
$_attr = $saved_data[ 0 ]; $_attr = $saved_data[ 0 ];
$_name = trim($_attr[ 'name' ], "'\""); $_name = trim($_attr[ 'name' ], "'\"");
$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->tpl_function[ $_name ][ 'compiled_filepath' ] =
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'compiled_filepath' ] =
$compiler->parent_compiler->template->compiled->filepath; $compiler->parent_compiler->template->compiled->filepath;
$compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
$compiler->called_functions = array();
$_parameter = $_attr; $_parameter = $_attr;
unset($_parameter[ 'name' ]); unset($_parameter[ 'name' ]);
// default parameter // default parameter
@@ -139,8 +130,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
$_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}"; $_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
$_funcNameCaching = $_funcName . '_nocache'; $_funcNameCaching = $_funcName . '_nocache';
if ($compiler->template->compiled->has_nocache_code) { if ($compiler->template->compiled->has_nocache_code) {
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
$output = "<?php\n"; $output = "<?php\n";
$output .= "/* {$_funcNameCaching} */\n"; $output .= "/* {$_funcNameCaching} */\n";
$output .= "if (!function_exists('{$_funcNameCaching}')) {\n"; $output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
@@ -151,14 +141,14 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}"; $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}";
$output .= "\$params = var_export(\$params, true);\n"; $output .= "\$params = var_export(\$params, true);\n";
$output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php "; $output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value, \\\$_smarty_tpl->isRenderingCache);\n}\n?>"; $output .= "\\\$_smarty_tpl->ext->_tplFunction->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value, \\\$_smarty_tpl->isRenderingCache);\n}\n?>";
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>"; $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>";
$compiler->parser->current_buffer->append_subtree($compiler->parser, $compiler->parser->current_buffer->append_subtree($compiler->parser,
new Smarty_Internal_ParseTree_Tag($compiler->parser, new Smarty_Internal_ParseTree_Tag($compiler->parser,
$output)); $output));
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode); $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
$output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php "; $output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n"; $output .= "\\\$_smarty_tpl->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>"; $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
$output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n"; $output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
$output .= "}\n}\n"; $output .= "}\n}\n";
@@ -172,8 +162,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
array($this, 'removeNocache'), array($this, 'removeNocache'),
$_functionCode->to_smarty_php($compiler->parser))); $_functionCode->to_smarty_php($compiler->parser)));
} }
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
$output = "<?php\n"; $output = "<?php\n";
$output .= "/* {$_funcName} */\n"; $output .= "/* {$_funcName} */\n";
$output .= "if (!function_exists('{$_funcName}')) {\n"; $output .= "if (!function_exists('{$_funcName}')) {\n";
@@ -210,9 +199,11 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
} }
/** /**
* Remove nocache code
*
* @param $match * @param $match
* *
* @return mixed * @return string
*/ */
function removeNocache($match) function removeNocache($match)
{ {

View File

@@ -10,6 +10,13 @@
**/ **/
class Smarty_Internal_Runtime_TplFunction class Smarty_Internal_Runtime_TplFunction
{ {
/**
* Array of source information for known template functions
*
* @var array
*/
private $tplFunctions = array();
/** /**
* Call template function * Call template function
* *
@@ -22,14 +29,14 @@ class Smarty_Internal_Runtime_TplFunction
*/ */
public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache) public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache)
{ {
if (isset($tpl->tpl_function[ $name ])) { if (isset($this->tplFunctions[ $name ])) {
if (!$tpl->caching || ($tpl->caching && $nocache)) { if (!$tpl->caching || ($tpl->caching && $nocache)) {
$function = $tpl->tpl_function[ $name ][ 'call_name' ]; $function = $this->tplFunctions[ $name ][ 'call_name' ];
} else { } else {
if (isset($tpl->tpl_function[ $name ][ 'call_name_caching' ])) { if (isset($this->tplFunctions[ $name ][ 'call_name_caching' ])) {
$function = $tpl->tpl_function[ $name ][ 'call_name_caching' ]; $function = $this->tplFunctions[ $name ][ 'call_name_caching' ];
} else { } else {
$function = $tpl->tpl_function[ $name ][ 'call_name' ]; $function = $this->tplFunctions[ $name ][ 'call_name' ];
} }
} }
if (function_exists($function)) { if (function_exists($function)) {
@@ -49,6 +56,39 @@ class Smarty_Internal_Runtime_TplFunction
throw new SmartyException("Unable to find template function '{$name}'"); throw new SmartyException("Unable to find template function '{$name}'");
} }
/**
* Register template functions defined by template
*
* @param \Smarty_Internal_Template $tpl
* @param array $tplFunctions source information array of template functions defined in template
*/
public function registerTplFunctions(Smarty_Internal_Template $tpl, $tplFunctions)
{
$this->tplFunctions = array_merge($this->tplFunctions, $tplFunctions);
$ptr = $tpl;
// make sure that the template functions are known in parent templates
while (isset($ptr->parent) && $ptr->parent->_objType === 2 && !isset($ptr->ext->_tplFunction)) {
$ptr->ext->_tplFunction = $this;
$ptr = $ptr->parent;
}
}
/**
* Return source parameter array for single or all template functions
*
* @param null|string $name template function name
*
* @return array|bool|mixed
*/
public function getTplFunction($name = null)
{
if (isset($name)) {
return $this->tplFunctions[ $name ] ? $this->tplFunctions[ $name ] : false;
} else {
return $this->tplFunctions;
}
}
/** /**
* *
* Add template function to cache file for nocache calls * Add template function to cache file for nocache calls
@@ -61,7 +101,7 @@ class Smarty_Internal_Runtime_TplFunction
*/ */
public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function) public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function)
{ {
$funcParam = $tpl->tpl_function[ $_name ]; $funcParam = $this->tplFunctions[ $_name ];
if (is_file($funcParam[ 'compiled_filepath' ])) { if (is_file($funcParam[ 'compiled_filepath' ])) {
// read compiled file // read compiled file
$code = file_get_contents($funcParam[ 'compiled_filepath' ]); $code = file_get_contents($funcParam[ 'compiled_filepath' ]);

View File

@@ -74,13 +74,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/ */
public $templateId = null; public $templateId = null;
/**
* Known template functions
*
* @var array
*/
public $tpl_function = array();
/** /**
* Scope in which variables shall be assigned * Scope in which variables shall be assigned
* *
@@ -220,10 +213,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
} }
} }
if ($parentIsTpl) { if ($parentIsTpl) {
if (!empty($this->tpl_function)) {
$this->parent->tpl_function = array_merge($this->parent->tpl_function, $this->tpl_function);
}
if (isset($this->compiled)) {
foreach ($this->compiled->required_plugins as $code => $tmp1) { foreach ($this->compiled->required_plugins as $code => $tmp1) {
foreach ($tmp1 as $name => $tmp) { foreach ($tmp1 as $name => $tmp) {
foreach ($tmp as $type => $data) { foreach ($tmp as $type => $data) {
@@ -232,7 +221,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
} }
} }
} }
}
if (!$no_output_filter && if (!$no_output_filter &&
(!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) && (!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) &&
(isset($this->smarty->autoload_filters[ 'output' ]) || (isset($this->smarty->autoload_filters[ 'output' ]) ||
@@ -483,9 +471,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
$resource->has_nocache_code = $properties[ 'has_nocache_code' ]; $resource->has_nocache_code = $properties[ 'has_nocache_code' ];
// $tpl->compiled->nocache_hash = $properties['nocache_hash']; // $tpl->compiled->nocache_hash = $properties['nocache_hash'];
$resource->file_dependency = $properties[ 'file_dependency' ]; $resource->file_dependency = $properties[ 'file_dependency' ];
if (isset($properties[ 'tpl_function' ])) {
$tpl->tpl_function = array_merge($tpl->tpl_function, $properties[ 'tpl_function' ]);
}
} }
return $is_valid && !function_exists($properties[ 'unifunc' ]); return $is_valid && !function_exists($properties[ 'unifunc' ]);
} }
@@ -571,7 +556,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/ */
public function _cleanUp() public function _cleanUp()
{ {
$this->tpl_function = array();
$this->startRenderCallbacks = array(); $this->startRenderCallbacks = array();
$this->endRenderCallbacks = array(); $this->endRenderCallbacks = array();
$this->inheritance = null; $this->inheritance = null;

View File

@@ -526,7 +526,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
} }
// compile the smarty tag (required compile classes to compile the tag are auto loaded) // compile the smarty tag (required compile classes to compile the tag are auto loaded)
if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) { if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
if (isset($this->parent_compiler->template->tpl_function[ $tag ])) { if (isset($this->parent_compiler->tpl_function[ $tag ])) {
// template defined by {template} tag // template defined by {template} tag
$args[ '_attr' ][ 'name' ] = "'" . $tag . "'"; $args[ '_attr' ][ 'name' ] = "'" . $tag . "'";
$_output = $this->callTagCompiler('call', $args, $parameter); $_output = $this->callTagCompiler('call', $args, $parameter);

View File

@@ -167,9 +167,6 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
if ($_template->caching && $this->has_nocache_code) { if ($_template->caching && $this->has_nocache_code) {
$_template->cached->hashes[ $this->nocache_hash ] = true; $_template->cached->hashes[ $this->nocache_hash ] = true;
} }
if (isset($_template->parent) && $_template->parent->_objType == 2 && !empty($_template->tpl_function)) {
$_template->parent->tpl_function = array_merge($_template->parent->tpl_function, $_template->tpl_function);
}
if ($_template->smarty->debugging) { if ($_template->smarty->debugging) {
$_template->smarty->_debug->end_render($_template); $_template->smarty->_debug->end_render($_template);
} }