From 8082bc74718174976759895a61110a7fc2aaaa4d Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Sun, 15 Feb 2015 01:45:37 +0100 Subject: [PATCH] get rid of smarty object self pointer --- libs/Smarty.class.php | 14 +- libs/sysplugins/smarty_internal_data.php | 7 +- .../smarty_internal_extension_config.php | 5 +- ...ernal_extension_defaulttemplatehandler.php | 16 +-- libs/sysplugins/smarty_internal_template.php | 24 ++++ .../smarty_internal_templatebase.php | 125 +++++++++--------- libs/sysplugins/smarty_resource.php | 7 +- 7 files changed, 109 insertions(+), 89 deletions(-) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index e42a9713..ae7d0d75 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.22-dev/7'; + const SMARTY_VERSION = '3.1.22-dev/8'; /** * define variable scopes @@ -655,8 +655,6 @@ class Smarty extends Smarty_Internal_TemplateBase */ public function __construct() { - // selfpointer needed by some other class methods - $this->smarty = $this; if (is_callable('mb_internal_encoding')) { mb_internal_encoding(Smarty::$_CHARSET); } @@ -721,7 +719,7 @@ class Smarty extends Smarty_Internal_TemplateBase $parent = $this; } // get template object - $_template = is_object($template) ? $template : $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false); + $_template = is_object($template) ? $template : $this->createTemplate($template, $cache_id, $compile_id, $parent, false); // set caching in template object $_template->caching = $this->caching; // fetch template content @@ -1567,14 +1565,6 @@ class Smarty extends Smarty_Internal_TemplateBase // intentionally left blank } - /** - * <> set self pointer on cloned object - */ - public function __clone() - { - $this->smarty = $this; - } - /** * <> Generic getter. * Calls the appropriate getter function. diff --git a/libs/sysplugins/smarty_internal_data.php b/libs/sysplugins/smarty_internal_data.php index 28cf2ab1..2ca0f31d 100644 --- a/libs/sysplugins/smarty_internal_data.php +++ b/libs/sysplugins/smarty_internal_data.php @@ -327,7 +327,8 @@ class Smarty_Internal_Data // found it, return it return Smarty::$global_tpl_vars[$variable]; } - if ($this->smarty->error_unassigned && $error_enable) { + $smarty = isset($this->smarty) ? $this->smarty : $this; + if ($smarty->error_unassigned && $error_enable) { // force a notice $x = $$variable; } @@ -393,8 +394,8 @@ class Smarty_Internal_Data return $_result; } - - if ($this->smarty->error_unassigned) { + $smarty = isset($this->smarty) ? $this->smarty : $this; + if ($smarty->error_unassigned) { throw new SmartyException('Undefined stream variable "' . $variable . '"'); } else { return null; diff --git a/libs/sysplugins/smarty_internal_extension_config.php b/libs/sysplugins/smarty_internal_extension_config.php index 827031ea..df90ad04 100644 --- a/libs/sysplugins/smarty_internal_extension_config.php +++ b/libs/sysplugins/smarty_internal_extension_config.php @@ -14,7 +14,8 @@ class Smarty_Internal_Extension_Config */ static function configLoad($obj, $config_file, $sections = null, $scope = 'local') { - $confObj = new $obj->smarty->template_class($config_file, $obj->smarty, $obj); + $smarty = isset($obj->smarty) ? $obj->smarty : $obj; + $confObj = new $smarty->template_class($config_file, $smarty, $obj); $confObj->caching = Smarty::CACHING_OFF; $confObj->source = Smarty_Template_Config::load($confObj); $confObj->source->config_sections = $sections; @@ -132,7 +133,7 @@ class Smarty_Internal_Extension_Config // not found, try at parent $_ptr = $_ptr->parent; } - if ($obj->smarty->error_unassigned && $error_enable) { + if ($smarty->error_unassigned && $error_enable) { // force a notice $x = $$variable; } diff --git a/libs/sysplugins/smarty_internal_extension_defaulttemplatehandler.php b/libs/sysplugins/smarty_internal_extension_defaulttemplatehandler.php index e67d9a81..bf25eb91 100644 --- a/libs/sysplugins/smarty_internal_extension_defaulttemplatehandler.php +++ b/libs/sysplugins/smarty_internal_extension_defaulttemplatehandler.php @@ -50,15 +50,15 @@ class Smarty_Internal_Extension_DefaultTemplateHandler /** * register template default handler * - * @param Smarty|Smarty_Internal_Template $obj - * @param mixed $callback + * @param Smarty $smarty + * @param mixed $callback * * @throws SmartyException */ - static function registerDefaultTemplateHandler($obj, $callback) + static function registerDefaultTemplateHandler(Smarty $smarty, $callback) { if (is_callable($callback)) { - $obj->smarty->default_template_handler_func = $callback; + $smarty->default_template_handler_func = $callback; } else { throw new SmartyException("Default template handler not callable"); } @@ -67,15 +67,15 @@ class Smarty_Internal_Extension_DefaultTemplateHandler /** * register config default handler * - * @param Smarty|Smarty_Internal_Template $obj - * @param mixed $callback + * @param Smarty $smarty + * @param mixed $callback * * @throws SmartyException */ - static function registerDefaultConfigHandler($obj, $callback) + static function registerDefaultConfigHandler(Smarty $smarty, $callback) { if (is_callable($callback)) { - $obj->smarty->default_config_handler_func = $callback; + $smarty->default_config_handler_func = $callback; } else { throw new SmartyException("Default config handler not callable"); } diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index d2b1b1a0..a510a398 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -20,6 +20,13 @@ */ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { + /** + * Global smarty instance + * + * @var Smarty + */ + public $smarty = null; + /** * Template resource * @@ -765,6 +772,23 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase } /** + * Handle unknown class methods + * + * @param string $name unknown method-name + * @param array $args argument array + * + * @throws SmartyException + */ + public function __call($name, $args) + { + // method of Smarty object? + if (method_exists($this->smarty, $name)) { + return call_user_func_array(array($this->smarty, $name), $args); + } + // parent + return parent::__call($name, $args); + } + /** * set Smarty property in template context * * @param string $property_name property name diff --git a/libs/sysplugins/smarty_internal_templatebase.php b/libs/sysplugins/smarty_internal_templatebase.php index 5a9cf01b..f0c3fd40 100644 --- a/libs/sysplugins/smarty_internal_templatebase.php +++ b/libs/sysplugins/smarty_internal_templatebase.php @@ -16,12 +16,6 @@ */ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data { - /** - * Global smarty instance - * - * @var Smarty - */ - public $smarty = null; /** * Set this if you want different sets of cache files for the same * templates. @@ -68,7 +62,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data if ($parent === null) { $parent = $this; } - $template = $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false); + $smarty = isset($this->smarty) ? $this->smarty : $this; + $template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent, false); } // return cache status of template return $template->cached->valid; @@ -104,10 +99,11 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data { $cache_id = isset($cache_id) ? $cache_id : $this->cache_id; $compile_id = isset($compile_id) ? $compile_id : $this->compile_id; - if ($this->smarty->allow_ambiguous_resources) { + $smarty = isset($this->smarty) ? $this->smarty : $this; + if ($smarty->allow_ambiguous_resources) { $_templateId = Smarty_Resource::getUniqueTemplateName($this, $template_name) . "#{$cache_id}#{$compile_id}"; } else { - $_templateId = $this->smarty->joined_template_dir . "#{$template_name}#{$cache_id}#{$compile_id}"; + $_templateId = $smarty->joined_template_dir . "#{$template_name}#{$cache_id}#{$compile_id}"; } if (isset($_templateId[150])) { $_templateId = sha1($_templateId); @@ -129,12 +125,13 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null) { - if (isset($this->smarty->registered_plugins[$type][$tag])) { + $smarty = isset($this->smarty) ? $this->smarty : $this; + if (isset($smarty->registered_plugins[$type][$tag])) { throw new SmartyException("Plugin tag \"{$tag}\" already registered"); } elseif (!is_callable($callback)) { throw new SmartyException("Plugin \"{$tag}\" not callable"); } else { - $this->smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr); + $smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr); } return $this; @@ -150,8 +147,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function unregisterPlugin($type, $tag) { - if (isset($this->smarty->registered_plugins[$type][$tag])) { - unset($this->smarty->registered_plugins[$type][$tag]); + $smarty = isset($this->smarty) ? $this->smarty : $this; + if (isset($smarty->registered_plugins[$type][$tag])) { + unset($smarty->registered_plugins[$type][$tag]); } return $this; @@ -167,7 +165,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function registerResource($type, $callback) { - $this->smarty->registered_resources[$type] = $callback instanceof Smarty_Resource ? $callback : array($callback, false); + $smarty = isset($this->smarty) ? $this->smarty : $this; + $smarty->registered_resources[$type] = $callback instanceof Smarty_Resource ? $callback : array($callback, false); return $this; } @@ -181,8 +180,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function unregisterResource($type) { - if (isset($this->smarty->registered_resources[$type])) { - unset($this->smarty->registered_resources[$type]); + $smarty = isset($this->smarty) ? $this->smarty : $this; + if (isset($smarty->registered_resources[$type])) { + unset($smarty->registered_resources[$type]); } return $this; @@ -198,7 +198,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function registerCacheResource($type, Smarty_CacheResource $callback) { - $this->smarty->registered_cache_resources[$type] = $callback; + $smarty = isset($this->smarty) ? $this->smarty : $this; + $smarty->registered_cache_resources[$type] = $callback; return $this; } @@ -212,8 +213,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function unregisterCacheResource($type) { - if (isset($this->smarty->registered_cache_resources[$type])) { - unset($this->smarty->registered_cache_resources[$type]); + $smarty = isset($this->smarty) ? $this->smarty : $this; + if (isset($smarty->registered_cache_resources[$type])) { + unset($smarty->registered_cache_resources[$type]); } return $this; @@ -250,7 +252,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data } } // register the object - $this->smarty->registered_objects[$object_name] = + $smarty = isset($this->smarty) ? $this->smarty : $this; + $smarty->registered_objects[$object_name] = array($object_impl, (array) $allowed, (boolean) $smarty_args, (array) $block_methods); return $this; @@ -266,14 +269,15 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function getRegisteredObject($name) { - if (!isset($this->smarty->registered_objects[$name])) { + $smarty = isset($this->smarty) ? $this->smarty : $this; + if (!isset($smarty->registered_objects[$name])) { throw new SmartyException("'$name' is not a registered object"); } - if (!is_object($this->smarty->registered_objects[$name][0])) { + if (!is_object($smarty->registered_objects[$name][0])) { throw new SmartyException("registered '$name' is not an object"); } - return $this->smarty->registered_objects[$name][0]; + return $smarty->registered_objects[$name][0]; } /** @@ -285,8 +289,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function unregisterObject($name) { - if (isset($this->smarty->registered_objects[$name])) { - unset($this->smarty->registered_objects[$name]); + $smarty = isset($this->smarty) ? $this->smarty : $this; + if (isset($smarty->registered_objects[$name])) { + unset($smarty->registered_objects[$name]); } return $this; @@ -308,7 +313,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data throw new SmartyException("Undefined class '$class_impl' in register template class"); } // register the class - $this->smarty->registered_classes[$class_name] = $class_impl; + $smarty = isset($this->smarty) ? $this->smarty : $this; + $smarty->registered_classes[$class_name] = $class_impl; return $this; } @@ -323,8 +329,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function registerDefaultPluginHandler($callback) { + $smarty = isset($this->smarty) ? $this->smarty : $this; if (is_callable($callback)) { - $this->smarty->default_plugin_handler_func = $callback; + $smarty->default_plugin_handler_func = $callback; } else { throw new SmartyException("Default plugin handler '$callback' not callable"); } @@ -370,7 +377,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function registerFilter($type, $callback) { - $this->smarty->registered_filters[$type][$this->_get_filter_name($callback)] = $callback; + $smarty = isset($this->smarty) ? $this->smarty : $this; + $smarty->registered_filters[$type][$this->_get_filter_name($callback)] = $callback; return $this; } @@ -386,8 +394,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data public function unregisterFilter($type, $callback) { $name = $this->_get_filter_name($callback); - if (isset($this->smarty->registered_filters[$type][$name])) { - unset($this->smarty->registered_filters[$type][$name]); + $smarty = isset($this->smarty) ? $this->smarty : $this; + if (isset($smarty->registered_filters[$type][$name])) { + unset($smarty->registered_filters[$type][$name]); } return $this; @@ -422,14 +431,15 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function loadFilter($type, $name) { + $smarty = isset($this->smarty) ? $this->smarty : $this; $_plugin = "smarty_{$type}filter_{$name}"; $_filter_name = $_plugin; - if ($this->smarty->loadPlugin($_plugin)) { + if ($smarty->loadPlugin($_plugin)) { if (class_exists($_plugin, false)) { $_plugin = array($_plugin, 'execute'); } if (is_callable($_plugin)) { - $this->smarty->registered_filters[$type][$_filter_name] = $_plugin; + $smarty->registered_filters[$type][$_filter_name] = $_plugin; return true; } @@ -447,9 +457,10 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data */ public function unloadFilter($type, $name) { + $smarty = isset($this->smarty) ? $this->smarty : $this; $_filter_name = "smarty_{$type}filter_{$name}"; - if (isset($this->smarty->registered_filters[$type][$_filter_name])) { - unset ($this->smarty->registered_filters[$type][$_filter_name]); + if (isset($smarty->registered_filters[$type][$_filter_name])) { + unset ($smarty->registered_filters[$type][$_filter_name]); } return $this; @@ -481,10 +492,6 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data static $_resolved_property_name = array(); static $_resolved_property_source = array(); - // method of Smarty object? - if (method_exists($this->smarty, $name)) { - return call_user_func_array(array($this->smarty, $name), $args); - } // see if this is a set/get for a property $first3 = strtolower(substr($name, 0, 3)); if (isset($_prefixes[$first3]) && isset($name[3]) && $name[3] !== '_') { @@ -499,36 +506,32 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data $_resolved_property_name[$name] = $property_name; } if (isset($_resolved_property_source[$property_name])) { - $_is_this = $_resolved_property_source[$property_name]; + $status = $_resolved_property_source[$property_name]; } else { - $_is_this = null; + $status = null; if (property_exists($this, $property_name)) { - $_is_this = true; + $status = true; } elseif (property_exists($this->smarty, $property_name)) { - $_is_this = false; + $status = false; } - $_resolved_property_source[$property_name] = $_is_this; + $_resolved_property_source[$property_name] = $status; } - if ($_is_this) { - if ($first3 == 'get') { - return $this->$property_name; - } else { - return $this->$property_name = $args[0]; - } - } elseif ($_is_this === false) { - if ($first3 == 'get') { - return $this->smarty->$property_name; - } else { - return $this->smarty->$property_name = $args[0]; - } - } else { - throw new SmartyException("property '$property_name' does not exist."); + $smarty = null; + if ($status === true) { + $smarty = $this; + } elseif ($status === false) { + $smarty = $this->smarty; } + if ($smarty) { + if ($first3 == 'get') { + return $smarty->$property_name; + } else { + return $smarty->$property_name = $args[0]; + } + } + throw new SmartyException("property '$property_name' does not exist."); } - if ($name == 'Smarty') { - throw new SmartyException("PHP5 requires you to call __construct() instead of Smarty()"); - } - // must be unknown throw new SmartyException("Call of unknown method '$name'."); } } + diff --git a/libs/sysplugins/smarty_resource.php b/libs/sysplugins/smarty_resource.php index 006cd01f..53f59b20 100644 --- a/libs/sysplugins/smarty_resource.php +++ b/libs/sysplugins/smarty_resource.php @@ -250,15 +250,16 @@ abstract class Smarty_Resource */ public static function getUniqueTemplateName($template, $template_resource) { - list($name, $type) = self::parseResourceName($template_resource, $template->smarty->default_resource_type); + $smarty = isset($template->smarty) ? $template->smarty : $template; + list($name, $type) = self::parseResourceName($template_resource, $smarty->default_resource_type); // TODO: optimize for Smarty's internal resource types - $resource = Smarty_Resource::load($template->smarty, $type); + $resource = Smarty_Resource::load($smarty, $type); // go relative to a given template? $_file_is_dotted = $name[0] == '.' && ($name[1] == '.' || $name[1] == '/'); if ($template instanceof Smarty_Internal_Template && $_file_is_dotted && ($template->source->type == 'file' || $template->parent->source->type == 'extends')) { $name = dirname($template->source->filepath) . DS . $name; } - return $resource->buildUniqueResourceName($template->smarty, $name); + return $resource->buildUniqueResourceName($smarty, $name); } /**