From 1d28939be9d867cda8234cefbc259245669cf4c4 Mon Sep 17 00:00:00 2001 From: tDidierjean Date: Thu, 12 Apr 2012 13:54:43 +0000 Subject: [PATCH] [*] BO : #PI-124 - lazy registering of smarty functions --- config/smarty.config.inc.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/config/smarty.config.inc.php b/config/smarty.config.inc.php index d35990861..710fbe860 100644 --- a/config/smarty.config.inc.php +++ b/config/smarty.config.inc.php @@ -153,7 +153,9 @@ function smartyRegisterFunction($smarty, $type, $function, $params) { if (!in_array($type, array('function', 'modifier'))) return false; - $smarty->registerPlugin($type, $function, $params); + + // SmartyLazyRegister allows to only load external class when they are needed + $smarty->registerPlugin($type, $function, array(new SmartyLazyRegister($function, $params), $params[1])); } function smartyHook($params, &$smarty) @@ -165,3 +167,24 @@ function smartyHook($params, &$smarty) return Hook::exec($params['h'], $hook_params); } } + +/** + * Used to delay loading of external classes with smarty->register_plugin + */ +class SmartyLazyRegister +{ + public function __construct($function, $params) + { + $this->function = $function; + $this->params = $params; + } + + public function __call($name, $arguments) + { + // case 1: call to static method - case 2 : call to static function + if (is_array($this->params)) + return call_user_func_array($this->params[0].'::'.$this->params[1], array($arguments[0], &$arguments[1])); + else + return call_user_func_array($this->params, array($arguments[0], &$arguments[1])); + } +}