From e6db82c9cc66b3796135e6467b6f5d58f2fe32ec Mon Sep 17 00:00:00 2001 From: messju Date: Tue, 15 Apr 2003 10:41:13 +0000 Subject: [PATCH] added object-callbacks for all types of plugins and filters (except for resources) --- NEWS | 4 +++ libs/Smarty.class.php | 43 ++++++++++++++++++---------- libs/Smarty_Compiler.class.php | 52 ++++++++++++++++++++++++++-------- 3 files changed, 72 insertions(+), 27 deletions(-) diff --git a/NEWS b/NEWS index 636f9b35..ec5a0d23 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ + - added possibility to register function-callbacks as "array(&$obj, 'method)" + this affects register_function(), -block, -compiler_function, -modifier, + -prefilter, -postfilter, -outputfilter-functions() and $cache_handler_func + (messju) - added to html_checkboxes and html_radios (Philippe, messju) - added "labels"-options to turn off labels in html_checkboxes and _radios (messju) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 035dbcf6..d7e94b35 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -820,7 +820,8 @@ class Smarty */ function register_prefilter($function) { - $this->_plugins['prefilter'][$function] + $_name = (is_array($function)) ? $function[1] : $function; + $this->_plugins['prefilter'][$_name] = array($function, null, null, false); } @@ -842,7 +843,8 @@ class Smarty */ function register_postfilter($function) { - $this->_plugins['postfilter'][$function] + $_name = (is_array($function)) ? $function[1] : $function; + $this->_plugins['postfilter'][$_name] = array($function, null, null, false); } @@ -864,7 +866,8 @@ class Smarty */ function register_outputfilter($function) { - $this->_plugins['outputfilter'][$function] + $_name = (is_array($function)) ? $function[1] : $function; + $this->_plugins['outputfilter'][$_name] = array($function, null, null, false); } @@ -919,8 +922,8 @@ class Smarty $_auto_id = $this->_get_auto_id($cache_id, $compile_id); if (!empty($this->cache_handler_func)) { - $_funcname = $this->cache_handler_func; - return $_funcname('clear', $this, $dummy, $tpl_file, $cache_id, $compile_id); + return call_user_func_array($this->cache_handler_func, + array('clear', &$this, &$dummy, $tpl_file, $cache_id, $compile_id)); } else { return $this->_rm_auto($this->cache_dir, $tpl_file, $_auto_id, $exp_time); } @@ -936,8 +939,8 @@ class Smarty function clear_all_cache($exp_time = null) { if (!empty($this->cache_handler_func)) { - $funcname = $this->cache_handler_func; - return $funcname('clear', $this, $dummy); + call_user_func_array($this->cache_handler_func, + array('clear', &$this, &$dummy)); } else { return $this->_rm_auto($this->cache_dir,null,null,$exp_time); } @@ -1167,7 +1170,7 @@ class Smarty ob_end_clean(); foreach ((array)$this->_plugins['outputfilter'] as $output_filter) { - $_smarty_results = $output_filter[0]($_smarty_results, $this); + $_smarty_results = call_user_func_array($output_filter[0], array($_smarty_results, &$this)); } } @@ -2283,8 +2286,8 @@ class Smarty if (!empty($this->cache_handler_func)) { // use cache_handler function - $_funcname = $this->cache_handler_func; - return $_funcname('write', $this, $results, $tpl_file, $cache_id, $compile_id); + call_user_func_array($this->cache_handler_func, + array('write', &$this, &$results, $tpl_file, $cache_id, $compile_id)); } else { // use local cache file $_auto_id = $this->_get_auto_id($cache_id, $compile_id); @@ -2319,8 +2322,8 @@ class Smarty if (!empty($this->cache_handler_func)) { // use cache_handler function - $_funcname = $this->cache_handler_func; - $_funcname('read', $this, $results, $tpl_file, $cache_id, $compile_id); + call_user_func_array($this->cache_handler_func, + array('read', &$this, &$results, $tpl_file, $cache_id, $compile_id)); } else { // use local cache file $_auto_id = $this->_get_auto_id($cache_id, $compile_id); @@ -2447,7 +2450,7 @@ class Smarty foreach ($plugins as $plugin_info) { list($type, $name, $tpl_file, $tpl_line, $delayed_loading) = $plugin_info; $plugin = &$this->_plugins[$type][$name]; - + /* * We do not load plugin more than once for each instance of Smarty. * The following code checks for that. The plugin can also be @@ -2460,7 +2463,7 @@ class Smarty */ if (isset($plugin)) { if (!$plugin[3]) { - if (!function_exists($plugin[0])) { + if (!$this->_plugin_implementation_exists($plugin[0])) { $this->_trigger_fatal_error("[plugin] $type '$name' is not implemented", $tpl_file, $tpl_line, __FILE__, __LINE__); } else { $plugin[1] = $tpl_file; @@ -2496,7 +2499,7 @@ class Smarty include_once $plugin_file; $plugin_func = 'smarty_' . $type . '_' . $name; - if (!function_exists($plugin_func)) { + if (!$this->_plugin_implementation_exists($plugin_func)) { $this->_trigger_fatal_error("[plugin] function $plugin_func() not found in $plugin_file", $tpl_file, $tpl_line, __FILE__, __LINE__); continue; } @@ -2697,6 +2700,16 @@ class Smarty } return false; } + + /** + * check if the function or method exists + * @return bool + */ + function _plugin_implementation_exists($function) + { + return (is_array($function)) ? + method_exists($function[0], $function[1]) : function_exists($function); + } /**#@-*/ } diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 0eddeeb5..bf0a690d 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -228,8 +228,8 @@ class Smarty_Compiler extends Smarty { if (count($this->_plugins['prefilter']) > 0) { foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) { if ($prefilter === false) continue; - if ($prefilter[3] || function_exists($prefilter[0])) { - $template_source = $prefilter[0]($template_source, $this); + if ($prefilter[3] || $this->_plugin_implementation_exists($prefilter[0])) { + $template_source = call_user_func($prefilter[0], $template_source, $this); $this->_plugins['prefilter'][$filter_name][3] = true; } else { $this->_trigger_fatal_error("[plugin] prefilter '$filter_name' is not implemented"); @@ -326,8 +326,8 @@ class Smarty_Compiler extends Smarty { if (count($this->_plugins['postfilter']) > 0) { foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) { if ($postfilter === false) continue; - if ($postfilter[3] || function_exists($postfilter[0])) { - $template_compiled = $postfilter[0]($template_compiled, $this); + if ($postfilter[3] || $this->_plugin_implementation_exists($postfilter[0])) { + $template_compiled = call_user_func($postfilter[0], $template_compiled, $this); $this->_plugins['postfilter'][$filter_name][3] = true; } else { $this->_trigger_fatal_error("Smarty plugin error: postfilter '$filter_name' is not implemented"); @@ -519,7 +519,7 @@ class Smarty_Compiler extends Smarty { if (isset($this->_plugins['compiler'][$tag_command])) { $found = true; $plugin_func = $this->_plugins['compiler'][$tag_command][0]; - if (!function_exists($plugin_func)) { + if (!$this->_plugin_implementation_exists($plugin_func)) { $message = "compiler function '$tag_command' is not implemented"; $have_function = false; } @@ -534,7 +534,7 @@ class Smarty_Compiler extends Smarty { include_once $plugin_file; $plugin_func = 'smarty_compiler_' . $tag_command; - if (!function_exists($plugin_func)) { + if (!$this->_plugin_implementation_exists($plugin_func)) { $message = "plugin function $plugin_func() not found in $plugin_file\n"; $have_function = false; } else { @@ -550,7 +550,7 @@ class Smarty_Compiler extends Smarty { */ if ($found) { if ($have_function) { - $output = ''; + $output = ''; } else { $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__); } @@ -588,7 +588,7 @@ class Smarty_Compiler extends Smarty { if (isset($this->_plugins['block'][$tag_command])) { $found = true; $plugin_func = $this->_plugins['block'][$tag_command][0]; - if (!function_exists($plugin_func)) { + if (!$this->_plugin_implementation_exists($plugin_func)) { $message = "block function '$tag_command' is not implemented"; $have_function = false; } @@ -634,10 +634,10 @@ class Smarty_Compiler extends Smarty { $arg_list[] = "'$arg_name' => $arg_value"; } - $output = "_tag_stack[] = array('$tag_command', array(".implode(',', (array)$arg_list).")); \$this->_plugins['block']['$tag_command'][0](array(".implode(',', (array)$arg_list)."), null, \$this); ob_start(); ?>"; + $output = "_tag_stack[] = array('$tag_command', array(".implode(',', (array)$arg_list).")); ".$this->_compile_plugin_call('block', $tag_command).'(array('.implode(',', (array)$arg_list)."), null, \$this); ob_start(); ?>"; } else { $output = "_block_content = ob_get_contents(); ob_end_clean(); "; - $out_tag_text = "\$this->_plugins['block']['$tag_command'][0](\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$this->_block_content, \$this)"; + $out_tag_text = $this->_compile_plugin_call('block', $tag_command)."(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$this->_block_content, \$this)"; if($tag_modifier != '') { $this->_parse_modifiers($out_tag_text, $tag_modifier); } @@ -671,8 +671,8 @@ class Smarty_Compiler extends Smarty { $arg_list[] = "'$arg_name' => $arg_value"; } - $return = "\$this->_plugins['function']['$tag_command'][0](array(".implode(',', (array)$arg_list)."), \$this)"; - + $return = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', (array)$arg_list)."), \$this)"; + if($tag_modifier != '') { $this->_parse_modifiers($return, $tag_modifier); } @@ -1781,6 +1781,34 @@ class Smarty_Compiler extends Smarty { return $compiled_ref; } + /** + * compiles call to plugin of type $type with name $name + * returns a string containing the function-name or method call + * without the paramter-list that would have follow to make the + * call valid php-syntax + * + * @param string $type + * @param string $name + */ + function _compile_plugin_call($type, $name) { + if (isset($this->_plugins[$type][$name])) { + /* plugin loaded */ + if (is_array($this->_plugins[$type][$name][0])) { + /* method callback */ + return "\$this->_plugins['$type']['$name'][0][0]->".$this->_plugins[$type][$name][0][1]; + + } else { + /* function callback */ + return $this->_plugins[$type][$name][0]; + + } + } else { + /* plugin not loaded -> auto-loadable-plugin */ + return 'smarty_'.$type.'_'.$name; + + } + } + /** * load pre- and post-filters