From bdb379d8fb4ca5b91c4cba1775003a395073a2bd Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Sun, 22 Jan 2023 22:44:45 +0100 Subject: [PATCH] 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. --- src/Compiler/CodeFrame.php | 3 +- src/Compiler/Template.php | 2 +- src/Template.php | 31 +++++----- .../TagTests/Append/CompileAppendTest.php | 6 ++ .../TemplateSource/X_Scopes/ScopeTest.php | 62 ++++++++++--------- 5 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/Compiler/CodeFrame.php b/src/Compiler/CodeFrame.php index b371e58d..3eee90a7 100644 --- a/src/Compiler/CodeFrame.php +++ b/src/Compiler/CodeFrame.php @@ -62,8 +62,7 @@ class CodeFrame str_replace('*/', '* /', $this->_template->getSource()->filepath) ); $output .= "/* @var \\Smarty\\Template \$_smarty_tpl */\n"; - $dec = "\$_smarty_tpl->isFresh(\$_smarty_tpl, " . var_export($properties, true) . ',' . - ($cache ? 'true' : 'false') . ')'; + $dec = "\$_smarty_tpl->isFresh(" . var_export($properties, true) . ',' . ($cache ? 'true' : 'false') . ')'; $output .= "if ({$dec}) {\n"; $output .= "function {$properties['unifunc']} (\\Smarty\\Template \$_smarty_tpl) {\n"; if (!$cache && !empty($compiler->tpl_function)) { diff --git a/src/Compiler/Template.php b/src/Compiler/Template.php index 8f37deb6..3a5db510 100644 --- a/src/Compiler/Template.php +++ b/src/Compiler/Template.php @@ -1104,7 +1104,7 @@ class Template extends BaseCompiler { // compile built-in tags if ($tagCompiler = $this->getTagCompiler($tag)) { 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); if ($_output !== false) { return $this->has_code && $_output !== true ? $_output : null; diff --git a/src/Template.php b/src/Template.php index 467be43e..85ef2be3 100644 --- a/src/Template.php +++ b/src/Template.php @@ -340,42 +340,41 @@ class Template extends TemplateBase { * - Decode saved properties from compiled template and cache files * - Check if compiled or cache file is valid * - * @param \Smarty\Template $tpl * @param array $properties special template properties * @param bool $cache flag if called from cache file * * @return bool flag if compiled or cache file is valid * @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 if (!isset($properties['version']) || \Smarty\Smarty::SMARTY_VERSION !== $properties['version']) { if ($cache) { - $tpl->getSmarty()->clearAllCache(); + $this->getSmarty()->clearAllCache(); } else { - $tpl->getSmarty()->clearCompiledTemplate(); + $this->getSmarty()->clearCompiledTemplate(); } return false; } $is_valid = true; 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 foreach ($properties['file_dependency'] as $_file_to_check) { 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 continue; - //$mtime = $tpl->getSource()->getTimeStamp(); + //$mtime = $this->getSource()->getTimeStamp(); } else { // 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; } } 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()) { - $source = Source::load($tpl, $tpl->getSmarty(), $_file_to_check[0]); + $source = Source::load($this, $this->getSmarty(), $_file_to_check[0]); $mtime = $source->getTimeStamp(); } else { continue; @@ -389,17 +388,17 @@ class Template extends TemplateBase { } if ($cache) { // 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 - && (time() > ($tpl->getCached()->timestamp + $properties['cache_lifetime'])) + if ($this->caching === \Smarty\Smarty::CACHING_LIFETIME_SAVED && $properties['cache_lifetime'] >= 0 + && (time() > ($this->getCached()->timestamp + $properties['cache_lifetime'])) ) { $is_valid = false; } - $tpl->getCached()->cache_lifetime = $properties['cache_lifetime']; - $tpl->getCached()->valid = $is_valid; - $generatedFile = $tpl->getCached(); + $this->getCached()->cache_lifetime = $properties['cache_lifetime']; + $this->getCached()->valid = $is_valid; + $generatedFile = $this->getCached(); } else { - $tpl->mustCompile = !$is_valid; - $generatedFile = $tpl->getCompiled(); + $this->mustCompile = !$is_valid; + $generatedFile = $this->getCompiled(); $generatedFile->includes = $properties['includes'] ?? []; } if ($is_valid) { diff --git a/tests/UnitTests/TemplateSource/TagTests/Append/CompileAppendTest.php b/tests/UnitTests/TemplateSource/TagTests/Append/CompileAppendTest.php index cb0ab284..dc99abcf 100644 --- a/tests/UnitTests/TemplateSource/TagTests/Append/CompileAppendTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/Append/CompileAppendTest.php @@ -118,6 +118,12 @@ class CompileAppendTest extends PHPUnit_Smarty $this->smarty->setCompileId('1'); $this->smarty->setCaching(1); $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->assertEquals(str_replace('bar','foo',$result), $this->smarty->fetch($file), diff --git a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php index e8fb0bd1..7c8e3957 100644 --- a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php +++ b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php @@ -292,10 +292,10 @@ class ScopeTest extends PHPUnit_Smarty { $file = "testConfigScope_{$testNumber}.tpl"; $this->makeTemplateFile($file, $code . '{checkconfigvar var=foo}'); - $this->smarty->assignGlobal('file', $file); - $this->smarty->configLoad('smarty.conf'); - $data = $this->smarty->createData($useSmarty ? $this->smarty : null); - $data->configLoad('data.conf'); + $this->smarty->configLoad('smarty.conf'); + $data = $this->smarty->createData($useSmarty ? $this->smarty : null); + $this->smarty->assign('file', $file); + $data->configLoad('data.conf'); $tpl = $this->smarty->createTemplate('scope_tag.tpl', $data); $this->assertEquals( '#' . $file . $result, @@ -309,30 +309,36 @@ class ScopeTest extends PHPUnit_Smarty */ public function dataTestConfigScope() { - $i = 0; - /* - * Code - * use Smarty object - * result - * test name - */ - return array(array('{config_load \'template.conf\'}', true, - ':$foo =\'newvar\'#scope_include.tpl:$foo =\'data\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', - '', $i ++,), array('{config_load \'template.conf\' scope=local}', true, - ':$foo =\'newvar\'#scope_include.tpl:$foo =\'data\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', - '', $i ++,), array('{config_load \'template.conf\' scope=parent}', true, - ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', - '', $i ++,), - array('{config_load \'template.conf\' scope=tpl_root}', true, - ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'data\'', - '', $i ++,), array('{config_load \'template.conf\' scope=root}', true, - ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'newvar\'', - '', $i ++,), array('{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 ++,), - array('{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 ++,),); + $i = 0; + /* + * Code + * use Smarty object + * result + * test name + */ + return [ + ['{config_load \'template.conf\'}', true, + ':$foo =\'newvar\'#scope_include.tpl:$foo =\'data\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', + '', $i++,], + ['{config_load \'template.conf\' scope=local}', true, + ':$foo =\'newvar\'#scope_include.tpl:$foo =\'data\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', + '', $i++,], + ['{config_load \'template.conf\' scope=parent}', true, + ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'data\'#data:$foo =\'data\'', + '', $i++,], + ['{config_load \'template.conf\' scope=tpl_root}', true, + ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'data\'', + '', $i++,], + ['{config_load \'template.conf\' scope=root}', true, + ':$foo =\'newvar\'#scope_include.tpl:$foo =\'newvar\'#scope_tag.tpl:$foo =\'newvar\'#data:$foo =\'newvar\'', + '', $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++,], + ]; } /**