From afe9af11b7de27335a8850b11dafe20734ca6ffd Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Sun, 28 Jun 2015 04:44:16 +0200 Subject: [PATCH] - move $smarty->getTemplateVars() into extension --- change_log.txt | 1 + libs/Autoloader.php | 1 + libs/Smarty.class.php | 2 +- libs/sysplugins/smarty_internal_data.php | 63 +---------- .../smarty_internal_extension_getvars.php | 103 ++++++++++++++++++ 5 files changed, 108 insertions(+), 62 deletions(-) create mode 100644 libs/sysplugins/smarty_internal_extension_getvars.php diff --git a/change_log.txt b/change_log.txt index 7c548b47..fe0521fc 100644 --- a/change_log.txt +++ b/change_log.txt @@ -3,6 +3,7 @@ - move $smarty->enableSecurity() into Smarty_Security class - optimize security isTrustedResourceDir() - move auto load filter methods into extension + - move $smarty->getTemplateVars() into extension 27.06.2015 - bugfix resolve naming conflict between custom Smarty delimiter '<%' and PHP ASP tags https://github.com/smarty-php/smarty/issues/64 diff --git a/libs/Autoloader.php b/libs/Autoloader.php index aa89cc97..503c8832 100644 --- a/libs/Autoloader.php +++ b/libs/Autoloader.php @@ -81,6 +81,7 @@ class Smarty_Autoloader 'smarty_internal_extension_object' => true, 'smarty_internal_extension_loadplugin' => true, 'smarty_internal_extension_clearcompiled' => true, + 'smarty_internal_extension_getvars' => true, 'smarty_internal_filter_handler' => true, 'smarty_internal_function_call_handler' => true, 'smarty_internal_cacheresource_file' => true, diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 95316a0e..e4ea1483 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/14'; + const SMARTY_VERSION = '3.1.28-dev/15'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_data.php b/libs/sysplugins/smarty_internal_data.php index 2ca0f31d..cefaafd8 100644 --- a/libs/sysplugins/smarty_internal_data.php +++ b/libs/sysplugins/smarty_internal_data.php @@ -212,41 +212,7 @@ class Smarty_Internal_Data */ public function getTemplateVars($varname = null, $_ptr = null, $search_parents = true) { - if (isset($varname)) { - $_var = $this->getVariable($varname, $_ptr, $search_parents, false); - if (is_object($_var)) { - return $_var->value; - } else { - return null; - } - } else { - $_result = array(); - if ($_ptr === null) { - $_ptr = $this; - } - while ($_ptr !== null) { - foreach ($_ptr->tpl_vars AS $key => $var) { - if (!array_key_exists($key, $_result)) { - $_result[$key] = $var->value; - } - } - // not found, try at parent - if ($search_parents) { - $_ptr = $_ptr->parent; - } else { - $_ptr = null; - } - } - if ($search_parents && isset(Smarty::$global_tpl_vars)) { - foreach (Smarty::$global_tpl_vars AS $key => $var) { - if (!array_key_exists($key, $_result)) { - $_result[$key] = $var->value; - } - } - } - - return $_result; - } + return Smarty_Internal_Extension_GetVars::getTemplateVars($this, $varname, $_ptr, $search_parents); } /** @@ -308,32 +274,7 @@ class Smarty_Internal_Data */ public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true) { - if ($_ptr === null) { - $_ptr = $this; - } - while ($_ptr !== null) { - if (isset($_ptr->tpl_vars[$variable])) { - // found it, return it - return $_ptr->tpl_vars[$variable]; - } - // not found, try at parent - if ($search_parents) { - $_ptr = $_ptr->parent; - } else { - $_ptr = null; - } - } - if (isset(Smarty::$global_tpl_vars[$variable])) { - // found it, return it - return Smarty::$global_tpl_vars[$variable]; - } - $smarty = isset($this->smarty) ? $this->smarty : $this; - if ($smarty->error_unassigned && $error_enable) { - // force a notice - $x = $$variable; - } - - return new Smarty_Undefined_Variable; + return Smarty_Internal_Extension_GetVars::getVariable($this, $variable, $_ptr, $search_parents, $error_enable); } /** diff --git a/libs/sysplugins/smarty_internal_extension_getvars.php b/libs/sysplugins/smarty_internal_extension_getvars.php new file mode 100644 index 00000000..40cd4b0a --- /dev/null +++ b/libs/sysplugins/smarty_internal_extension_getvars.php @@ -0,0 +1,103 @@ +value; + } else { + return null; + } + } else { + $_result = array(); + if ($_ptr === null) { + $_ptr = $obj; + } + while ($_ptr !== null) { + foreach ($_ptr->tpl_vars AS $key => $var) { + if (!array_key_exists($key, $_result)) { + $_result[$key] = $var->value; + } + } + // not found, try at parent + if ($search_parents) { + $_ptr = $_ptr->parent; + } else { + $_ptr = null; + } + } + if ($search_parents && isset(Smarty::$global_tpl_vars)) { + foreach (Smarty::$global_tpl_vars AS $key => $var) { + if (!array_key_exists($key, $_result)) { + $_result[$key] = $var->value; + } + } + } + + return $_result; + } + } + + /** + * gets the object of a Smarty variable + * + * @param $obj + * @param string $variable the name of the Smarty variable + * @param object $_ptr optional pointer to data object + * @param boolean $search_parents search also in parent data + * @param bool $error_enable + * + * @return object the object of the variable + */ + public static function getVariable($obj, $variable, $_ptr, $search_parents, $error_enable) + { + if ($_ptr === null) { + $_ptr = $obj; + } + while ($_ptr !== null) { + if (isset($_ptr->tpl_vars[$variable])) { + // found it, return it + return $_ptr->tpl_vars[$variable]; + } + // not found, try at parent + if ($search_parents) { + $_ptr = $_ptr->parent; + } else { + $_ptr = null; + } + } + if (isset(Smarty::$global_tpl_vars[$variable])) { + // found it, return it + return Smarty::$global_tpl_vars[$variable]; + } + $smarty = isset($obj->smarty) ? $obj->smarty : $obj; + if ($smarty->error_unassigned && $error_enable) { + // force a notice + $x = $$variable; + } + + return new Smarty_Undefined_Variable; + } +} \ No newline at end of file