mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-02 17:34:26 +02:00
Fixed cacheability of block plugins.
This commit is contained in:
19
src/BlockHandler/Base.php
Normal file
19
src/BlockHandler/Base.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Smarty\BlockHandler;
|
||||
|
||||
use Smarty\Template;
|
||||
|
||||
abstract class Base implements BlockHandlerInterface {
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $cacheable = true;
|
||||
|
||||
abstract public function handle($params, $content, Template $template, &$repeat);
|
||||
|
||||
public function isCacheable(): bool {
|
||||
return $this->cacheable;
|
||||
}
|
||||
}
|
@@ -6,4 +6,5 @@ use Smarty\Template;
|
||||
|
||||
interface BlockHandlerInterface {
|
||||
public function handle($params, $content, Template $template, &$repeat);
|
||||
public function isCacheable(): bool;
|
||||
}
|
@@ -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) {
|
||||
|
@@ -107,4 +107,7 @@ class TextFormat implements BlockHandlerInterface {
|
||||
}
|
||||
}
|
||||
|
||||
public function isCacheable(): bool {
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -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 = "<?php \$_block_repeat=true;
|
||||
if (!" . $this->getIsCallableCode($tag, $function) .") {\nthrow new \\Smarty\\Exception('block tag \'{$tag}\' not callable or registered');\n}\n
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
@@ -34,4 +34,11 @@ class ObjectMethodBlockCompiler extends BlockCompiler {
|
||||
return "{$callbackObject}->{$function}";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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) {
|
||||
|
@@ -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'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user