From 14bbffd584c9e9e8311d6fafa5b1e71d24159147 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Wed, 4 Jan 2023 13:19:25 +0100 Subject: [PATCH] move runPluginFromDefaultHandler to new Runtime class --- .../DefaultHandlerFunctionCallCompiler.php | 2 +- src/Runtime/DefaultPluginHandlerRuntime.php | 48 +++++++++++++++++++ src/Smarty.php | 29 ++--------- 3 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 src/Runtime/DefaultPluginHandlerRuntime.php diff --git a/src/Compile/DefaultHandlerFunctionCallCompiler.php b/src/Compile/DefaultHandlerFunctionCallCompiler.php index dce5fceb..bc88648d 100644 --- a/src/Compile/DefaultHandlerFunctionCallCompiler.php +++ b/src/Compile/DefaultHandlerFunctionCallCompiler.php @@ -44,7 +44,7 @@ class DefaultHandlerFunctionCallCompiler extends Base { } $_params = 'array(' . implode(',', $_paramsArray) . ')'; - $output = "\$_smarty_tpl->smarty->runPluginFromDefaultHandler(" . var_export($function, true) . + $output = "\$_smarty_tpl->smarty->getRuntime('DefaultPluginHandler')->runPlugin(" . var_export($function, true) . ",'function',$_params, \$_smarty_tpl)"; if (!empty($parameter['modifierlist'])) { diff --git a/src/Runtime/DefaultPluginHandlerRuntime.php b/src/Runtime/DefaultPluginHandlerRuntime.php new file mode 100644 index 00000000..e53de7b4 --- /dev/null +++ b/src/Runtime/DefaultPluginHandlerRuntime.php @@ -0,0 +1,48 @@ +defaultPluginHandler = $defaultPluginHandler; + } + + /** + * @throws Exception + */ + public function runPlugin($tag, $plugin_type, $params, \Smarty\Template $template) { + + if ($this->defaultPluginHandler === null) { + return false; + } + + $callback = null; + + // these are not used here + $script = null; + $cacheable = null; + + if (call_user_func_array( + $this->defaultPluginHandler, + [ + $tag, + $plugin_type, + null, // This used to pass $this->template, but this parameter has been removed in 5.0 + &$callback, + &$script, + &$cacheable, + ] + ) && $callback) { + return $callback($params, $template); + } + throw new Exception("Default plugin handler: Returned callback for '{$tag}' not callable at runtime"); + } +} \ No newline at end of file diff --git a/src/Smarty.php b/src/Smarty.php index 8285df3f..763f695e 100644 --- a/src/Smarty.php +++ b/src/Smarty.php @@ -2075,6 +2075,10 @@ class Smarty extends \Smarty\TemplateBase return $this->runtimes[$type] = new \Smarty\Runtime\MakeNocacheRuntime(); case 'TplFunction': return $this->runtimes[$type] = new \Smarty\Runtime\TplFunctionRuntime(); + case 'DefaultPluginHandler': + return $this->runtimes[$type] = new \Smarty\Runtime\DefaultPluginHandlerRuntime( + $this->getDefaultPluginHandlerFunc() + ); } throw new \Smarty\Exception('Trying to load invalid runtime ' . $type); @@ -2103,31 +2107,6 @@ class Smarty extends \Smarty\TemplateBase return $this->default_plugin_handler_func; } - public function runPluginFromDefaultHandler($tag, $plugin_type, $params, \Smarty\Template $template) { - $defaultPluginHandlerFunc = $this->getDefaultPluginHandlerFunc(); - - $callback = null; - - // these are not used here - $script = null; - $cacheable = null; - - if (call_user_func_array( - $defaultPluginHandlerFunc, - [ - $tag, - $plugin_type, - null, // This used to pass $this->template, but this parameter has been removed in 5.0 - &$callback, - &$script, - &$cacheable, - ] - ) && $callback) { - return $callback($params, $template); - } - throw new Exception("Default plugin handler: Returned callback for '{$tag}' not callable at runtime"); - } - /** * load a filter of specified type and name *