added object-callbacks for all types of plugins and filters

(except for resources)
This commit is contained in:
messju
2003-04-15 10:41:13 +00:00
parent 423c1a2eae
commit e6db82c9cc
3 changed files with 72 additions and 27 deletions

4
NEWS
View File

@@ -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 <labels> to html_checkboxes and html_radios (Philippe, messju) - added <labels> to html_checkboxes and html_radios (Philippe, messju)
- added "labels"-options to turn off labels in html_checkboxes and _radios - added "labels"-options to turn off labels in html_checkboxes and _radios
(messju) (messju)

View File

@@ -820,7 +820,8 @@ class Smarty
*/ */
function register_prefilter($function) function register_prefilter($function)
{ {
$this->_plugins['prefilter'][$function] $_name = (is_array($function)) ? $function[1] : $function;
$this->_plugins['prefilter'][$_name]
= array($function, null, null, false); = array($function, null, null, false);
} }
@@ -842,7 +843,8 @@ class Smarty
*/ */
function register_postfilter($function) function register_postfilter($function)
{ {
$this->_plugins['postfilter'][$function] $_name = (is_array($function)) ? $function[1] : $function;
$this->_plugins['postfilter'][$_name]
= array($function, null, null, false); = array($function, null, null, false);
} }
@@ -864,7 +866,8 @@ class Smarty
*/ */
function register_outputfilter($function) function register_outputfilter($function)
{ {
$this->_plugins['outputfilter'][$function] $_name = (is_array($function)) ? $function[1] : $function;
$this->_plugins['outputfilter'][$_name]
= array($function, null, null, false); = array($function, null, null, false);
} }
@@ -919,8 +922,8 @@ class Smarty
$_auto_id = $this->_get_auto_id($cache_id, $compile_id); $_auto_id = $this->_get_auto_id($cache_id, $compile_id);
if (!empty($this->cache_handler_func)) { if (!empty($this->cache_handler_func)) {
$_funcname = $this->cache_handler_func; return call_user_func_array($this->cache_handler_func,
return $_funcname('clear', $this, $dummy, $tpl_file, $cache_id, $compile_id); array('clear', &$this, &$dummy, $tpl_file, $cache_id, $compile_id));
} else { } else {
return $this->_rm_auto($this->cache_dir, $tpl_file, $_auto_id, $exp_time); 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) function clear_all_cache($exp_time = null)
{ {
if (!empty($this->cache_handler_func)) { if (!empty($this->cache_handler_func)) {
$funcname = $this->cache_handler_func; call_user_func_array($this->cache_handler_func,
return $funcname('clear', $this, $dummy); array('clear', &$this, &$dummy));
} else { } else {
return $this->_rm_auto($this->cache_dir,null,null,$exp_time); return $this->_rm_auto($this->cache_dir,null,null,$exp_time);
} }
@@ -1167,7 +1170,7 @@ class Smarty
ob_end_clean(); ob_end_clean();
foreach ((array)$this->_plugins['outputfilter'] as $output_filter) { 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)) { if (!empty($this->cache_handler_func)) {
// use cache_handler function // use cache_handler function
$_funcname = $this->cache_handler_func; call_user_func_array($this->cache_handler_func,
return $_funcname('write', $this, $results, $tpl_file, $cache_id, $compile_id); array('write', &$this, &$results, $tpl_file, $cache_id, $compile_id));
} else { } else {
// use local cache file // use local cache file
$_auto_id = $this->_get_auto_id($cache_id, $compile_id); $_auto_id = $this->_get_auto_id($cache_id, $compile_id);
@@ -2319,8 +2322,8 @@ class Smarty
if (!empty($this->cache_handler_func)) { if (!empty($this->cache_handler_func)) {
// use cache_handler function // use cache_handler function
$_funcname = $this->cache_handler_func; call_user_func_array($this->cache_handler_func,
$_funcname('read', $this, $results, $tpl_file, $cache_id, $compile_id); array('read', &$this, &$results, $tpl_file, $cache_id, $compile_id));
} else { } else {
// use local cache file // use local cache file
$_auto_id = $this->_get_auto_id($cache_id, $compile_id); $_auto_id = $this->_get_auto_id($cache_id, $compile_id);
@@ -2460,7 +2463,7 @@ class Smarty
*/ */
if (isset($plugin)) { if (isset($plugin)) {
if (!$plugin[3]) { 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__); $this->_trigger_fatal_error("[plugin] $type '$name' is not implemented", $tpl_file, $tpl_line, __FILE__, __LINE__);
} else { } else {
$plugin[1] = $tpl_file; $plugin[1] = $tpl_file;
@@ -2496,7 +2499,7 @@ class Smarty
include_once $plugin_file; include_once $plugin_file;
$plugin_func = 'smarty_' . $type . '_' . $name; $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__); $this->_trigger_fatal_error("[plugin] function $plugin_func() not found in $plugin_file", $tpl_file, $tpl_line, __FILE__, __LINE__);
continue; continue;
} }
@@ -2697,6 +2700,16 @@ class Smarty
} }
return false; 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);
}
/**#@-*/ /**#@-*/
} }

View File

@@ -228,8 +228,8 @@ class Smarty_Compiler extends Smarty {
if (count($this->_plugins['prefilter']) > 0) { if (count($this->_plugins['prefilter']) > 0) {
foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) { foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
if ($prefilter === false) continue; if ($prefilter === false) continue;
if ($prefilter[3] || function_exists($prefilter[0])) { if ($prefilter[3] || $this->_plugin_implementation_exists($prefilter[0])) {
$template_source = $prefilter[0]($template_source, $this); $template_source = call_user_func($prefilter[0], $template_source, $this);
$this->_plugins['prefilter'][$filter_name][3] = true; $this->_plugins['prefilter'][$filter_name][3] = true;
} else { } else {
$this->_trigger_fatal_error("[plugin] prefilter '$filter_name' is not implemented"); $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) { if (count($this->_plugins['postfilter']) > 0) {
foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) { foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
if ($postfilter === false) continue; if ($postfilter === false) continue;
if ($postfilter[3] || function_exists($postfilter[0])) { if ($postfilter[3] || $this->_plugin_implementation_exists($postfilter[0])) {
$template_compiled = $postfilter[0]($template_compiled, $this); $template_compiled = call_user_func($postfilter[0], $template_compiled, $this);
$this->_plugins['postfilter'][$filter_name][3] = true; $this->_plugins['postfilter'][$filter_name][3] = true;
} else { } else {
$this->_trigger_fatal_error("Smarty plugin error: postfilter '$filter_name' is not implemented"); $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])) { if (isset($this->_plugins['compiler'][$tag_command])) {
$found = true; $found = true;
$plugin_func = $this->_plugins['compiler'][$tag_command][0]; $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"; $message = "compiler function '$tag_command' is not implemented";
$have_function = false; $have_function = false;
} }
@@ -534,7 +534,7 @@ class Smarty_Compiler extends Smarty {
include_once $plugin_file; include_once $plugin_file;
$plugin_func = 'smarty_compiler_' . $tag_command; $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"; $message = "plugin function $plugin_func() not found in $plugin_file\n";
$have_function = false; $have_function = false;
} else { } else {
@@ -550,7 +550,7 @@ class Smarty_Compiler extends Smarty {
*/ */
if ($found) { if ($found) {
if ($have_function) { if ($have_function) {
$output = '<?php ' . $plugin_func($tag_args, $this) . ' ?>'; $output = '<?php ' . call_user_func($plugin_func, $tag_args, $this) . ' ?>';
} else { } else {
$this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__); $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])) { if (isset($this->_plugins['block'][$tag_command])) {
$found = true; $found = true;
$plugin_func = $this->_plugins['block'][$tag_command][0]; $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"; $message = "block function '$tag_command' is not implemented";
$have_function = false; $have_function = false;
} }
@@ -634,10 +634,10 @@ class Smarty_Compiler extends Smarty {
$arg_list[] = "'$arg_name' => $arg_value"; $arg_list[] = "'$arg_name' => $arg_value";
} }
$output = "<?php \$this->_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 = "<?php \$this->_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 { } else {
$output = "<?php \$this->_block_content = ob_get_contents(); ob_end_clean(); "; $output = "<?php \$this->_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 != '') { if($tag_modifier != '') {
$this->_parse_modifiers($out_tag_text, $tag_modifier); $this->_parse_modifiers($out_tag_text, $tag_modifier);
} }
@@ -671,7 +671,7 @@ class Smarty_Compiler extends Smarty {
$arg_list[] = "'$arg_name' => $arg_value"; $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 != '') { if($tag_modifier != '') {
$this->_parse_modifiers($return, $tag_modifier); $this->_parse_modifiers($return, $tag_modifier);
@@ -1781,6 +1781,34 @@ class Smarty_Compiler extends Smarty {
return $compiled_ref; 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 * load pre- and post-filters