get rid of smarty object self pointer

This commit is contained in:
Uwe Tews
2015-02-15 01:45:37 +01:00
parent f93cfc63ad
commit 8082bc7471
7 changed files with 109 additions and 89 deletions

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.22-dev/7'; const SMARTY_VERSION = '3.1.22-dev/8';
/** /**
* define variable scopes * define variable scopes
@@ -655,8 +655,6 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public function __construct() public function __construct()
{ {
// selfpointer needed by some other class methods
$this->smarty = $this;
if (is_callable('mb_internal_encoding')) { if (is_callable('mb_internal_encoding')) {
mb_internal_encoding(Smarty::$_CHARSET); mb_internal_encoding(Smarty::$_CHARSET);
} }
@@ -721,7 +719,7 @@ class Smarty extends Smarty_Internal_TemplateBase
$parent = $this; $parent = $this;
} }
// get template object // 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 // set caching in template object
$_template->caching = $this->caching; $_template->caching = $this->caching;
// fetch template content // fetch template content
@@ -1567,14 +1565,6 @@ class Smarty extends Smarty_Internal_TemplateBase
// intentionally left blank // intentionally left blank
} }
/**
* <<magic>> set self pointer on cloned object
*/
public function __clone()
{
$this->smarty = $this;
}
/** /**
* <<magic>> Generic getter. * <<magic>> Generic getter.
* Calls the appropriate getter function. * Calls the appropriate getter function.

View File

@@ -327,7 +327,8 @@ class Smarty_Internal_Data
// found it, return it // found it, return it
return Smarty::$global_tpl_vars[$variable]; 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 // force a notice
$x = $$variable; $x = $$variable;
} }
@@ -393,8 +394,8 @@ class Smarty_Internal_Data
return $_result; return $_result;
} }
$smarty = isset($this->smarty) ? $this->smarty : $this;
if ($this->smarty->error_unassigned) { if ($smarty->error_unassigned) {
throw new SmartyException('Undefined stream variable "' . $variable . '"'); throw new SmartyException('Undefined stream variable "' . $variable . '"');
} else { } else {
return null; return null;

View File

@@ -14,7 +14,8 @@ class Smarty_Internal_Extension_Config
*/ */
static function configLoad($obj, $config_file, $sections = null, $scope = 'local') 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->caching = Smarty::CACHING_OFF;
$confObj->source = Smarty_Template_Config::load($confObj); $confObj->source = Smarty_Template_Config::load($confObj);
$confObj->source->config_sections = $sections; $confObj->source->config_sections = $sections;
@@ -132,7 +133,7 @@ class Smarty_Internal_Extension_Config
// not found, try at parent // not found, try at parent
$_ptr = $_ptr->parent; $_ptr = $_ptr->parent;
} }
if ($obj->smarty->error_unassigned && $error_enable) { if ($smarty->error_unassigned && $error_enable) {
// force a notice // force a notice
$x = $$variable; $x = $$variable;
} }

View File

@@ -50,15 +50,15 @@ class Smarty_Internal_Extension_DefaultTemplateHandler
/** /**
* register template default handler * register template default handler
* *
* @param Smarty|Smarty_Internal_Template $obj * @param Smarty $smarty
* @param mixed $callback * @param mixed $callback
* *
* @throws SmartyException * @throws SmartyException
*/ */
static function registerDefaultTemplateHandler($obj, $callback) static function registerDefaultTemplateHandler(Smarty $smarty, $callback)
{ {
if (is_callable($callback)) { if (is_callable($callback)) {
$obj->smarty->default_template_handler_func = $callback; $smarty->default_template_handler_func = $callback;
} else { } else {
throw new SmartyException("Default template handler not callable"); throw new SmartyException("Default template handler not callable");
} }
@@ -67,15 +67,15 @@ class Smarty_Internal_Extension_DefaultTemplateHandler
/** /**
* register config default handler * register config default handler
* *
* @param Smarty|Smarty_Internal_Template $obj * @param Smarty $smarty
* @param mixed $callback * @param mixed $callback
* *
* @throws SmartyException * @throws SmartyException
*/ */
static function registerDefaultConfigHandler($obj, $callback) static function registerDefaultConfigHandler(Smarty $smarty, $callback)
{ {
if (is_callable($callback)) { if (is_callable($callback)) {
$obj->smarty->default_config_handler_func = $callback; $smarty->default_config_handler_func = $callback;
} else { } else {
throw new SmartyException("Default config handler not callable"); throw new SmartyException("Default config handler not callable");
} }

View File

@@ -20,6 +20,13 @@
*/ */
class Smarty_Internal_Template extends Smarty_Internal_TemplateBase class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
{ {
/**
* Global smarty instance
*
* @var Smarty
*/
public $smarty = null;
/** /**
* Template resource * 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 * set Smarty property in template context
* *
* @param string $property_name property name * @param string $property_name property name

View File

@@ -16,12 +16,6 @@
*/ */
abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data 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 * Set this if you want different sets of cache files for the same
* templates. * templates.
@@ -68,7 +62,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
if ($parent === null) { if ($parent === null) {
$parent = $this; $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 cache status of template
return $template->cached->valid; 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; $cache_id = isset($cache_id) ? $cache_id : $this->cache_id;
$compile_id = isset($compile_id) ? $compile_id : $this->compile_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}"; $_templateId = Smarty_Resource::getUniqueTemplateName($this, $template_name) . "#{$cache_id}#{$compile_id}";
} else { } 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])) { if (isset($_templateId[150])) {
$_templateId = sha1($_templateId); $_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) 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"); throw new SmartyException("Plugin tag \"{$tag}\" already registered");
} elseif (!is_callable($callback)) { } elseif (!is_callable($callback)) {
throw new SmartyException("Plugin \"{$tag}\" not callable"); throw new SmartyException("Plugin \"{$tag}\" not callable");
} else { } 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; return $this;
@@ -150,8 +147,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function unregisterPlugin($type, $tag) public function unregisterPlugin($type, $tag)
{ {
if (isset($this->smarty->registered_plugins[$type][$tag])) { $smarty = isset($this->smarty) ? $this->smarty : $this;
unset($this->smarty->registered_plugins[$type][$tag]); if (isset($smarty->registered_plugins[$type][$tag])) {
unset($smarty->registered_plugins[$type][$tag]);
} }
return $this; return $this;
@@ -167,7 +165,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function registerResource($type, $callback) 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; return $this;
} }
@@ -181,8 +180,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function unregisterResource($type) public function unregisterResource($type)
{ {
if (isset($this->smarty->registered_resources[$type])) { $smarty = isset($this->smarty) ? $this->smarty : $this;
unset($this->smarty->registered_resources[$type]); if (isset($smarty->registered_resources[$type])) {
unset($smarty->registered_resources[$type]);
} }
return $this; return $this;
@@ -198,7 +198,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function registerCacheResource($type, Smarty_CacheResource $callback) 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; return $this;
} }
@@ -212,8 +213,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function unregisterCacheResource($type) public function unregisterCacheResource($type)
{ {
if (isset($this->smarty->registered_cache_resources[$type])) { $smarty = isset($this->smarty) ? $this->smarty : $this;
unset($this->smarty->registered_cache_resources[$type]); if (isset($smarty->registered_cache_resources[$type])) {
unset($smarty->registered_cache_resources[$type]);
} }
return $this; return $this;
@@ -250,7 +252,8 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
} }
} }
// register the object // 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); array($object_impl, (array) $allowed, (boolean) $smarty_args, (array) $block_methods);
return $this; return $this;
@@ -266,14 +269,15 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function getRegisteredObject($name) 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"); 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"); 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) public function unregisterObject($name)
{ {
if (isset($this->smarty->registered_objects[$name])) { $smarty = isset($this->smarty) ? $this->smarty : $this;
unset($this->smarty->registered_objects[$name]); if (isset($smarty->registered_objects[$name])) {
unset($smarty->registered_objects[$name]);
} }
return $this; 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"); throw new SmartyException("Undefined class '$class_impl' in register template class");
} }
// register the 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; return $this;
} }
@@ -323,8 +329,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function registerDefaultPluginHandler($callback) public function registerDefaultPluginHandler($callback)
{ {
$smarty = isset($this->smarty) ? $this->smarty : $this;
if (is_callable($callback)) { if (is_callable($callback)) {
$this->smarty->default_plugin_handler_func = $callback; $smarty->default_plugin_handler_func = $callback;
} else { } else {
throw new SmartyException("Default plugin handler '$callback' not callable"); 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) 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; return $this;
} }
@@ -386,8 +394,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
public function unregisterFilter($type, $callback) public function unregisterFilter($type, $callback)
{ {
$name = $this->_get_filter_name($callback); $name = $this->_get_filter_name($callback);
if (isset($this->smarty->registered_filters[$type][$name])) { $smarty = isset($this->smarty) ? $this->smarty : $this;
unset($this->smarty->registered_filters[$type][$name]); if (isset($smarty->registered_filters[$type][$name])) {
unset($smarty->registered_filters[$type][$name]);
} }
return $this; return $this;
@@ -422,14 +431,15 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function loadFilter($type, $name) public function loadFilter($type, $name)
{ {
$smarty = isset($this->smarty) ? $this->smarty : $this;
$_plugin = "smarty_{$type}filter_{$name}"; $_plugin = "smarty_{$type}filter_{$name}";
$_filter_name = $_plugin; $_filter_name = $_plugin;
if ($this->smarty->loadPlugin($_plugin)) { if ($smarty->loadPlugin($_plugin)) {
if (class_exists($_plugin, false)) { if (class_exists($_plugin, false)) {
$_plugin = array($_plugin, 'execute'); $_plugin = array($_plugin, 'execute');
} }
if (is_callable($_plugin)) { if (is_callable($_plugin)) {
$this->smarty->registered_filters[$type][$_filter_name] = $_plugin; $smarty->registered_filters[$type][$_filter_name] = $_plugin;
return true; return true;
} }
@@ -447,9 +457,10 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
*/ */
public function unloadFilter($type, $name) public function unloadFilter($type, $name)
{ {
$smarty = isset($this->smarty) ? $this->smarty : $this;
$_filter_name = "smarty_{$type}filter_{$name}"; $_filter_name = "smarty_{$type}filter_{$name}";
if (isset($this->smarty->registered_filters[$type][$_filter_name])) { if (isset($smarty->registered_filters[$type][$_filter_name])) {
unset ($this->smarty->registered_filters[$type][$_filter_name]); unset ($smarty->registered_filters[$type][$_filter_name]);
} }
return $this; return $this;
@@ -481,10 +492,6 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
static $_resolved_property_name = array(); static $_resolved_property_name = array();
static $_resolved_property_source = 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 // see if this is a set/get for a property
$first3 = strtolower(substr($name, 0, 3)); $first3 = strtolower(substr($name, 0, 3));
if (isset($_prefixes[$first3]) && isset($name[3]) && $name[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; $_resolved_property_name[$name] = $property_name;
} }
if (isset($_resolved_property_source[$property_name])) { if (isset($_resolved_property_source[$property_name])) {
$_is_this = $_resolved_property_source[$property_name]; $status = $_resolved_property_source[$property_name];
} else { } else {
$_is_this = null; $status = null;
if (property_exists($this, $property_name)) { if (property_exists($this, $property_name)) {
$_is_this = true; $status = true;
} elseif (property_exists($this->smarty, $property_name)) { } 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) { $smarty = null;
if ($first3 == 'get') { if ($status === true) {
return $this->$property_name; $smarty = $this;
} else { } elseif ($status === false) {
return $this->$property_name = $args[0]; $smarty = $this->smarty;
}
} 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.");
} }
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'."); throw new SmartyException("Call of unknown method '$name'.");
} }
} }

View File

@@ -250,15 +250,16 @@ abstract class Smarty_Resource
*/ */
public static function getUniqueTemplateName($template, $template_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 // 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? // go relative to a given template?
$_file_is_dotted = $name[0] == '.' && ($name[1] == '.' || $name[1] == '/'); $_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')) { 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; $name = dirname($template->source->filepath) . DS . $name;
} }
return $resource->buildUniqueResourceName($template->smarty, $name); return $resource->buildUniqueResourceName($smarty, $name);
} }
/** /**