From 431d77505f1daac45451db4dcdb8f316591aefe2 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Tue, 24 Jan 2023 10:16:00 +0100 Subject: [PATCH] Fixed all scope assignment bugs --- CHANGELOG.md | 4 ++ src/Compile/Base.php | 3 +- src/Compile/Tag/Assign.php | 9 +---- src/Compile/Tag/IncludeTag.php | 2 +- .../TemplateSource/X_Scopes/ScopeTest.php | 40 ++++--------------- .../function.checkconfigvar.php | 2 +- 6 files changed, 17 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3318960a..53c95da5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- Template variable scope bubbling has been simplified and made more consistent + ### Removed - Removed support for $cache_attrs for registered plugins - Removed support for undocumented {make_nocache} tag - Removed support for deprecated {insert} tag, the 'insert' plugin type and the associated $smarty->trusted_dir variable - Removed the undocumented {block_parent} and {parent} alternatives to {$smarty.block.parent} - Removed the undocumented {block_child} and {child} alternatives to {$smarty.block.child} +- Removed support for loading config files into a non-local scope using {config_load} from a template ### Fixed - `$smarty->muteUndefinedOrNullWarnings()` now also mutes PHP7 notices for undefined array indexes [#736](https://github.com/smarty-php/smarty/issues/736) diff --git a/src/Compile/Base.php b/src/Compile/Base.php index 90e1b347..adee289a 100644 --- a/src/Compile/Base.php +++ b/src/Compile/Base.php @@ -194,7 +194,7 @@ abstract class Base implements CompilerInterface { * @return int * @throws Exception */ - protected function convertScope($scope, $invalidScopes = []): int { + protected function convertScope($scope): int { static $scopes = [ 'local' => Data::SCOPE_LOCAL, // current scope @@ -211,7 +211,6 @@ abstract class Base implements CompilerInterface { return (int) $_scopeName; } - $_scopeName = trim($_scopeName, '\'"'); if (isset($scopes[$_scopeName])) { return $scopes[$_scopeName]; } diff --git a/src/Compile/Tag/Assign.php b/src/Compile/Tag/Assign.php index 1b7c3ce7..f53bdf33 100644 --- a/src/Compile/Tag/Assign.php +++ b/src/Compile/Tag/Assign.php @@ -78,11 +78,6 @@ class Assign extends Base } else { $_scope = isset($_attr['scope']) ? $this->convertScope($_attr['scope']) : null; } - // optional parameter - $_params = ''; - if ($_nocache) { - $_params .= ' ,' . var_export($_nocache, true); - } if (isset($parameter[ 'smarty_internal_index' ])) { $output = @@ -91,9 +86,9 @@ class Assign extends Base $output .= "settype(\$_tmp_array, 'array');\n"; $output .= "}\n"; $output .= "\$_tmp_array{$parameter['smarty_internal_index']} = {$_attr['value']};\n"; - $output .= "\$_smarty_tpl->assign({$_var}, \$_tmp_array{$_params}, false, " . var_export($_scope, true) . ");?>"; + $output .= "\$_smarty_tpl->assign({$_var}, \$_tmp_array, " . var_export($_nocache, true) . ", " . var_export($_scope, true) . ");?>"; } else { - $output = "assign({$_var}, {$_attr['value']}{$_params}, false, " . var_export($_scope, true) . ");?>"; + $output = "assign({$_var}, {$_attr['value']}, " . var_export($_nocache, true) . ", " . var_export($_scope, true) . ");?>"; } return $output; } diff --git a/src/Compile/Tag/IncludeTag.php b/src/Compile/Tag/IncludeTag.php index 06750534..8f100795 100644 --- a/src/Compile/Tag/IncludeTag.php +++ b/src/Compile/Tag/IncludeTag.php @@ -107,7 +107,7 @@ class IncludeTag extends Base { $variable_template = true; } // scope setup - $_scope = isset($_attr['scope']) ? $this->convertScope($_attr['scope'], [Data::SCOPE_LOCAL]) : 0; + $_scope = isset($_attr['scope']) ? $this->convertScope($_attr['scope']) : 0; // assume caching is off $_caching = Smarty::CACHING_OFF; diff --git a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php index 7c8e3957..4f805fae 100644 --- a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php +++ b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php @@ -130,7 +130,7 @@ class ScopeTest extends PHPUnit_Smarty } /* - * Data provider für testAssignScope + * Data provider for testAssignScope */ public function dataTestAssignScope() { @@ -216,9 +216,6 @@ class ScopeTest extends PHPUnit_Smarty /** * Test scope - * - * - * * @dataProvider dataTestIncludeScope */ public function testIncludeScope($code, $useSmarty, $result, $testName, $testNumber = null) @@ -267,12 +264,12 @@ class ScopeTest extends PHPUnit_Smarty '.tpl:$foo =\'newvar\'#test_scope.tpl:$foo =\'newvar\'#data:$foo =\'newvar\'#global:$foo =\'global\'', 'root scope / no smarty', $i++], ['{include \'test_scope_assign.tpl\' scope=global}', true, - '#test_scope_assign.tpl:$foo =\'newvar\'#testIncludeScope_' . $i . - '.tpl:$foo =\'newvar\'#test_scope.tpl:$foo =\'newvar\'#data:$foo =\'data\'#global:$foo =\'global\'', + '#test_scope_assign.tpl:$foo =\'data\'#testIncludeScope_' . $i . + '.tpl:$foo =\'data\'#test_scope.tpl:$foo =\'data\'#data:$foo =\'data\'#global:$foo =\'newvar\'', 'global scope', $i++], ['{include \'test_scope_pluginassign.tpl\' scope=global}', true, - '#test_scope_pluginassign.tpl:$foo =\'newvar\'#testIncludeScope_' . $i . - '.tpl:$foo =\'newvar\'#test_scope.tpl:$foo =\'newvar\'#data:$foo =\'data\'#global:$foo =\'newvar\'', + '#test_scope_pluginassign.tpl:$foo =\'data\'#testIncludeScope_' . $i . + '.tpl:$foo =\'data\'#test_scope.tpl:$foo =\'data\'#data:$foo =\'data\'#global:$foo =\'newvar\'', 'pluginassign global', $i++], ['{include \'test_scope_assign_noscope.tpl\' scope=root}', true, '#test_scope_assign_noscope.tpl:$foo =\'newvar\'#testIncludeScope_' . $i . @@ -283,9 +280,6 @@ class ScopeTest extends PHPUnit_Smarty /** * Test scope - * - * @not runInSeparateProcess - * * @dataProvider dataTestConfigScope */ public function testConfigScope($code, $useSmarty, $result, $testName, $testNumber) @@ -294,7 +288,7 @@ class ScopeTest extends PHPUnit_Smarty $this->makeTemplateFile($file, $code . '{checkconfigvar var=foo}'); $this->smarty->configLoad('smarty.conf'); $data = $this->smarty->createData($useSmarty ? $this->smarty : null); - $this->smarty->assign('file', $file); + $data->assign('file', $file); $data->configLoad('data.conf'); $tpl = $this->smarty->createTemplate('scope_tag.tpl', $data); $this->assertEquals( @@ -305,7 +299,7 @@ class ScopeTest extends PHPUnit_Smarty } /* - * Data provider für testConfigScope + * Data provider for testConfigScope */ public function dataTestConfigScope() { @@ -318,26 +312,8 @@ class ScopeTest extends PHPUnit_Smarty */ return [ ['{config_load \'template.conf\'}', 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\'#global:$foo =\'smarty\'', '', $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++,], ]; } diff --git a/tests/UnitTests/__shared/PHPunitplugins/function.checkconfigvar.php b/tests/UnitTests/__shared/PHPunitplugins/function.checkconfigvar.php index bc2452ef..94fcc2c9 100644 --- a/tests/UnitTests/__shared/PHPunitplugins/function.checkconfigvar.php +++ b/tests/UnitTests/__shared/PHPunitplugins/function.checkconfigvar.php @@ -30,7 +30,7 @@ function smarty_function_checkconfigvar($params, $template) $output .= "#{$ptr->getSource()->name}:\${$var} ="; $output .= $ptr->hasConfigVariable($var) ? preg_replace('/\s/', '', var_export($ptr->getConfigVariable($var), true)) : 'null'; $ptr = $ptr->parent; - } elseif (in_array('data', $types) && !($ptr instanceof Template || $ptr instanceof Smarty)) { + } elseif (in_array('data', $types) && !($ptr instanceof Template || $ptr instanceof \Smarty\Smarty)) { $output .= "#data:\${$var} ="; $output .= $ptr->hasConfigVariable($var) ? preg_replace('/\s/', '', var_export($ptr->getConfigVariable($var), true)) : 'null'; $ptr = $ptr->parent;