From ffa270c395b915f90f3e2a05df5c31492c6af51b Mon Sep 17 00:00:00 2001 From: uwetews Date: Thu, 4 Aug 2016 23:10:26 +0200 Subject: [PATCH] 04.08.2015 - improvement move template function source parameter into extension --- change_log.txt | 3 ++ libs/Smarty.class.php | 2 +- .../smarty_internal_compile_function.php | 27 ++++------ .../smarty_internal_runtime_tplfunction.php | 52 ++++++++++++++++--- libs/sysplugins/smarty_internal_template.php | 24 ++------- .../smarty_internal_templatecompilerbase.php | 2 +- libs/sysplugins/smarty_template_compiled.php | 3 -- 7 files changed, 64 insertions(+), 49 deletions(-) diff --git a/change_log.txt b/change_log.txt index 0e29a182..93608c87 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@  ===== 3.1.30-dev ===== (xx.xx.xx) + 04.08.2015 + - improvement move template function source parameter into extension + 26.07.2016 - optimization unneeded loading of compiled resource diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 43088c11..b0ecc0ba 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.30-dev/(88)'; + const SMARTY_VERSION = '3.1.30-dev/89'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_function.php b/libs/sysplugins/smarty_internal_compile_function.php index bb1e498a..c861443a 100644 --- a/libs/sysplugins/smarty_internal_compile_function.php +++ b/libs/sysplugins/smarty_internal_compile_function.php @@ -53,7 +53,6 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase */ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) { - $compiler->loopNesting ++; // check and get attributes $_attr = $this->getAttributes($compiler, $args); @@ -62,8 +61,7 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase } unset($_attr[ 'nocache' ]); $_name = trim($_attr[ 'name' ], "'\""); - $compiler->parent_compiler->tpl_function[ $_name ] = - $compiler->parent_compiler->template->tpl_function[ $_name ] = array(); + $compiler->parent_compiler->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); @@ -101,20 +99,13 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase */ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) { - $compiler->loopNesting --; $this->compiler = $compiler; $saved_data = $this->closeTag($compiler, array('function')); $_attr = $saved_data[ 0 ]; $_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->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(); + $compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid; $_parameter = $_attr; unset($_parameter[ 'name' ]); // 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}"; $_funcNameCaching = $_funcName . '_nocache'; if ($compiler->template->compiled->has_nocache_code) { - $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = - $compiler->parent_compiler->template->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching; + $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching; $output = " \$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}%%*/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\";?>"; $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}%%*/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 .= "template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n"; $output .= "}\n}\n"; @@ -172,8 +162,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase array($this, 'removeNocache'), $_functionCode->to_smarty_php($compiler->parser))); } - $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = - $compiler->parent_compiler->template->tpl_function[ $_name ][ 'call_name' ] = $_funcName; + $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $_funcName; $output = "tpl_function[ $name ])) { + if (isset($this->tplFunctions[ $name ])) { if (!$tpl->caching || ($tpl->caching && $nocache)) { - $function = $tpl->tpl_function[ $name ][ 'call_name' ]; + $function = $this->tplFunctions[ $name ][ 'call_name' ]; } else { - if (isset($tpl->tpl_function[ $name ][ 'call_name_caching' ])) { - $function = $tpl->tpl_function[ $name ][ 'call_name_caching' ]; + if (isset($this->tplFunctions[ $name ][ 'call_name_caching' ])) { + $function = $this->tplFunctions[ $name ][ 'call_name_caching' ]; } else { - $function = $tpl->tpl_function[ $name ][ 'call_name' ]; + $function = $this->tplFunctions[ $name ][ 'call_name' ]; } } if (function_exists($function)) { @@ -49,6 +56,39 @@ class Smarty_Internal_Runtime_TplFunction 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 @@ -61,7 +101,7 @@ class Smarty_Internal_Runtime_TplFunction */ public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function) { - $funcParam = $tpl->tpl_function[ $_name ]; + $funcParam = $this->tplFunctions[ $_name ]; if (is_file($funcParam[ 'compiled_filepath' ])) { // read compiled file $code = file_get_contents($funcParam[ 'compiled_filepath' ]); diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 8fa2f438..de307d70 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -74,13 +74,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase */ public $templateId = null; - /** - * Known template functions - * - * @var array - */ - public $tpl_function = array(); - /** * Scope in which variables shall be assigned * @@ -220,15 +213,10 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase } } 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 ($tmp1 as $name => $tmp) { - foreach ($tmp as $type => $data) { - $this->parent->compiled->required_plugins[ $code ][ $name ][ $type ] = $data; - } + foreach ($this->compiled->required_plugins as $code => $tmp1) { + foreach ($tmp1 as $name => $tmp) { + foreach ($tmp as $type => $data) { + $this->parent->compiled->required_plugins[ $code ][ $name ][ $type ] = $data; } } } @@ -483,9 +471,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase $resource->has_nocache_code = $properties[ 'has_nocache_code' ]; // $tpl->compiled->nocache_hash = $properties['nocache_hash']; $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' ]); } @@ -571,7 +556,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase */ public function _cleanUp() { - $this->tpl_function = array(); $this->startRenderCallbacks = array(); $this->endRenderCallbacks = array(); $this->inheritance = null; diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 9ce910fd..053c853b 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -526,7 +526,7 @@ 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->template->tpl_function[ $tag ])) { + if (isset($this->parent_compiler->tpl_function[ $tag ])) { // template defined by {template} tag $args[ '_attr' ][ 'name' ] = "'" . $tag . "'"; $_output = $this->callTagCompiler('call', $args, $parameter); diff --git a/libs/sysplugins/smarty_template_compiled.php b/libs/sysplugins/smarty_template_compiled.php index be46529f..4de3aac6 100644 --- a/libs/sysplugins/smarty_template_compiled.php +++ b/libs/sysplugins/smarty_template_compiled.php @@ -167,9 +167,6 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base if ($_template->caching && $this->has_nocache_code) { $_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) { $_template->smarty->_debug->end_render($_template); }