From ad2703dd756131d3939cabaaee37f3aea3f3c0a7 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Mon, 23 Jan 2023 12:08:36 +0100 Subject: [PATCH] Handle BC registered compilers and missed parameters for openTag and closeTag. --- src/Compile/Tag/ForClose.php | 2 +- src/Compile/Tag/ForTag.php | 2 +- src/Compile/Tag/ForeachClose.php | 2 +- src/Compile/Tag/ForeachTag.php | 2 +- src/Compile/Tag/IfClose.php | 2 +- src/Compile/Tag/IfTag.php | 2 +- src/Compile/Tag/WhileClose.php | 2 +- src/Compile/Tag/WhileTag.php | 2 +- src/Extension/BCPluginsAdapter.php | 21 ++++++++++++++++----- src/Smarty.php | 2 +- 10 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/Compile/Tag/ForClose.php b/src/Compile/Tag/ForClose.php index c3646d5f..bde1a075 100644 --- a/src/Compile/Tag/ForClose.php +++ b/src/Compile/Tag/ForClose.php @@ -41,7 +41,7 @@ class ForClose extends Base { if ($nocache_pushed) { // pop the pushed virtual nocache tag - $this->closeTag('nocache'); + $this->closeTag($compiler, 'nocache'); $compiler->tag_nocache = true; } diff --git a/src/Compile/Tag/ForTag.php b/src/Compile/Tag/ForTag.php index 38266944..8066d83e 100644 --- a/src/Compile/Tag/ForTag.php +++ b/src/Compile/Tag/ForTag.php @@ -90,7 +90,7 @@ class ForTag extends Base { if ($compiler->tag_nocache) { // push a {nocache} tag onto the stack to prevent caching of this for loop - $this->openTag('nocache'); + $this->openTag($compiler, 'nocache'); } $this->openTag($compiler, 'for', ['for', $compiler->tag_nocache]); diff --git a/src/Compile/Tag/ForeachClose.php b/src/Compile/Tag/ForeachClose.php index 7ec2393b..76975452 100644 --- a/src/Compile/Tag/ForeachClose.php +++ b/src/Compile/Tag/ForeachClose.php @@ -36,7 +36,7 @@ class ForeachClose extends Base { if ($nocache_pushed) { // pop the pushed virtual nocache tag - $this->closeTag('nocache'); + $this->closeTag($compiler, 'nocache'); $compiler->tag_nocache = true; } diff --git a/src/Compile/Tag/ForeachTag.php b/src/Compile/Tag/ForeachTag.php index 24c93898..da9712ae 100644 --- a/src/Compile/Tag/ForeachTag.php +++ b/src/Compile/Tag/ForeachTag.php @@ -184,7 +184,7 @@ class ForeachTag extends ForeachSection { if ($compiler->tag_nocache) { // push a {nocache} tag onto the stack to prevent caching of this block - $this->openTag('nocache'); + $this->openTag($compiler, 'nocache'); } // Register tag diff --git a/src/Compile/Tag/IfClose.php b/src/Compile/Tag/IfClose.php index bf092d1f..12f7e442 100644 --- a/src/Compile/Tag/IfClose.php +++ b/src/Compile/Tag/IfClose.php @@ -34,7 +34,7 @@ class IfClose extends Base { if ($nocache_pushed) { // pop the pushed virtual nocache tag - $this->closeTag('nocache'); + $this->closeTag($compiler, 'nocache'); $compiler->tag_nocache = true; } diff --git a/src/Compile/Tag/IfTag.php b/src/Compile/Tag/IfTag.php index a07877c3..84bf477c 100644 --- a/src/Compile/Tag/IfTag.php +++ b/src/Compile/Tag/IfTag.php @@ -26,7 +26,7 @@ class IfTag extends Base { if ($compiler->tag_nocache) { // push a {nocache} tag onto the stack to prevent caching of this block - $this->openTag('nocache'); + $this->openTag($compiler, 'nocache'); } $this->openTag($compiler, 'if', [1, $compiler->tag_nocache]); diff --git a/src/Compile/Tag/WhileClose.php b/src/Compile/Tag/WhileClose.php index fcb690ed..6c45cd72 100644 --- a/src/Compile/Tag/WhileClose.php +++ b/src/Compile/Tag/WhileClose.php @@ -35,7 +35,7 @@ class WhileClose extends Base { if ($nocache_pushed) { // pop the pushed virtual nocache tag - $this->closeTag('nocache'); + $this->closeTag($compiler, 'nocache'); $compiler->tag_nocache = true; } diff --git a/src/Compile/Tag/WhileTag.php b/src/Compile/Tag/WhileTag.php index 8b7aac5f..3df7d197 100644 --- a/src/Compile/Tag/WhileTag.php +++ b/src/Compile/Tag/WhileTag.php @@ -27,7 +27,7 @@ class WhileTag extends Base { if ($compiler->tag_nocache) { // push a {nocache} tag onto the stack to prevent caching of this block - $this->openTag('nocache'); + $this->openTag($compiler, 'nocache'); } $this->openTag($compiler, 'while', $compiler->tag_nocache); diff --git a/src/Extension/BCPluginsAdapter.php b/src/Extension/BCPluginsAdapter.php index ccfc40cf..aa0eefe2 100644 --- a/src/Extension/BCPluginsAdapter.php +++ b/src/Extension/BCPluginsAdapter.php @@ -3,6 +3,7 @@ namespace Smarty\Extension; use Smarty\BlockHandler\BlockPluginWrapper; +use Smarty\Compile\CompilerInterface; use Smarty\Compile\Modifier\BCPluginWrapper as ModifierCompilerPluginWrapper; use Smarty\Compile\Tag\BCPluginWrapper as TagPluginWrapper; use Smarty\Filter\FilterPluginWrapper; @@ -34,9 +35,18 @@ class BCPluginsAdapter extends Base { return null; } - $callback = $plugin[0]; - $cacheable = (bool) $plugin[1] ?? true; - return new TagPluginWrapper($callback, $cacheable); + if (is_callable($plugin[0])) { + $callback = $plugin[0]; + $cacheable = (bool) $plugin[1] ?? true; + return new TagPluginWrapper($callback, $cacheable); + } elseif (class_exists($plugin[0])) { + $compiler = new $plugin[0]; + if ($compiler instanceof CompilerInterface) { + return $compiler; + } + } + + return null; } public function getFunctionHandler(string $functionName): ?\Smarty\FunctionHandler\FunctionHandlerInterface { @@ -172,8 +182,9 @@ class BCPluginsAdapter extends Base { $pluginName = $this->getPluginNameFromFilename($filename); if ($pluginName !== null) { require_once $filename; - if (function_exists($functionName = 'smarty_' . $type . '_' . $pluginName)) { - $this->smarty->registerPlugin($type, $pluginName, $functionName, true, []); + $functionOrClassName = 'smarty_' . $type . '_' . $pluginName; + if (function_exists($functionOrClassName) || class_exists($functionOrClassName)) { + $this->smarty->registerPlugin($type, $pluginName, $functionOrClassName, true, []); } } } diff --git a/src/Smarty.php b/src/Smarty.php index a23071fa..14af6a1e 100644 --- a/src/Smarty.php +++ b/src/Smarty.php @@ -769,7 +769,7 @@ class Smarty extends \Smarty\TemplateBase public function registerPlugin($type, $name, $callback, $cacheable = true) { if (isset($this->registered_plugins[$type][$name])) { throw new Exception("Plugin tag '{$name}' already registered"); - } elseif (!is_callable($callback)) { + } elseif (!is_callable($callback) && !class_exists($callback)) { throw new Exception("Plugin '{$name}' not callable"); } else { $this->registered_plugins[$type][$name] = [$callback, (bool)$cacheable];