- optimization move runtime extension for template functions into Smarty objects

This commit is contained in:
uwetews
2016-10-08 06:07:14 +02:00
parent caceefc300
commit 3c07a28434
8 changed files with 39 additions and 35 deletions

View File

@@ -1,4 +1,7 @@
===== 3.1.31-dev ===== (xx.xx.xx)
08.10.2016
- optimization move runtime extension for template functions into Smarty objects
29.09.2016
- improvement new Smarty::$extends_recursion property to disable execution of {extends} in templates called by extends resource
https://github.com/smarty-php/smarty/issues/296

View File

@@ -114,7 +114,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.31-dev/32';
const SMARTY_VERSION = '3.1.31-dev/33';
/**
* define variable scopes

View File

@@ -79,10 +79,10 @@ class Smarty_Internal_Compile_Call extends Smarty_Internal_CompileBase
// was there an assign attribute
if (isset($_assign)) {
$_output =
"<?php ob_start();\n\$_smarty_tpl->ext->_tplFunction->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});\n\$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n";
"<?php ob_start();\n\$_smarty_tpl->smarty->ext->_tplFunction->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});\n\$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n";
} else {
$_output =
"<?php \$_smarty_tpl->ext->_tplFunction->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});?>\n";
"<?php \$_smarty_tpl->smarty->ext->_tplFunction->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});?>\n";
}
return $_output;
}

View File

@@ -141,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 .= "\$params = var_export(\$params, true);\n";
$output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
$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 .= "\\\$_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 .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>";
$compiler->parser->current_buffer->append_subtree($compiler->parser,
new Smarty_Internal_ParseTree_Tag($compiler->parser,
$output));
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
$output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
$output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\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 .= "}\n}\n";

View File

@@ -49,12 +49,12 @@ class Smarty_Internal_Runtime_CodeFrame
$output .= "if ({$dec}) {\n";
$output .= "function {$properties['unifunc']} (Smarty_Internal_Template \$_smarty_tpl) {\n";
if (!$cache && !empty($compiler->tpl_function)) {
$output .= "\$_smarty_tpl->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
$output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
var_export($compiler->tpl_function, true) . ");\n";
}
if ($cache && isset($_template->ext->_tplFunction)) {
$output .= "\$_smarty_tpl->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
var_export($_template->ext->_tplFunction->getTplFunction(), true) . ");\n";
if ($cache && isset($_template->smarty->ext->_tplFunction)) {
$output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
var_export($_template->smarty->ext->_tplFunction->getTplFunction($_template), true) . ");\n";
}
// include code for plugins

View File

@@ -1,7 +1,7 @@
<?php
/**
* Tplfunc Runtime Methods callTemplateFunction
* TplFunction Runtime Methods callTemplateFunction
*
* @package Smarty
* @subpackage PluginsInternal
@@ -10,13 +10,6 @@
**/
class Smarty_Internal_Runtime_TplFunction
{
/**
* Array of source information for known template functions
*
* @var array
*/
private $tplFunctions = array();
/**
* Call template function
*
@@ -29,14 +22,14 @@ class Smarty_Internal_Runtime_TplFunction
*/
public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache)
{
if (isset($this->tplFunctions[ $name ])) {
if (isset($tpl->tplFunctions[ $name ])) {
if (!$tpl->caching || ($tpl->caching && $nocache)) {
$function = $this->tplFunctions[ $name ][ 'call_name' ];
$function = $tpl->tplFunctions[ $name ][ 'call_name' ];
} else {
if (isset($this->tplFunctions[ $name ][ 'call_name_caching' ])) {
$function = $this->tplFunctions[ $name ][ 'call_name_caching' ];
if (isset($tpl->tplFunctions[ $name ][ 'call_name_caching' ])) {
$function = $tpl->tplFunctions[ $name ][ 'call_name_caching' ];
} else {
$function = $this->tplFunctions[ $name ][ 'call_name' ];
$function = $tpl->tplFunctions[ $name ][ 'call_name' ];
}
}
if (function_exists($function)) {
@@ -61,31 +54,31 @@ class Smarty_Internal_Runtime_TplFunction
*
* @param \Smarty_Internal_Template $tpl
* @param array $tplFunctions source information array of template functions defined in template
* @param bool $override if true replace existing functions with same name
*/
public function registerTplFunctions(Smarty_Internal_Template $tpl, $tplFunctions)
public function registerTplFunctions(Smarty_Internal_Template $tpl, $tplFunctions, $override = true)
{
$this->tplFunctions = array_merge($this->tplFunctions, $tplFunctions);
$ptr = $tpl;
$tpl->tplFunctions = $override ? array_merge($tpl->tplFunctions, $tplFunctions) : array_merge($tplFunctions, $tpl->tplFunctions);
// make sure that the template functions are known in parent templates
while ($ptr->_isTplObj() && !isset($ptr->ext->_tplFunction)) {
$ptr->ext->_tplFunction = $this;
$ptr = $ptr->parent;
if ($tpl->_isSubTpl()) {
$tpl->smarty->ext->_tplFunction->registerTplFunctions($tpl->parent,$tplFunctions, false);
}
}
/**
* Return source parameter array for single or all template functions
*
* @param \Smarty_Internal_Template $tpl template object
* @param null|string $name template function name
*
* @return array|bool|mixed
*/
public function getTplFunction($name = null)
public function getTplFunction(Smarty_Internal_Template $tpl, $name = null)
{
if (isset($name)) {
return $this->tplFunctions[ $name ] ? $this->tplFunctions[ $name ] : false;
return isset($tpl->tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] : false;
} else {
return $this->tplFunctions;
return $tpl->tplFunctions;
}
}
@@ -101,7 +94,7 @@ class Smarty_Internal_Runtime_TplFunction
*/
public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function)
{
$funcParam = $this->tplFunctions[ $_name ];
$funcParam = $tpl->tplFunctions[ $_name ];
if (is_file($funcParam[ 'compiled_filepath' ])) {
// read compiled file
$code = file_get_contents($funcParam[ 'compiled_filepath' ]);
@@ -120,7 +113,7 @@ class Smarty_Internal_Runtime_TplFunction
}
// add template function code to cache file
if (isset($tplPtr->cached)) {
/* @var Smarty_CacheResource $cache */
/* @var Smarty_Template_Cached $cache */
$cache = $tplPtr->cached;
$content = $cache->read($tplPtr);
if ($content) {

View File

@@ -81,6 +81,13 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/
public $scope = 0;
/**
* Array of source information for known template functions
*
* @var array
*/
public $tplFunctions = array();
/**
* Flag which is set while rending a cache file
*

View File

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