diff options
Diffstat (limited to 'lib/smarty3/sysplugins/smarty_internal_runtime_tplfunction.php')
-rw-r--r-- | lib/smarty3/sysplugins/smarty_internal_runtime_tplfunction.php | 86 |
1 files changed, 46 insertions, 40 deletions
diff --git a/lib/smarty3/sysplugins/smarty_internal_runtime_tplfunction.php b/lib/smarty3/sysplugins/smarty_internal_runtime_tplfunction.php index f75028a..e5f8e48 100644 --- a/lib/smarty3/sysplugins/smarty_internal_runtime_tplfunction.php +++ b/lib/smarty3/sysplugins/smarty_internal_runtime_tplfunction.php @@ -1,23 +1,15 @@ <?php /** - * Tplfunc Runtime Methods callTemplateFunction + * TplFunction Runtime Methods callTemplateFunction * * @package Smarty * @subpackage PluginsInternal * @author Uwe Tews - * **/ class Smarty_Internal_Runtime_TplFunction { /** - * Array of source information for known template functions - * - * @var array - */ - private $tplFunctions = array(); - - /** * Call template function * * @param \Smarty_Internal_Template $tpl template object @@ -29,26 +21,28 @@ class Smarty_Internal_Runtime_TplFunction */ public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache) { - if (isset($this->tplFunctions[ $name ])) { + $funcParam = isset($tpl->tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] : + (isset($tpl->smarty->tplFunctions[ $name ]) ? $tpl->smarty->tplFunctions[ $name ] : null); + if (isset($funcParam)) { if (!$tpl->caching || ($tpl->caching && $nocache)) { - $function = $this->tplFunctions[ $name ][ 'call_name' ]; + $function = $funcParam[ 'call_name' ]; } else { - if (isset($this->tplFunctions[ $name ][ 'call_name_caching' ])) { - $function = $this->tplFunctions[ $name ][ 'call_name_caching' ]; + if (isset($funcParam[ 'call_name_caching' ])) { + $function = $funcParam[ 'call_name_caching' ]; } else { - $function = $this->tplFunctions[ $name ][ 'call_name' ]; + $function = $funcParam[ 'call_name' ]; } } if (function_exists($function)) { $this->saveTemplateVariables($tpl, $name); - $function ($tpl, $params); + $function($tpl, $params); $this->restoreTemplateVariables($tpl, $name); return; } // try to load template function dynamically if ($this->addTplFuncToCache($tpl, $name, $function)) { $this->saveTemplateVariables($tpl, $name); - $function ($tpl, $params); + $function($tpl, $params); $this->restoreTemplateVariables($tpl, $name); return; } @@ -59,38 +53,45 @@ class Smarty_Internal_Runtime_TplFunction /** * Register template functions defined by template * - * @param \Smarty_Internal_Template $tpl - * @param array $tplFunctions source information array of template functions defined in template + * @param \Smarty|\Smarty_Internal_Template|\Smarty_Internal_TemplateBase $obj + * @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_TemplateBase $obj, $tplFunctions, $override = true) { - $this->tplFunctions = array_merge($this->tplFunctions, $tplFunctions); - $ptr = $tpl; + $obj->tplFunctions = + $override ? array_merge($obj->tplFunctions, $tplFunctions) : array_merge($tplFunctions, $obj->tplFunctions); // 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; + if ($obj->_isSubTpl()) { + $obj->smarty->ext->_tplFunction->registerTplFunctions($obj->parent, $tplFunctions, false); + } else { + $obj->smarty->tplFunctions = $override ? array_merge($obj->smarty->tplFunctions, $tplFunctions) : + array_merge($tplFunctions, $obj->smarty->tplFunctions); } } /** * 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 ] : + (isset($tpl->smarty->tplFunctions[ $name ]) ? $tpl->smarty->tplFunctions[ $name ] : false); } else { - return $this->tplFunctions; + return empty($tpl->tplFunctions) ? $tpl->smarty->tplFunctions : $tpl->tplFunctions; } } /** - * * Add template function to cache file for nocache calls * * @param Smarty_Internal_Template $tpl @@ -101,7 +102,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,19 +121,24 @@ class Smarty_Internal_Runtime_TplFunction } // add template function code to cache file if (isset($tplPtr->cached)) { - /* @var Smarty_CacheResource $cache */ - $cache = $tplPtr->cached; - $content = $cache->read($tplPtr); + $content = $tplPtr->cached->read($tplPtr); if ($content) { // check if we must update file dependency if (!preg_match("/'{$funcParam['uid']}'(.*?)'nocache_hash'/", $content, $match2)) { $content = preg_replace("/('file_dependency'(.*?)\()/", "\\1{$match1[0]}", $content); } - $tplPtr->smarty->ext->_updateCache->write($cache, $tplPtr, - preg_replace('/\s*\?>\s*$/', "\n", $content) . - "\n" . preg_replace(array('/^\s*<\?php\s+/', - '/\s*\?>\s*$/',), "\n", - $match[ 0 ])); + $tplPtr->smarty->ext->_updateCache->write( + $tplPtr, + preg_replace('/\s*\?>\s*$/', "\n", $content) . + "\n" . preg_replace( + array( + '/^\s*<\?php\s+/', + '/\s*\?>\s*$/', + ), + "\n", + $match[ 0 ] + ) + ); } } return true; @@ -146,7 +152,7 @@ class Smarty_Internal_Runtime_TplFunction * Save current template variables on stack * * @param \Smarty_Internal_Template $tpl - * @param string $name stack name + * @param string $name stack name */ public function saveTemplateVariables(Smarty_Internal_Template $tpl, $name) { @@ -158,7 +164,7 @@ class Smarty_Internal_Runtime_TplFunction * Restore saved variables into template objects * * @param \Smarty_Internal_Template $tpl - * @param string $name stack name + * @param string $name stack name */ public function restoreTemplateVariables(Smarty_Internal_Template $tpl, $name) { |