diff --git a/change_log.txt b/change_log.txt index 2905b4ea..6ac13400 100644 --- a/change_log.txt +++ b/change_log.txt @@ -2,6 +2,7 @@ 21.06.2015 - optimization of template/config file normalization - optimization of directory handling / build realpath + - optimization of filter execution 19.06.2015 - improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 6bf45598..b49e307e 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/3'; + const SMARTY_VERSION = '3.1.28-dev/4'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_filter_handler.php b/libs/sysplugins/smarty_internal_filter_handler.php index 754f924f..0a6db7d8 100644 --- a/libs/sysplugins/smarty_internal_filter_handler.php +++ b/libs/sysplugins/smarty_internal_filter_handler.php @@ -32,35 +32,38 @@ class Smarty_Internal_Filter_Handler */ public static function runFilter($type, $content, Smarty_Internal_Template $template) { - $output = $content; // loop over autoload filters of specified type if (!empty($template->smarty->autoload_filters[$type])) { foreach ((array) $template->smarty->autoload_filters[$type] as $name) { $plugin_name = "Smarty_{$type}filter_{$name}"; - if ($template->smarty->loadPlugin($plugin_name)) { + if (function_exists($plugin_name)) { + $callback = $plugin_name; + } elseif (class_exists($plugin_name, false) && is_callable(array($plugin_name, 'execute'))) { + $callback = array($plugin_name, 'execute'); + } elseif ($template->smarty->loadPlugin($plugin_name, false)) { if (function_exists($plugin_name)) { // use loaded Smarty2 style plugin - $output = $plugin_name($output, $template); - } elseif (class_exists($plugin_name, false)) { + $callback = $plugin_name; + } elseif (class_exists($plugin_name, false) && is_callable(array($plugin_name, 'execute'))) { // loaded class of filter plugin - if (!is_callable(array($plugin_name, 'execute'))) { - throw new SmartyException("Auto load {$type}-filter plugin method \"{$plugin_name}::execute\" not callable"); - } - $output = call_user_func(array($plugin_name, 'execute'), $output, $template); + $callback = array($plugin_name, 'execute'); + } else { + throw new SmartyException("Auto load {$type}-filter plugin method \"{$plugin_name}::execute\" not callable"); } } else { // nothing found, throw exception throw new SmartyException("Unable to auto load {$type}-filter plugin \"{$plugin_name}\""); } + $content = call_user_func($callback, $content, $template); } } - // loop over registerd filters of specified type + // loop over registered filters of specified type if (!empty($template->smarty->registered_filters[$type])) { foreach ($template->smarty->registered_filters[$type] as $key => $name) { - $output = call_user_func($template->smarty->registered_filters[$type][$key], $output, $template); + $content = call_user_func($template->smarty->registered_filters[$type][$key], $content, $template); } } // return filtered output - return $output; + return $content; } }