diff --git a/change_log.txt b/change_log.txt index 8f37b13b..602f3f72 100644 --- a/change_log.txt +++ b/change_log.txt @@ -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 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 3805c770..a9af2d8e 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -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 diff --git a/libs/sysplugins/smarty_internal_compile_call.php b/libs/sysplugins/smarty_internal_compile_call.php index 33480dee..739df5ec 100644 --- a/libs/sysplugins/smarty_internal_compile_call.php +++ b/libs/sysplugins/smarty_internal_compile_call.php @@ -79,10 +79,10 @@ class Smarty_Internal_Compile_Call extends Smarty_Internal_CompileBase // was there an assign attribute if (isset($_assign)) { $_output = - "ext->_tplFunction->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});\n\$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n"; + "smarty->ext->_tplFunction->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});\n\$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n"; } else { $_output = - "ext->_tplFunction->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});?>\n"; + "smarty->ext->_tplFunction->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});?>\n"; } return $_output; } diff --git a/libs/sysplugins/smarty_internal_compile_function.php b/libs/sysplugins/smarty_internal_compile_function.php index c861443a..b7cd9e1c 100644 --- a/libs/sysplugins/smarty_internal_compile_function.php +++ b/libs/sysplugins/smarty_internal_compile_function.php @@ -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}%%*/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 = "template->compiled->nocache_hash}%%*/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 .= "template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n"; $output .= "}\n}\n"; diff --git a/libs/sysplugins/smarty_internal_runtime_codeframe.php b/libs/sysplugins/smarty_internal_runtime_codeframe.php index 66faf67b..e363712a 100644 --- a/libs/sysplugins/smarty_internal_runtime_codeframe.php +++ b/libs/sysplugins/smarty_internal_runtime_codeframe.php @@ -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 diff --git a/libs/sysplugins/smarty_internal_runtime_tplfunction.php b/libs/sysplugins/smarty_internal_runtime_tplfunction.php index 5cedca13..a8deaf28 100644 --- a/libs/sysplugins/smarty_internal_runtime_tplfunction.php +++ b/libs/sysplugins/smarty_internal_runtime_tplfunction.php @@ -1,7 +1,7 @@ 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 null|string $name template function name + * @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) { diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 5096eee3..0efcd698 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -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 * diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 13a7019c..158d654c 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -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);