From 22ce23b47bdc1ae8bcdb583db9a205eced58d3fe Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Wed, 11 Jan 2023 11:22:24 +0100 Subject: [PATCH] Made variable scoping more sensible --- demo/templates/index.tpl | 2 - .../language-function-append.md | 2 +- .../language-function-assign.md | 2 +- src/Compile/Base.php | 10 ++- src/Compile/Tag/ConfigLoad.php | 2 +- src/Data.php | 56 +++++++++--- src/Debug.php | 21 ----- src/Runtime/InheritanceRuntime.php | 2 +- src/Smarty.php | 61 ++++--------- src/Template.php | 25 +++--- src/Template/Cached.php | 4 +- src/Template/Compiled.php | 2 +- src/Template/ResourceBase.php | 7 -- src/TemplateBase.php | 42 --------- .../ConfigFileTests/file/ConfigVarTest.php | 3 +- .../AssignGlobal/AssignGlobalTest.php | 69 --------------- .../AssignGlobal/cache/.gitignore | 2 - .../AssignGlobal/templates_c/.gitignore | 2 - .../TemplateSource/X_Scopes/ScopeTest.php | 88 ++++++++++++++----- .../PHPunitplugins/function.checkvar.php | 11 +-- 20 files changed, 155 insertions(+), 258 deletions(-) delete mode 100644 tests/UnitTests/SmartyMethodsTests/AssignGlobal/AssignGlobalTest.php delete mode 100644 tests/UnitTests/SmartyMethodsTests/AssignGlobal/cache/.gitignore delete mode 100644 tests/UnitTests/SmartyMethodsTests/AssignGlobal/templates_c/.gitignore diff --git a/demo/templates/index.tpl b/demo/templates/index.tpl index 1fbb6d37..1a0f431b 100644 --- a/demo/templates/index.tpl +++ b/demo/templates/index.tpl @@ -11,8 +11,6 @@ The current date and time is {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} - The value of global assigned variable $SCRIPT_NAME is {$SCRIPT_NAME} - Example of accessing server environment variable SERVER_NAME: {$smarty.server.SERVER_NAME} The value of {ldelim}$Name{rdelim} is {$Name} diff --git a/docs/designers/language-builtin-functions/language-function-append.md b/docs/designers/language-builtin-functions/language-function-append.md index 62f2c7e1..0b4c3efa 100644 --- a/docs/designers/language-builtin-functions/language-function-append.md +++ b/docs/designers/language-builtin-functions/language-function-append.md @@ -17,7 +17,7 @@ var string Yes *n/a* The name of the variable being assigned value string Yes *n/a* The value being assigned index string No *n/a* The index for the new array element. If not specified the value is append to the end of the array. - scope string No *n/a* The scope of the assigned variable: \'parent\',\'root\' or \'global\' + scope string No *n/a* The scope of the assigned variable: \'parent\',\'root\' or \'smarty\' **Option Flags:** diff --git a/docs/designers/language-builtin-functions/language-function-assign.md b/docs/designers/language-builtin-functions/language-function-assign.md index e4d50d30..ec44f0e4 100644 --- a/docs/designers/language-builtin-functions/language-function-assign.md +++ b/docs/designers/language-builtin-functions/language-function-assign.md @@ -21,7 +21,7 @@ execution of a template**. ---------------- -------- ---------- --------- ----------------------------------------------------------------------- var string Yes *n/a* The name of the variable being assigned value string Yes *n/a* The value being assigned - scope string No *n/a* The scope of the assigned variable: \'parent\',\'root\' or \'global\' + scope string No *n/a* The scope of the assigned variable: \'parent\',\'root\' or \'smarty\' **Option Flags:** diff --git a/src/Compile/Base.php b/src/Compile/Base.php index 0c216c78..70831e51 100644 --- a/src/Compile/Base.php +++ b/src/Compile/Base.php @@ -238,9 +238,13 @@ abstract class Base implements CompilerInterface { protected function convertScope($_attr, $invalidScopes = []): int { static $scopes = [ - 'local' => Data::SCOPE_LOCAL, 'parent' => Data::SCOPE_PARENT, - 'root' => Data::SCOPE_ROOT, 'global' => Data::SCOPE_GLOBAL, - 'tpl_root' => Data::SCOPE_TPL_ROOT, 'smarty' => Data::SCOPE_SMARTY + 'local' => Data::SCOPE_LOCAL, // current scope + 'parent' => Data::SCOPE_PARENT, // parent scope (definition unclear) + 'tpl_root' => Data::SCOPE_TPL_ROOT, // highest template (keep going up until parent is not a template) + 'root' => Data::SCOPE_ROOT, // highest scope (definition unclear) + 'global' => Data::SCOPE_GLOBAL, // smarty object + + 'smarty' => Data::SCOPE_SMARTY, // @deprecated alias of 'global' ]; if (!isset($_attr['scope'])) { diff --git a/src/Compile/Tag/ConfigLoad.php b/src/Compile/Tag/ConfigLoad.php index cc38a427..2adffe36 100644 --- a/src/Compile/Tag/ConfigLoad.php +++ b/src/Compile/Tag/ConfigLoad.php @@ -78,6 +78,6 @@ class ConfigLoad extends Base { $_scope = $this->convertScope($_attr); } // create config object - return "_loadConfigfile({$conf_file}, {$section});\n?>\n"; + return "configLoad({$conf_file}, {$section});\n?>\n"; } } diff --git a/src/Data.php b/src/Data.php index b5c7e095..cf80fdcc 100644 --- a/src/Data.php +++ b/src/Data.php @@ -72,6 +72,12 @@ abstract class Data $this->_getSmartyObj()->assign($tpl_var, $value); break; case self::SCOPE_TPL_ROOT: + $ptr = $this; + while (isset($ptr->parent) && ($ptr->parent instanceof Template)) { + $ptr = $ptr->parent; + } + $ptr->assign($tpl_var, $value); + break; case self::SCOPE_ROOT: $ptr = $this; while (isset($ptr->parent) && !($ptr->parent instanceof Smarty)) { @@ -82,6 +88,9 @@ abstract class Data case self::SCOPE_PARENT: if ($this->parent) { $this->parent->assign($tpl_var, $value); + } else { + // assign local as fallback + $this->assign($tpl_var, $value); } break; default: @@ -376,21 +385,48 @@ abstract class Data } /** - * Returns a single or all global variables + * load a config file, optionally load just selected sections * - * @api Smarty::getGlobal() + * @param string $config_file filename + * @param mixed $sections array of section names, single + * section or null * - * @param string $varName variable name or null + * @return $this + * @throws \Exception + *@api Smarty::configLoad() + * @link https://www.smarty.net/docs/en/api.config.load.tpl * - * @return string|array variable value or or array of variables - * - * @deprecated since 5.0 */ - public function getGlobal($varName = null) + public function configLoad($config_file, $sections = null) { - trigger_error(__METHOD__ . " is deprecated. Use \\Smarty\\Smarty::getValue() to retrieve a variable " . - " at the Smarty level.", E_USER_DEPRECATED); - return $this->_getSmartyObj()->getValue($varName); + $this->_loadConfigfile($config_file, $sections); + return $this; } + /** + * load a config file, optionally load just selected sections + * + * @param string $config_file filename + * @param mixed $sections array of section names, single + * section or null + + * @returns Template + * @throws \Exception + * @link https://www.smarty.net/docs/en/api.config.load.tpl + * + * @api Smarty::configLoad() + */ + protected function _loadConfigfile($config_file, $sections = null) + { + $smarty = $this->_getSmartyObj(); + + $confObj = new Template($config_file, $smarty, $this, null, null, null, null, true); + $confObj->caching = Smarty::CACHING_OFF; + $confObj->source->config_sections = $sections; + $confObj->compiled = \Smarty\Template\Compiled::load($confObj); + $confObj->compiled->render($confObj); + return $confObj; + } + + } diff --git a/src/Debug.php b/src/Debug.php index 756aa674..59eff06c 100644 --- a/src/Debug.php +++ b/src/Debug.php @@ -292,27 +292,6 @@ class Debug extends Data } } $config_vars = array_merge($parent->config_vars, $config_vars); - } else { - foreach ($this->_getSmartyObj()->getAllGlobalTemplateVars() as $key => $var) { - if (!array_key_exists($key, $tpl_vars)) { - foreach ($var as $varkey => $varvalue) { - if ($varkey === 'value') { - $tpl_vars[ $key ][ $varkey ] = $varvalue; - } else { - if ($varkey === 'nocache') { - if ($varvalue === true) { - $tpl_vars[ $key ][ $varkey ] = $varvalue; - } - } else { - if ($varkey !== 'scope' || $varvalue !== 0) { - $tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue; - } - } - } - } - $tpl_vars[ $key ][ 'scope' ] = 'Global'; - } - } } return (object)array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars); } diff --git a/src/Runtime/InheritanceRuntime.php b/src/Runtime/InheritanceRuntime.php index f7587830..bdec8290 100644 --- a/src/Runtime/InheritanceRuntime.php +++ b/src/Runtime/InheritanceRuntime.php @@ -164,7 +164,7 @@ class InheritanceRuntime { * * @throws Exception */ - public function process( + private function process( Template $tpl, \Smarty\Runtime\Block $block, \Smarty\Runtime\Block $parent = null diff --git a/src/Smarty.php b/src/Smarty.php index f77a8b6d..8a2f7f55 100644 --- a/src/Smarty.php +++ b/src/Smarty.php @@ -972,23 +972,23 @@ class Smarty extends \Smarty\TemplateBase * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template * @param object $parent next higher level of Smarty variables - * @param boolean $do_clone flag is template object shall be cloned * * @return \Smarty\Template template object * @throws \Smarty\Exception */ - public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true) + public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null) { - if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) { + if ((is_object($cache_id) || is_array($cache_id))) { $parent = $cache_id; $cache_id = null; } - if ($parent !== null && is_array($parent)) { + if (is_array($parent)) { $data = $parent; $parent = null; } else { $data = null; } + if (!$this->_templateDirNormalized) { $this->_normalizeTemplateConfig(false); } @@ -1005,6 +1005,9 @@ class Smarty extends \Smarty\TemplateBase $tpl->assign($_key, $_val); } } + + $tpl->tplFunctions = array_merge($parent->tplFunctions ?? [], $tpl->tplFunctions ?? []); + if (!$this->debugging && $this->debugging_ctrl === 'URL') { $tpl->smarty->getDebug()->debugUrl($tpl->smarty); } @@ -2196,51 +2199,19 @@ class Smarty extends \Smarty\TemplateBase $this->cacheResource = $cacheResource; } - /** - * Sets a global variable, available in all templates - * - * @param string $varName - * @param Variable $param - * - * @return void - */ - public function setGlobalVariable(string $varName, Variable $param) { - $this->global_tpl_vars[$varName] = $param; - } - - /** - * Returns all global variables - * - * @return array - */ - public function getAllGlobalTemplateVars() { - return $this->global_tpl_vars; - } - - /** - * Returns a single global variable, or null if not found. - * @param string $varName - * - * @return Variable|null - */ - public function getGlobalVariable(string $varName): ?Variable { - return $this->global_tpl_vars[$varName] ?? null; - } - /** * fetches a rendered Smarty template * * @param string $template the resource handle of the template file or template object * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template - * @param object $parent next higher level of Smarty variables * * @return string rendered template output * @throws Exception * @throws Exception */ - public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null) { - return $this->returnOrCreateTemplate($template)->fetch($cache_id, $compile_id, $parent); + public function fetch($template = null, $cache_id = null, $compile_id = null) { + return $this->returnOrCreateTemplate($template, $cache_id, $compile_id)->fetch(); } /** @@ -2249,13 +2220,12 @@ class Smarty extends \Smarty\TemplateBase * @param string $template the resource handle of the template file or template object * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template - * @param object $parent next higher level of Smarty variables * * @throws \Exception * @throws \Smarty\Exception */ - public function display($template = null, $cache_id = null, $compile_id = null, $parent = null) { - return $this->returnOrCreateTemplate($template)->display($cache_id, $compile_id, $parent); + public function display($template = null, $cache_id = null, $compile_id = null) { + return $this->returnOrCreateTemplate($template, $cache_id, $compile_id)->display(); } /** @@ -2265,7 +2235,6 @@ class Smarty extends \Smarty\TemplateBase * object * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template - * @param object $parent next higher level of Smarty variables * * @return bool cache status * @throws \Exception @@ -2274,8 +2243,8 @@ class Smarty extends \Smarty\TemplateBase * * @api Smarty::isCached() */ - public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null) { - return $this->returnOrCreateTemplate($template)->isCached($cache_id, $compile_id, $parent); + public function isCached($template = null, $cache_id = null, $compile_id = null) { + return $this->returnOrCreateTemplate($template, $cache_id, $compile_id)->isCached(); } /** @@ -2287,9 +2256,9 @@ class Smarty extends \Smarty\TemplateBase * @return Template * @throws Exception */ - private function returnOrCreateTemplate($template, $cache_id = null, $compile_id = null, $parent = null) { + private function returnOrCreateTemplate($template, $cache_id = null, $compile_id = null) { if (!($template instanceof Template)) { - $template = $this->createTemplate($template, $cache_id, $compile_id, $parent ?: $this, false); + $template = $this->createTemplate($template, $cache_id, $compile_id, $this); $template->caching = $this->caching; } return $template; diff --git a/src/Template.php b/src/Template.php index b87e1759..0d626b2a 100644 --- a/src/Template.php +++ b/src/Template.php @@ -693,7 +693,7 @@ class Template extends TemplateBase { /** * @inheritdoc */ - public function _loadConfigfile($config_file, $sections = null) + protected function _loadConfigfile($config_file, $sections = null) { $confObj = parent::_loadConfigfile($config_file, $sections); @@ -703,13 +703,13 @@ class Template extends TemplateBase { return $confObj; } - public function fetch($cache_id = null, $compile_id = null, $parent = null) { - $result = $this->_execute($cache_id, $compile_id, $parent, 0); + public function fetch() { + $result = $this->_execute(0); return $result === null ? ob_get_clean() : $result; } - public function display($cache_id = null, $compile_id = null, $parent = null) { - $this->_execute($cache_id, $compile_id, $parent, 1); + public function display() { + $this->_execute(1); } /** @@ -726,23 +726,20 @@ class Template extends TemplateBase { * * @api Smarty::isCached() */ - public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null): bool { - return (bool) $this->_execute($template, $cache_id, $compile_id, $parent, 2); + public function isCached(): bool { + return (bool) $this->_execute(2); } /** * fetches a rendered Smarty template * - * @param mixed $cache_id cache id to be used with this template - * @param mixed $compile_id compile id to be used with this template - * @param object $parent next higher level of Smarty variables * @param string $function function type 0 = fetch, 1 = display, 2 = isCache * * @return mixed - * @throws \Exception - * @throws \Smarty\Exception|\Throwable + * @throws Exception + * @throws \Throwable */ - private function _execute($cache_id, $compile_id, $parent, $function) { + private function _execute($function) { $smarty = $this->_getSmartyObj(); @@ -759,8 +756,6 @@ class Template extends TemplateBase { $errorHandler->activate(); } - $this->tplFunctions = array_merge($parent->tplFunctions ?? [], $this->tplFunctions ?? []); - if ($function === 2) { if ($this->caching) { // return cache status of template diff --git a/src/Template/Cached.php b/src/Template/Cached.php index 857e807e..55083adc 100644 --- a/src/Template/Cached.php +++ b/src/Template/Cached.php @@ -218,7 +218,7 @@ class Cached extends ResourceBase { * @param Template $_template template object * @param bool $update flag if called because cache update */ - public function process(Template $_template, $update = false) { + private function process(Template $_template, $update = false) { if ($this->handler->process($_template, $this, $update) === false) { $this->valid = false; } @@ -281,7 +281,7 @@ class Cached extends ResourceBase { * * @throws \Smarty\Exception */ - public function updateCache(Template $_template, $no_output_filter) { + private function updateCache(Template $_template, $no_output_filter) { ob_start(); if (!isset($_template->compiled)) { $_template->loadCompiled(); diff --git a/src/Template/Compiled.php b/src/Template/Compiled.php index efd0053b..1e4082e1 100644 --- a/src/Template/Compiled.php +++ b/src/Template/Compiled.php @@ -125,7 +125,7 @@ class Compiled extends ResourceBase { * * @throws Exception */ - public function process(Template $_smarty_tpl) { + private function process(Template $_smarty_tpl) { $source = &$_smarty_tpl->source; $smarty = &$_smarty_tpl->smarty; if ($source->handler->recompiled) { diff --git a/src/Template/ResourceBase.php b/src/Template/ResourceBase.php index 6f786418..33e41f22 100644 --- a/src/Template/ResourceBase.php +++ b/src/Template/ResourceBase.php @@ -92,13 +92,6 @@ abstract class ResourceBase { */ public $isCache = false; - /** - * Process resource - * - * @param Template $_template template object - */ - abstract public function process(Template $_template); - /** * get rendered template content by calling compiled or cached template code * diff --git a/src/TemplateBase.php b/src/TemplateBase.php index ced74e36..d0134485 100644 --- a/src/TemplateBase.php +++ b/src/TemplateBase.php @@ -434,48 +434,6 @@ abstract class TemplateBase extends Data { return $this; } - /** - * load a config file, optionally load just selected sections - * - * @param string $config_file filename - * @param mixed $sections array of section names, single - * section or null - * - * @return $this - * @throws \Exception - *@api Smarty::configLoad() - * @link https://www.smarty.net/docs/en/api.config.load.tpl - * - */ - public function configLoad($config_file, $sections = null) - { - $this->_loadConfigfile($config_file, $sections); - return $this; - } - /** - * load a config file, optionally load just selected sections - * - * @param string $config_file filename - * @param mixed $sections array of section names, single - * section or null - - * @returns Template - * @throws \Exception - * @link https://www.smarty.net/docs/en/api.config.load.tpl - * - * @api Smarty::configLoad() - */ - public function _loadConfigfile($config_file, $sections = null) - { - $smarty = $this->_getSmartyObj(); - - $confObj = new Template($config_file, $smarty, $this, null, null, null, null, true); - $confObj->caching = Smarty::CACHING_OFF; - $confObj->source->config_sections = $sections; - $confObj->compiled = \Smarty\Template\Compiled::load($confObj); - $confObj->compiled->render($confObj); - return $confObj; - } } diff --git a/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php b/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php index 32a6d118..d555df72 100644 --- a/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php +++ b/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php @@ -181,7 +181,8 @@ class ConfigVarTest extends PHPUnit_Smarty { $data = $this->smarty->createData(); $data->configLoad('test.conf'); - $this->assertEquals("123bvc", $this->smarty->fetch('text.tpl', $data)); + $tpl = $this->smarty->createTemplate('text.tpl', $data); + $this->assertEquals("123bvc", $this->smarty->fetch($tpl)); } /** diff --git a/tests/UnitTests/SmartyMethodsTests/AssignGlobal/AssignGlobalTest.php b/tests/UnitTests/SmartyMethodsTests/AssignGlobal/AssignGlobalTest.php deleted file mode 100644 index 19985832..00000000 --- a/tests/UnitTests/SmartyMethodsTests/AssignGlobal/AssignGlobalTest.php +++ /dev/null @@ -1,69 +0,0 @@ -setUpSmarty(__DIR__); - } - - - public function testInit() - { - $this->cleanDirs(); - } - /** - * test assignGlobal and getGlobal - */ - public function testAssignGlobalGetGlobal() - { - $this->smarty->assignGlobal('foo', 'bar'); - $this->assertEquals('bar', $this->smarty->getGlobal('foo')); - } - - /** - * test assignGlobal and getGlobal on arrays - */ - public function testAssignGlobalGetGlobalArray() - { - $this->smarty->assignGlobal('foo', array('foo' => 'bar', 'foo2' => 'bar2')); - $a1 = array('foo' => array('foo' => 'bar', 'foo2' => 'bar2')); - $a2 = $this->smarty->getGlobal(); - $this->assertTrue($a1 === $a2); - } - - /** - * test assignGlobal tag - */ - public function testAssignGlobalTag() - { - $this->smarty->assignGlobal('foo', 'bar'); - $this->assertEquals('bar', $this->smarty->fetch('eval:{$foo}')); - $this->assertEquals('buh', $this->smarty->fetch('eval:{assign var=foo value=buh scope=global}{$foo}')); - $this->assertEquals('buh', $this->smarty->fetch('eval:{$foo}')); - $this->assertEquals('buh', $this->smarty->getGlobal('foo')); - } - - /** - * test global var array element tag - */ - public function testGlobalVarArrayTag() - { - $this->smarty->assignGlobal('foo', array('foo' => 'bar', 'foo2' => 'bar2')); - $this->assertEquals('bar2', $this->smarty->fetch('eval:{$foo.foo2}')); - $this->assertEquals('bar', $this->smarty->fetch('eval:{$foo.foo}')); - } -} diff --git a/tests/UnitTests/SmartyMethodsTests/AssignGlobal/cache/.gitignore b/tests/UnitTests/SmartyMethodsTests/AssignGlobal/cache/.gitignore deleted file mode 100644 index d88cc144..00000000 --- a/tests/UnitTests/SmartyMethodsTests/AssignGlobal/cache/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Ignore anything in here, but keep this directory -* diff --git a/tests/UnitTests/SmartyMethodsTests/AssignGlobal/templates_c/.gitignore b/tests/UnitTests/SmartyMethodsTests/AssignGlobal/templates_c/.gitignore deleted file mode 100644 index d88cc144..00000000 --- a/tests/UnitTests/SmartyMethodsTests/AssignGlobal/templates_c/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Ignore anything in here, but keep this directory -* diff --git a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php index 847406c8..d210e111 100644 --- a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php +++ b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php @@ -37,15 +37,16 @@ class ScopeTest extends PHPUnit_Smarty { $file = "testAppendScope_{$testNumber}.tpl"; $this->makeTemplateFile($file, $code . '{checkvar var=foo}'); - $this->smarty->assignGlobal('file', $file); - $this->smarty->assign('foo', 'smarty'); - $this->smarty->assignGlobal('foo', 'global'); + $this->smarty->assign('file', $file); + $this->smarty->assign('foo', 'global'); $data1 = $this->smarty->createData($useSmarty ? $this->smarty : null); $data = $this->smarty->createData($data1); $data1->assign('foo', 'data1'); $data->assign('foo', 'data'); - $this->assertEquals($result, $this->smarty->fetch('scope_tag.tpl', $data), - "test - {$code} - {$testName}"); + + $tpl = $this->smarty->createTemplate('scope_tag.tpl', $data); + + $this->assertEquals($result, $this->smarty->fetch($tpl),"test - {$code} - {$testName}"); } /** @@ -60,18 +61,53 @@ class ScopeTest extends PHPUnit_Smarty * result * test name */ - return array(array('{$foo[] = \'newvar\' scope=tpl_root}', true, - '#testAppendScope_0.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#data:$foo =\'data\'#data:$foo =\'data1\'#Smarty:$foo =\'smarty\'#global:$foo =\'global\'', - '', $i ++,), - array('{append var=foo value=\'newvar\' scope=tpl_root}', true, - '#testAppendScope_1.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#data:$foo =\'data\'#data:$foo =\'data1\'#Smarty:$foo =\'smarty\'#global:$foo =\'global\'', - '', $i ++,), - array('{append var=foo value=\'newvar\' scope=global}', true, - '#testAppendScope_2.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#data:$foo =\'data\'#data:$foo =\'data1\'#Smarty:$foo =\'smarty\'#global:$foo =array(0=>\'data\',1=>\'newvar\',)', - '', $i ++,), - array('{append var=foo value=\'newvar\' scope=smarty}', true, - '#testAppendScope_3.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)#data:$foo =\'data\'#data:$foo =\'data1\'#Smarty:$foo =array(0=>\'data\',1=>\'newvar\',)#global:$foo =\'global\'', - '', $i ++,),); + return [ + [ + '{$foo[] = \'newvar\' scope=tpl_root}', + true, + '#testAppendScope_0.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' . + '#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' . + '#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' . + '#data:$foo =\'data\'' . + '#data:$foo =\'data1\'' . + '#global:$foo =\'global\'', + '', + $i++, + ], + [ + '{append var=foo value=\'newvar\' scope=tpl_root}', true, + '#testAppendScope_1.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' . + '#scope_include.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' . + '#scope_tag.tpl:$foo =array(0=>\'data\',1=>\'newvar\',)' . + '#data:$foo =\'data\'' . + '#data:$foo =\'data1\'' . + '#global:$foo =\'global\'', + '', + $i++, + ], + [ + '{append var=foo value=\'newvar\' scope=global}', true, + '#testAppendScope_2.tpl:$foo =\'data\'' . + '#scope_include.tpl:$foo =\'data\'' . + '#scope_tag.tpl:$foo =\'data\'' . + '#data:$foo =\'data\'' . + '#data:$foo =\'data1\'' . + '#global:$foo =array(0=>\'data\',1=>\'newvar\',)', + '', + $i++, + ], + [ + '{append var=foo value=\'newvar\' scope=smarty}', true, + '#testAppendScope_3.tpl:$foo =\'data\'' . + '#scope_include.tpl:$foo =\'data\'' . + '#scope_tag.tpl:$foo =\'data\'' . + '#data:$foo =\'data\'' . + '#data:$foo =\'data1\'' . + '#global:$foo =array(0=>\'data\',1=>\'newvar\',)', + '', + $i++, + ], + ]; } /** @@ -87,8 +123,10 @@ class ScopeTest extends PHPUnit_Smarty $this->smarty->assignGlobal('foo', 'global'); $data = $this->smarty->createData($useSmarty ? $this->smarty : null); $data->assign('foo', 'data'); - $this->assertEquals('#' . $file . $result, - $this->smarty->fetch('scope_tag.tpl', $data), "test - {$code} - {$testName}"); + + $tpl = $this->smarty->createTemplate('scope_tag.tpl', $data); + + $this->assertEquals('#' . $file . $result, $this->smarty->fetch($tpl), "test - {$code} - {$testName}"); } /* @@ -186,8 +224,8 @@ class ScopeTest extends PHPUnit_Smarty if (!$useSmarty) { $testName .= 'no smarty'; } - $this->assertEquals($result, $this->smarty->fetch('test_scope.tpl', $data), - "test - {$code} - {$testName}"); + $tpl = $this->smarty->createTemplate('test_scope.tpl', $data); + $this->assertEquals($result, $this->smarty->fetch($tpl), "test - {$code} - {$testName}"); } /* @@ -251,8 +289,12 @@ class ScopeTest extends PHPUnit_Smarty $this->smarty->configLoad('smarty.conf'); $data = $this->smarty->createData($useSmarty ? $this->smarty : null); $data->configLoad('data.conf'); - $this->assertEquals('#' . $file . $result, - $this->smarty->fetch('scope_tag.tpl', $data), "test - {$code} - {$testName}"); + $tpl = $this->smarty->createTemplate('scope_tag.tpl', $data); + $this->assertEquals( + '#' . $file . $result, + $this->smarty->fetch($tpl), + "test - {$code} - {$testName} + "); } /* diff --git a/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php b/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php index 4893c50a..589df651 100644 --- a/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php +++ b/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php @@ -20,7 +20,7 @@ use Smarty\Template; function smarty_function_checkvar($params, \Smarty\Template $template) { $output = ''; - $types = array('template', 'data', 'smarty', 'global'); + $types = ['template', 'data', 'global']; if (isset($params['types'])) { $types = (array)$params['types']; } @@ -45,15 +45,10 @@ function smarty_function_checkvar($params, \Smarty\Template $template) $ptr = null; } } - if (in_array('smarty', $types)) { - $output .= "#Smarty:\${$var} ="; + if (in_array('global', $types)) { + $output .= "#global:\${$var} ="; $output .= $template->smarty->hasVariable($var) ? preg_replace('/\s/', '', var_export($template->smarty->getValue($var), true)) : '>unassigned<'; } - if (in_array('global', $types)) { - $output .= "#global:\${$var} ="; - $output .= $template->_getSmartyObj()->getGlobalVariable($var) ? - preg_replace('/\s/', '', var_export($template->_getSmartyObj()->getGlobalVariable($var)->getValue(), true)) : '>unassigned<'; - } return $output; }