diff --git a/src/BlockHandler/Base.php b/src/BlockHandler/Base.php new file mode 100644 index 00000000..e194f67b --- /dev/null +++ b/src/BlockHandler/Base.php @@ -0,0 +1,19 @@ +cacheable; + } +} \ No newline at end of file diff --git a/src/BlockHandler/BlockHandlerInterface.php b/src/BlockHandler/BlockHandlerInterface.php index 918a0bb6..9aa744ec 100644 --- a/src/BlockHandler/BlockHandlerInterface.php +++ b/src/BlockHandler/BlockHandlerInterface.php @@ -6,4 +6,5 @@ use Smarty\Template; interface BlockHandlerInterface { public function handle($params, $content, Template $template, &$repeat); + public function isCacheable(): bool; } \ No newline at end of file diff --git a/src/BlockHandler/BlockPluginWrapper.php b/src/BlockHandler/BlockPluginWrapper.php index 48f6d2c5..47005945 100644 --- a/src/BlockHandler/BlockPluginWrapper.php +++ b/src/BlockHandler/BlockPluginWrapper.php @@ -4,12 +4,13 @@ namespace Smarty\BlockHandler; use Smarty\Template; -class BlockPluginWrapper implements BlockHandlerInterface { +class BlockPluginWrapper extends Base { private $callback; - public function __construct($callback) { + public function __construct($callback, bool $cacheable = true) { $this->callback = $callback; + $this->cacheable = $cacheable; } public function handle($params, $content, Template $template, &$repeat) { diff --git a/src/BlockHandler/TextFormat.php b/src/BlockHandler/TextFormat.php index af545fa9..9cd804bb 100644 --- a/src/BlockHandler/TextFormat.php +++ b/src/BlockHandler/TextFormat.php @@ -107,4 +107,7 @@ class TextFormat implements BlockHandlerInterface { } } + public function isCacheable(): bool { + return true; + } } \ No newline at end of file diff --git a/src/Compile/BlockCompiler.php b/src/Compile/BlockCompiler.php index 0c409101..373a21b2 100644 --- a/src/Compile/BlockCompiler.php +++ b/src/Compile/BlockCompiler.php @@ -13,6 +13,7 @@ namespace Smarty\Compile; use Smarty\Compiler\Template; use Smarty\CompilerException; use Smarty\Exception; +use Smarty\Smarty; /** * Smarty Internal Plugin Compile Block Plugin Class @@ -61,6 +62,18 @@ class BlockCompiler extends Base { return $output; } + /** + * Returns true if this block is cacheable. + * + * @param Smarty $smarty + * @param $function + * + * @return bool + */ + protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool { + return $smarty->getBlockHandler($function)->isCacheable(); + } + /** * Returns the code used for the isset check * @@ -101,6 +114,10 @@ class BlockCompiler extends Base { unset($_attr['nocache']); $_params = 'array(' . implode(',', $this->formatParamsArray($_attr)) . ')'; + if (!$this->blockIsCacheable($compiler->getSmarty(), $function)) { + $compiler->tag_nocache = true; + } + // compile code $output = "getIsCallableCode($tag, $function) .") {\nthrow new \\Smarty\\Exception('block tag \'{$tag}\' not callable or registered');\n}\n diff --git a/src/Compile/DefaultHandlerBlockCompiler.php b/src/Compile/DefaultHandlerBlockCompiler.php index 034ea04e..394a6e76 100644 --- a/src/Compile/DefaultHandlerBlockCompiler.php +++ b/src/Compile/DefaultHandlerBlockCompiler.php @@ -18,4 +18,12 @@ class DefaultHandlerBlockCompiler extends BlockCompiler { return "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->getCallback(" . var_export($function, true) . ", 'block')"; } + + /** + * @inheritDoc + */ + protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool { + return true; + } + } \ No newline at end of file diff --git a/src/Compile/ObjectMethodBlockCompiler.php b/src/Compile/ObjectMethodBlockCompiler.php index f8fba227..6c05c422 100644 --- a/src/Compile/ObjectMethodBlockCompiler.php +++ b/src/Compile/ObjectMethodBlockCompiler.php @@ -34,4 +34,11 @@ class ObjectMethodBlockCompiler extends BlockCompiler { return "{$callbackObject}->{$function}"; } + /** + * @inheritDoc + */ + protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool { + return true; + } + } diff --git a/src/Extension/BCPluginsAdapter.php b/src/Extension/BCPluginsAdapter.php index 950043d6..ccfc40cf 100644 --- a/src/Extension/BCPluginsAdapter.php +++ b/src/Extension/BCPluginsAdapter.php @@ -57,8 +57,9 @@ class BCPluginsAdapter extends Base { return null; } $callback = $plugin[0]; + $cacheable = (bool) $plugin[1] ?? true; - return new BlockPluginWrapper($callback); + return new BlockPluginWrapper($callback, $cacheable); } public function getModifierCallback(string $modifierName) { diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php index 94f3c689..8836a9e2 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php @@ -152,10 +152,16 @@ class RegisterBlockTest extends PHPUnit_Smarty { $this->smarty->caching = 1; $this->smarty->cache_lifetime = 1000; - $this->smarty->assign('x', 4); - $this->smarty->assign('y', 40); - $this->smarty->assign('z', 400); - $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false); + $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false); + + $this->smarty->assign('x', 3); + $this->smarty->assign('y', 30); + $this->smarty->assign('z', 300); + $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl')); + + $this->smarty->assign('x', 4); + $this->smarty->assign('y', 40); + $this->smarty->assign('z', 400); $this->assertEquals('3 40 300', $this->smarty->fetch('test_register_block.tpl')); } @@ -209,20 +215,23 @@ class RegisterBlockTest extends PHPUnit_Smarty $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl')); } - /** - * - * - * - */ public function testRegisterBlockCaching4Wrapper() { + $this->cleanDirs(); + $this->smarty->caching = 1; $this->smarty->cache_lifetime = 1000; + $this->smarty->registerPlugin('block', 'testblock', 'myblockcache'); + + $this->smarty->assign('x', 3); + $this->smarty->assign('y', 30); + $this->smarty->assign('z', 300); + $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl')); + $this->smarty->assign('x', 4); $this->smarty->assign('y', 40); $this->smarty->assign('z', 400); - $this->smarty->registerPlugin('block', 'testblock', 'myblockcache', false); - $this->assertEquals('3 40 300', $this->smarty->fetch('test_register_block.tpl')); + $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl')); } /**