move runPluginFromDefaultHandler to new Runtime class

This commit is contained in:
Simon Wisselink
2023-01-04 13:19:25 +01:00
parent 41054cfb05
commit 14bbffd584
3 changed files with 53 additions and 26 deletions

View File

@@ -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'])) {

View File

@@ -0,0 +1,48 @@
<?php
namespace Smarty\Runtime;
use Smarty\Exception;
class DefaultPluginHandlerRuntime {
/**
* @var callable
*/
private $defaultPluginHandler;
public function __construct(?callable $defaultPluginHandler = null) {
$this->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");
}
}

View File

@@ -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
*