From 9b41308bf6f4497585e6774a9afcea75f8912152 Mon Sep 17 00:00:00 2001 From: "Uwe.Tews" Date: Tue, 7 Apr 2009 16:22:31 +0000 Subject: [PATCH] - changed variable scopes to SMARTY_LOCAL_SCOPE, SMARTY_PARENT_SCOPE, SMARTY_ROOT_SCOPE to avoid possible conflicts with user constants - Smarty variable global attribute replaced with scope attribute --- change_log.txt | 4 +++ libs/Smarty.class.php | 6 ++-- libs/sysplugins/internal.compile_append.php | 13 ++++----- libs/sysplugins/internal.compile_assign.php | 16 +++++------ libs/sysplugins/internal.compile_include.php | 8 +++--- libs/sysplugins/internal.template.php | 9 +++--- libs/sysplugins/internal.templatebase.php | 30 +++++++++++--------- 7 files changed, 46 insertions(+), 40 deletions(-) diff --git a/change_log.txt b/change_log.txt index 509188a5..5b2f67f9 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,3 +1,7 @@ +04/07/2009 +- changed variable scopes to SMARTY_LOCAL_SCOPE, SMARTY_PARENT_SCOPE, SMARTY_ROOT_SCOPE to avoid possible conflicts with user constants +- Smarty variable global attribute replaced with scope attribute + 04/06/2009 - variable scopes LOCAL_SCOPE, PARENT_SCOPE, ROOT_SCOPE - more getter/setter methodes diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index c020ec03..9b33647b 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -42,9 +42,9 @@ if (!defined('SMARTY_DIR')) { /** * define variable scopes */ -define('LOCAL_SCOPE',0); -define('PARENT_SCOPE',1); -define('ROOT_SCOPE',2); +define('SMARTY_LOCAL_SCOPE',0); +define('SMARTY_PARENT_SCOPE',1); +define('SMARTY_ROOT_SCOPE',2); /** * load required base class for creation of the smarty object diff --git a/libs/sysplugins/internal.compile_append.php b/libs/sysplugins/internal.compile_append.php index 316739f4..495efac2 100644 --- a/libs/sysplugins/internal.compile_append.php +++ b/libs/sysplugins/internal.compile_append.php @@ -24,10 +24,10 @@ class Smarty_Internal_Compile_Append extends Smarty_Internal_CompileBase { { $this->compiler = $compiler; $this->required_attributes = array('var', 'value'); - $this->optional_attributes = array('global', 'nocache', 'index'); + $this->optional_attributes = array('scope', 'nocache', 'index'); $_nocache = 'null'; - $_global = 'null'; + $_scope = 'null'; // check for nocache attribute before _get_attributes because // it shall not controll caching of the compiled code, but is a parameter if (isset($args['nocache'])) { @@ -40,15 +40,14 @@ class Smarty_Internal_Compile_Append extends Smarty_Internal_CompileBase { // check and get attributes $_attr = $this->_get_attributes($args); - if (isset($_attr['global']) && $_attr['global'] == 'true') { - $_global = 'true'; - $_global_boolean = true; + if (isset($_attr['scope'])) { + $_scope = trim($_attr['scope'],'\''); } // compiled output if (isset($_attr['index'])) { - return "append($_attr[var],array($_attr[index] => $_attr[value]),true,$_nocache,$_global);?>"; + return "append($_attr[var],array($_attr[index] => $_attr[value]),true,$_nocache,$_scope);?>"; } else { - return "append($_attr[var],$_attr[value],false,$_nocache,$_global);?>"; + return "append($_attr[var],$_attr[value],false,$_nocache,$_scope);?>"; } } } diff --git a/libs/sysplugins/internal.compile_assign.php b/libs/sysplugins/internal.compile_assign.php index 95734c43..c227f445 100644 --- a/libs/sysplugins/internal.compile_assign.php +++ b/libs/sysplugins/internal.compile_assign.php @@ -24,10 +24,10 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase { { $this->compiler = $compiler; $this->required_attributes = array('var', 'value'); - $this->optional_attributes = array('global', 'nocache', 'index'); + $this->optional_attributes = array('scope', 'nocache', 'index'); $_nocache = 'null'; - $_global = 'null'; + $_scope = 'null'; // check for nocache attribute before _get_attributes because // it shall not controll caching of the compiled code, but is a parameter if (isset($args['nocache'])) { @@ -40,22 +40,22 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase { // check and get attributes $_attr = $this->_get_attributes($args); - if (isset($_attr['global']) && $_attr['global'] == 'true') { - $_global = 'true'; - $_global_boolean = true; + if (isset($_attr['scope'])) { + $_scope = trim($_attr['scope'],'\''); } + if (isset($_attr['index'])) { $_index = $_attr['index']; } // compiled output if (isset($_attr['index'])) { if ($_attr['index'] == '') { - return "append($_attr[var],$_attr[value],false,$_nocache,$_global);?>"; + return "append($_attr[var],$_attr[value],false,$_nocache,$_scope);?>"; } else { - return "append($_attr[var],\$_tmp,true,$_nocache,$_global);?>"; + return "append($_attr[var],\$_tmp,true,$_nocache,$_scope);?>"; } } else { - return "assign($_attr[var],$_attr[value],$_nocache,$_global);?>"; + return "assign($_attr[var],$_attr[value],$_nocache,$_scope);?>"; } } } diff --git a/libs/sysplugins/internal.compile_include.php b/libs/sysplugins/internal.compile_include.php index 5ec33158..a7a1fcbd 100644 --- a/libs/sysplugins/internal.compile_include.php +++ b/libs/sysplugins/internal.compile_include.php @@ -34,12 +34,12 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase { $_assign = $_attr['assign']; } - $_parent_scope = LOCAL_SCOPE; + $_parent_scope = SMARTY_LOCAL_SCOPE; if (isset($_attr['scope'])) { if ($_attr['scope'] == '\'parent\'') { - $_parent_scope = PARENT_SCOPE; + $_parent_scope = SMARTY_PARENT_SCOPE; } elseif ($_attr['scope'] == '\'root\'') { - $_parent_scope = ROOT_SCOPE; + $_parent_scope = SMARTY_ROOT_SCOPE; } } @@ -69,7 +69,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase { unset($_attr['file'], $_attr['assign'], $_attr['caching_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope']); // remaining attributes must be assigned as smarty variable if (!empty($_attr)) { - if ($_parent_scope == LOCAL_SCOPE) { + if ($_parent_scope == SMARTY_LOCAL_SCOPE) { // create variables foreach ($_attr as $_key => $_value) { $_output .= "\$_template->assign('$_key',$_value);"; diff --git a/libs/sysplugins/internal.template.php b/libs/sysplugins/internal.template.php index 5d1bc871..c3df08bb 100644 --- a/libs/sysplugins/internal.template.php +++ b/libs/sysplugins/internal.template.php @@ -89,7 +89,8 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { $this->caching_type = $this->smarty->default_caching_type; $this->security = $this->smarty->security; $this->cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($this->caching_type); - $this->parent = $_parent; + $this->parent = $_parent; + $this->tpl_vars['smarty'] = new Smarty_Variable; // load filter handler if required if (!is_object($this->smarty->filter_handler) && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { $this->smarty->loadPlugin('Smarty_Internal_Run_Filter'); @@ -546,11 +547,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { /** * Update Smarty variables in parent variable object */ - public function updateParentVariables ($scope = LOCAL_SCOPE) + public function updateParentVariables ($scope = SMARTY_LOCAL_SCOPE) { foreach ($this->tpl_vars as $_key => $_value) { // copy global vars back to parent - if (isset($this->parent) && ($scope == PARENT_SCOPE || $this->tpl_vars[$_key]->global)) { + if (isset($this->parent) && ($scope == SMARTY_PARENT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_PARENT_SCOPE)) { if (isset($this->parent->tpl_vars[$_key])) { // variable is already defined in parent, copy value $this->parent->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value; @@ -559,7 +560,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { $this->parent->tpl_vars[$_key] = clone $_value; } } - if ($scope == ROOT_SCOPE) { + if ($scope == SMARTY_ROOT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_ROOT_SCOPE) { if (isset($this->smarty->tpl_vars[$_key])) { // variable is already defined in root, copy value $this->smarty->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value; diff --git a/libs/sysplugins/internal.templatebase.php b/libs/sysplugins/internal.templatebase.php index 4c0351b3..bf777aae 100644 --- a/libs/sysplugins/internal.templatebase.php +++ b/libs/sysplugins/internal.templatebase.php @@ -20,21 +20,21 @@ class Smarty_Internal_TemplateBase { * @param array $ |string $tpl_var the template variable name(s) * @param mixed $value the value to assign * @param boolean $nocache if true any output of this variable will be not cached - * @param boolean $global if true the variable will have global scope + * @param boolean $scope the scope the variable will have (local,parent or root) */ - public function assign($tpl_var, $value = null, $nocache = false, $global = false) + public function assign($tpl_var, $value = null, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) { if (is_array($tpl_var)) { foreach ($tpl_var as $_key => $_val) { if ($_key != '') { $this->check_tplvar($_key); - $this->tpl_vars[$_key] = new Smarty_variable($_val, $nocache, $global); + $this->tpl_vars[$_key] = new Smarty_variable($_val, $nocache, $scope); } } } else { if ($tpl_var != '') { $this->check_tplvar($tpl_var); - $this->tpl_vars[$tpl_var] = new Smarty_variable($value, $nocache, $global); + $this->tpl_vars[$tpl_var] = new Smarty_variable($value, $nocache, $scope); } } } @@ -59,13 +59,13 @@ class Smarty_Internal_TemplateBase { * @param string $tpl_var the template variable name * @param mixed $ &$value the referenced value to assign * @param boolean $nocache if true any output of this variable will be not cached - * @param boolean $global if true the variable will have global scope + * @param boolean $scope the scope the variable will have (local,parent or root) */ - public function assign_by_ref($tpl_var, &$value, $nocache = false, $global = false) + public function assign_by_ref($tpl_var, &$value, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) { if ($tpl_var != '') { $this->check_tplvar($tpl_var); - $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $global); + $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $scope); $this->tpl_vars[$tpl_var]->value = &$value; } } @@ -75,8 +75,10 @@ class Smarty_Internal_TemplateBase { * @param array $ |string $tpl_var the template variable name(s) * @param mixed $value the value to append * @param boolean $merge flag if array elements shall be merged + * @param boolean $nocache if true any output of this variable will be not cached + * @param boolean $scope the scope the variable will have (local,parent or root) */ - public function append($tpl_var, $value = null, $merge = false, $nocache = false, $global = false) + public function append($tpl_var, $value = null, $merge = false, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) { if (is_array($tpl_var)) { // $tpl_var is an array, ignore $value @@ -84,7 +86,7 @@ class Smarty_Internal_TemplateBase { if ($_key != '') { if (!isset($this->tpl_vars[$_key])) { $this->check_tplvar($_key); - $this->tpl_vars[$_key] = new Smarty_variable(null, $nocache, $global); + $this->tpl_vars[$_key] = new Smarty_variable(null, $nocache, $scope); } if (!is_array($this->tpl_vars[$_key]->value)) { settype($this->tpl_vars[$_key]->value, 'array'); @@ -102,7 +104,7 @@ class Smarty_Internal_TemplateBase { if ($tpl_var != '' && isset($value)) { if (!isset($this->tpl_vars[$tpl_var])) { $this->check_tplvar($tpl_var); - $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $global); + $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $scope); } if (!is_array($this->tpl_vars[$tpl_var]->value)) { settype($this->tpl_vars[$tpl_var]->value, 'array'); @@ -349,19 +351,19 @@ class Smarty_Variable { // template variable public $value; public $nocache; - public $global; + public $scope; /** * create Smarty variable object * * @param mixed $value the value to assign * @param boolean $nocache if true any output of this variable will be not cached - * @param boolean $global if true the variable will have global scope + * @param boolean $scope the scope the variable will have (local,parent or root) */ - public function __construct ($value = null, $nocache = false, $global = false) + public function __construct ($value = null, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) { $this->value = $value; $this->nocache = $nocache; - $this->global = $global; + $this->scope = $scope; } }