From 8f8d462b8a214875cef2fc9667c7c92181b32ede Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Sat, 8 Nov 2014 17:19:53 +0100 Subject: [PATCH] add code of template functions called in nocache mode dynamically to cache file --- .../smarty_internal_function_call_handler.php | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/libs/sysplugins/smarty_internal_function_call_handler.php b/libs/sysplugins/smarty_internal_function_call_handler.php index a8d68c85..7d835990 100644 --- a/libs/sysplugins/smarty_internal_function_call_handler.php +++ b/libs/sysplugins/smarty_internal_function_call_handler.php @@ -8,7 +8,10 @@ */ /** - * This class does call function defined with the {function} tag + * This class does handles template functions defined with the {function} tag missing in cache file. + * It can happen when the template function was called with the nocache option or within a nocache section. + * The template function will be loaded from it's compiled template file, executed and added to the cache file + * for later use. * * @package Smarty * @subpackage PluginsInternal @@ -21,7 +24,7 @@ class Smarty_Internal_Function_Call_Handler * * @param string $_name template function name * @param Smarty_Internal_Template $_smarty_tpl - * @param $_function + * @param string $_function PHP function name * @param array $_params Smarty variables passed as call parameter * @param bool $_nocache nocache flag * @@ -30,24 +33,37 @@ class Smarty_Internal_Function_Call_Handler public static function call($_name, Smarty_Internal_Template $_smarty_tpl, $_function, $_params, $_nocache) { $funcParam = $_smarty_tpl->properties['tpl_function']['param'][$_name]; - $code = file_get_contents($funcParam['compiled_filepath']); - if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) { - $output = "\n"; - $output .= $match[0]; - $output .= "?>\n"; - } - unset($code, $match); - eval($output); - if (function_exists($_function)) { - $_function ($_smarty_tpl, $_params); - $tplPtr = $_smarty_tpl; - while (isset($tplPtr->parent) && !isset($tplPtr->parent->cached)) { - $tplPtr = $tplPtr->parent; + if (is_file($funcParam['compiled_filepath'])) { + // read compiled file + $code = file_get_contents($funcParam['compiled_filepath']); + // grab template function + if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) { + $code = "\n"; + $code .= $match[0]; + $code .= "?>\n"; + unset($match); + $output = ''; + // make PHP function known + eval($code); + if (function_exists($_function)) { + // call template function + $_function ($_smarty_tpl, $_params); + // search cache file template + $tplPtr = $_smarty_tpl; + while (!isset($tplPtr->cached) && isset($tplPtr->parent)) { + $tplPtr = $tplPtr->parent; + } + // add template function code to cache file + if (isset($tplPtr->cached)) { + $cache = $tplPtr->cached; + $content = $cache->read($tplPtr); + if ($content) { + $cache->write($tplPtr, $content . "parent->cached)) { - $cached = $tplPtr->parent->cached; - } - return true; } return false; }