Simply the "isFresh" method by not including the first param anymore. Fix a couple of unit tests by respecting tag_nocache set by nocache vars used in a tag.

This commit is contained in:
Simon Wisselink
2023-01-22 22:44:45 +01:00
parent 2d2d052557
commit bdb379d8fb
5 changed files with 57 additions and 47 deletions

View File

@@ -62,8 +62,7 @@ class CodeFrame
str_replace('*/', '* /', $this->_template->getSource()->filepath) str_replace('*/', '* /', $this->_template->getSource()->filepath)
); );
$output .= "/* @var \\Smarty\\Template \$_smarty_tpl */\n"; $output .= "/* @var \\Smarty\\Template \$_smarty_tpl */\n";
$dec = "\$_smarty_tpl->isFresh(\$_smarty_tpl, " . var_export($properties, true) . ',' . $dec = "\$_smarty_tpl->isFresh(" . var_export($properties, true) . ',' . ($cache ? 'true' : 'false') . ')';
($cache ? 'true' : 'false') . ')';
$output .= "if ({$dec}) {\n"; $output .= "if ({$dec}) {\n";
$output .= "function {$properties['unifunc']} (\\Smarty\\Template \$_smarty_tpl) {\n"; $output .= "function {$properties['unifunc']} (\\Smarty\\Template \$_smarty_tpl) {\n";
if (!$cache && !empty($compiler->tpl_function)) { if (!$cache && !empty($compiler->tpl_function)) {

View File

@@ -1104,7 +1104,7 @@ class Template extends BaseCompiler {
// compile built-in tags // compile built-in tags
if ($tagCompiler = $this->getTagCompiler($tag)) { if ($tagCompiler = $this->getTagCompiler($tag)) {
if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) { if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
$this->tag_nocache = !$tagCompiler->isCacheable(); $this->tag_nocache = $this->tag_nocache | !$tagCompiler->isCacheable();
$_output = $tagCompiler->compile($args, $this, $parameter); $_output = $tagCompiler->compile($args, $this, $parameter);
if ($_output !== false) { if ($_output !== false) {
return $this->has_code && $_output !== true ? $_output : null; return $this->has_code && $_output !== true ? $_output : null;

View File

@@ -340,42 +340,41 @@ class Template extends TemplateBase {
* - Decode saved properties from compiled template and cache files * - Decode saved properties from compiled template and cache files
* - Check if compiled or cache file is valid * - Check if compiled or cache file is valid
* *
* @param \Smarty\Template $tpl
* @param array $properties special template properties * @param array $properties special template properties
* @param bool $cache flag if called from cache file * @param bool $cache flag if called from cache file
* *
* @return bool flag if compiled or cache file is valid * @return bool flag if compiled or cache file is valid
* @throws \Smarty\Exception * @throws \Smarty\Exception
*/ */
public function isFresh(Template $tpl, $properties, $cache = false) { public function isFresh($properties, $cache = false) {
// on cache resources other than file check version stored in cache code // on cache resources other than file check version stored in cache code
if (!isset($properties['version']) || \Smarty\Smarty::SMARTY_VERSION !== $properties['version']) { if (!isset($properties['version']) || \Smarty\Smarty::SMARTY_VERSION !== $properties['version']) {
if ($cache) { if ($cache) {
$tpl->getSmarty()->clearAllCache(); $this->getSmarty()->clearAllCache();
} else { } else {
$tpl->getSmarty()->clearCompiledTemplate(); $this->getSmarty()->clearCompiledTemplate();
} }
return false; return false;
} }
$is_valid = true; $is_valid = true;
if (!empty($properties['file_dependency']) if (!empty($properties['file_dependency'])
&& ((!$cache && $tpl->compile_check) || $tpl->compile_check === \Smarty\Smarty::COMPILECHECK_ON) && ((!$cache && $this->compile_check) || $this->compile_check === \Smarty\Smarty::COMPILECHECK_ON)
) { ) {
// check file dependencies at compiled code // check file dependencies at compiled code
foreach ($properties['file_dependency'] as $_file_to_check) { foreach ($properties['file_dependency'] as $_file_to_check) {
if ($_file_to_check[2] === 'file' || $_file_to_check[2] === 'php') { if ($_file_to_check[2] === 'file' || $_file_to_check[2] === 'php') {
if ($tpl->getSource()->filepath === $_file_to_check[0]) { if ($this->getSource()->filepath === $_file_to_check[0]) {
// do not recheck current template // do not recheck current template
continue; continue;
//$mtime = $tpl->getSource()->getTimeStamp(); //$mtime = $this->getSource()->getTimeStamp();
} else { } else {
// file and php types can be checked without loading the respective resource handlers // file and php types can be checked without loading the respective resource handlers
$mtime = is_file($_file_to_check[0]) ? filemtime($_file_to_check[0]) : false; $mtime = is_file($_file_to_check[0]) ? filemtime($_file_to_check[0]) : false;
} }
} else { } else {
$handler = \Smarty\Resource\BasePlugin::load($tpl->getSmarty(), $_file_to_check[2]); $handler = \Smarty\Resource\BasePlugin::load($this->getSmarty(), $_file_to_check[2]);
if ($handler->checkTimestamps()) { if ($handler->checkTimestamps()) {
$source = Source::load($tpl, $tpl->getSmarty(), $_file_to_check[0]); $source = Source::load($this, $this->getSmarty(), $_file_to_check[0]);
$mtime = $source->getTimeStamp(); $mtime = $source->getTimeStamp();
} else { } else {
continue; continue;
@@ -389,17 +388,17 @@ class Template extends TemplateBase {
} }
if ($cache) { if ($cache) {
// CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc // CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
if ($tpl->caching === \Smarty\Smarty::CACHING_LIFETIME_SAVED && $properties['cache_lifetime'] >= 0 if ($this->caching === \Smarty\Smarty::CACHING_LIFETIME_SAVED && $properties['cache_lifetime'] >= 0
&& (time() > ($tpl->getCached()->timestamp + $properties['cache_lifetime'])) && (time() > ($this->getCached()->timestamp + $properties['cache_lifetime']))
) { ) {
$is_valid = false; $is_valid = false;
} }
$tpl->getCached()->cache_lifetime = $properties['cache_lifetime']; $this->getCached()->cache_lifetime = $properties['cache_lifetime'];
$tpl->getCached()->valid = $is_valid; $this->getCached()->valid = $is_valid;
$generatedFile = $tpl->getCached(); $generatedFile = $this->getCached();
} else { } else {
$tpl->mustCompile = !$is_valid; $this->mustCompile = !$is_valid;
$generatedFile = $tpl->getCompiled(); $generatedFile = $this->getCompiled();
$generatedFile->includes = $properties['includes'] ?? []; $generatedFile->includes = $properties['includes'] ?? [];
} }
if ($is_valid) { if ($is_valid) {

View File

@@ -118,6 +118,12 @@ class CompileAppendTest extends PHPUnit_Smarty
$this->smarty->setCompileId('1'); $this->smarty->setCompileId('1');
$this->smarty->setCaching(1); $this->smarty->setCaching(1);
$this->smarty->setTemplateDir('./templates_tmp'); $this->smarty->setTemplateDir('./templates_tmp');
$this->smarty->assign('foo', 'bar',true);
$this->assertEquals($result,
$this->smarty->fetch($file),
"testSpacing - {$file}");
$this->smarty->assign('foo', 'foo',true); $this->smarty->assign('foo', 'foo',true);
$this->assertEquals(str_replace('bar','foo',$result), $this->assertEquals(str_replace('bar','foo',$result),
$this->smarty->fetch($file), $this->smarty->fetch($file),

View File

@@ -292,10 +292,10 @@ class ScopeTest extends PHPUnit_Smarty
{ {
$file = "testConfigScope_{$testNumber}.tpl"; $file = "testConfigScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code . '{checkconfigvar var=foo}'); $this->makeTemplateFile($file, $code . '{checkconfigvar var=foo}');
$this->smarty->assignGlobal('file', $file); $this->smarty->configLoad('smarty.conf');
$this->smarty->configLoad('smarty.conf'); $data = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data = $this->smarty->createData($useSmarty ? $this->smarty : null); $this->smarty->assign('file', $file);
$data->configLoad('data.conf'); $data->configLoad('data.conf');
$tpl = $this->smarty->createTemplate('scope_tag.tpl', $data); $tpl = $this->smarty->createTemplate('scope_tag.tpl', $data);
$this->assertEquals( $this->assertEquals(
'#' . $file . $result, '#' . $file . $result,
@@ -309,30 +309,36 @@ class ScopeTest extends PHPUnit_Smarty
*/ */
public function dataTestConfigScope() public function dataTestConfigScope()
{ {
$i = 0; $i = 0;
/* /*
* Code * Code
* use Smarty object * use Smarty object
* result * result
* test name * test name
*/ */
return array(array('{config_load \'template.conf\'}', true, return [
':$foo =\'newvar\'#scope_include.tpl:$foo =\'data\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', ['{config_load \'template.conf\'}', true,
'', $i ++,), array('{config_load \'template.conf\' scope=local}', true, ':$foo =\'newvar\'#scope_include.tpl:$foo =\'data\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'',
':$foo =\'newvar\'#scope_include.tpl:$foo =\'data\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', '', $i++,],
'', $i ++,), array('{config_load \'template.conf\' scope=parent}', true, ['{config_load \'template.conf\' scope=local}', true,
':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', ':$foo =\'newvar\'#scope_include.tpl:$foo =\'data\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'',
'', $i ++,), '', $i++,],
array('{config_load \'template.conf\' scope=tpl_root}', true, ['{config_load \'template.conf\' scope=parent}', true,
':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'data\'', ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'',
'', $i ++,), array('{config_load \'template.conf\' scope=root}', true, '', $i++,],
':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'newvar\'', ['{config_load \'template.conf\' scope=tpl_root}', true,
'', $i ++,), array('{config_load \'template.conf\' scope=root}', false, ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'data\'',
':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'newvar\'', '', $i++,],
'no smarty', $i ++,), ['{config_load \'template.conf\' scope=root}', true,
array('{config_load \'template.conf\' scope=global}', false, ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'newvar\'',
':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'data\'#global:$foo =\'newvar\'', '', $i++,],
'no smarty', $i ++,),); ['{config_load \'template.conf\' scope=root}', false,
':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'newvar\'',
'no smarty', $i++,],
['{config_load \'template.conf\' scope=global}', false,
':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'data\'#global:$foo =\'newvar\'',
'no smarty', $i++,],
];
} }
/** /**