From 131553272a30ab72808a929b559684690895c013 Mon Sep 17 00:00:00 2001 From: rodneyrehm Date: Sat, 15 Oct 2011 10:46:03 +0000 Subject: [PATCH] - bugfix Smarty_Resource and Smarty_CacheResource runtime caching (Forum Post 75264) www.smarty.net/forums/viewtopic.php?p=75264#75264 --- change_log.txt | 3 ++ libs/Smarty.class.php | 11 ++++ libs/sysplugins/smarty_cacheresource.php | 27 ++++++---- libs/sysplugins/smarty_resource.php | 64 ++++++++++++++---------- 4 files changed, 69 insertions(+), 36 deletions(-) diff --git a/change_log.txt b/change_log.txt index 089063df..6308219a 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@ ===== trunk ===== +15.10.2011 +- bugfix Smarty_Resource and Smarty_CacheResource runtime caching (Forum Post 75264) + 14.10.2011 - bugfix unique_resource did not properly apply to compiled resources (Forum Topic 20128) - add locking to custom resources (Forum Post 75252) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 70f4999f..fd9dc627 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -493,11 +493,21 @@ class Smarty extends Smarty_Internal_TemplateBase { */ public $registered_resources = array(); /** + * resource handler cache + * @var array + */ + public $_resource_handlers = array(); + /** * registered cache resources * @var array */ public $registered_cache_resources = array(); /** + * cache resource handler cache + * @var array + */ + public $_cacheresource_handlers = array(); + /** * autoload filter * @var array */ @@ -584,6 +594,7 @@ class Smarty extends Smarty_Internal_TemplateBase { $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']); } } + /** * Class destructor diff --git a/libs/sysplugins/smarty_cacheresource.php b/libs/sysplugins/smarty_cacheresource.php index 28df37a8..d2be6f62 100644 --- a/libs/sysplugins/smarty_cacheresource.php +++ b/libs/sysplugins/smarty_cacheresource.php @@ -18,14 +18,14 @@ abstract class Smarty_CacheResource { * cache for Smarty_CacheResource instances * @var array */ - protected static $resources = array(); + public static $resources = array(); /** * resource types provided by the core * @var array */ protected static $sysplugins = array( - 'file' => true, + 'file' => true, ); /** @@ -148,25 +148,32 @@ abstract class Smarty_CacheResource { if (!isset($type)) { $type = $smarty->caching_type; } - // try the instance cache - if (isset(self::$resources[$type])) { - // FIXME: rodneyrehm need to validate if cache resource may be used in given $smarty. - return self::$resources[$type]; + + // try smarty's cache + if (isset($smarty->_cacheresource_handlers[$type])) { + return $smarty->_cacheresource_handlers[$type]; } + // try registered resource if (isset($smarty->registered_cache_resources[$type])) { // do not cache these instances as they may vary from instance to instance - return $smarty->registered_cache_resources[$type]; + return $smarty->_cacheresource_handlers[$type] = $smarty->registered_cache_resources[$type]; } // try sysplugins dir if (isset(self::$sysplugins[$type])) { - $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type); - return self::$resources[$type] = new $cache_resource_class(); + if (!isset(self::$resources[$type])) { + $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type); + self::$resources[$type] = new $cache_resource_class(); + } + return $smarty->_cacheresource_handlers[$type] = self::$resources[$type]; } // try plugins dir $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type); if ($smarty->loadPlugin($cache_resource_class)) { - return self::$resources[$type] = new $cache_resource_class(); + if (!isset(self::$resources[$type])) { + self::$resources[$type] = new $cache_resource_class(); + } + return $smarty->_cacheresource_handlers[$type] = self::$resources[$type]; } // give up throw new SmartyException("Unable to load cache resource '{$type}'"); diff --git a/libs/sysplugins/smarty_resource.php b/libs/sysplugins/smarty_resource.php index f2e3c1b2..afe702bb 100644 --- a/libs/sysplugins/smarty_resource.php +++ b/libs/sysplugins/smarty_resource.php @@ -331,68 +331,80 @@ abstract class Smarty_Resource { /** * Load Resource Handler * - * @param Smarty $smarty smarty object - * @param string $resource_type name of the resource + * @param Smarty $smarty smarty object + * @param string $type name of the resource * @return Smarty_Resource Resource Handler */ - public static function load(Smarty $smarty, $resource_type) + public static function load(Smarty $smarty, $type) { - // try the instance cache - if (isset(self::$resources[$resource_type])) { - // FIXME: rodneyrehm need to validate if resource may be used in given $smarty. - return self::$resources[$resource_type]; + // try smarty's cache + if (isset($smarty->_resource_handlers[$type])) { + return $smarty->_resource_handlers[$type]; } // try registered resource - if (isset($smarty->registered_resources[$resource_type])) { - if ($smarty->registered_resources[$resource_type] instanceof Smarty_Resource) { - return self::$resources[$resource_type] = $smarty->registered_resources[$resource_type]; + if (isset($smarty->registered_resources[$type])) { + if ($smarty->registered_resources[$type] instanceof Smarty_Resource) { + $smarty->_resource_handlers[$type] = $smarty->registered_resources[$type]; + // note registered to smarty is not kept unique! + return $smarty->_resource_handlers[$type]; } if (!isset(self::$resources['registered'])) { self::$resources['registered'] = new Smarty_Internal_Resource_Registered(); + $smarty->_resource_handlers[$type] = self::$resources['registered']; } - return self::$resources['registered']; + return $smarty->_resource_handlers[$type]; } // try sysplugins dir - if (isset(self::$sysplugins[$resource_type])) { - $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($resource_type); - return self::$resources[$resource_type] = new $_resource_class(); + if (isset(self::$sysplugins[$type])) { + if (!isset(self::$resources[$type])) { + $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type); + self::$resources[$type] = new $_resource_class(); + } + return $smarty->_resource_handlers[$type] = self::$resources[$type]; } // try plugins dir - $_resource_class = 'Smarty_Resource_' . ucfirst($resource_type); + $_resource_class = 'Smarty_Resource_' . ucfirst($type); if ($smarty->loadPlugin($_resource_class)) { + if (isset(self::$resources[$type])) { + return $smarty->_resource_handlers[$type] = self::$resources[$type]; + } + if (class_exists($_resource_class, false)) { - return self::$resources[$resource_type] = new $_resource_class(); + self::$resources[$type] = new $_resource_class(); + return $smarty->_resource_handlers[$type] = self::$resources[$type]; } else { - $smarty->registerResource($resource_type, - array("smarty_resource_{$resource_type}_source", - "smarty_resource_{$resource_type}_timestamp", - "smarty_resource_{$resource_type}_secure", - "smarty_resource_{$resource_type}_trusted")); + $smarty->registerResource($type, array( + "smarty_resource_{$type}_source", + "smarty_resource_{$type}_timestamp", + "smarty_resource_{$type}_secure", + "smarty_resource_{$type}_trusted" + )); + // give it another try, now that the resource is registered properly - return self::load($smarty, $resource_type); + return self::load($smarty, $type); } } // try streams $_known_stream = stream_get_wrappers(); - if (in_array($resource_type, $_known_stream)) { + if (in_array($type, $_known_stream)) { // is known stream if (is_object($smarty->security_policy)) { - $smarty->security_policy->isTrustedStream($resource_type); + $smarty->security_policy->isTrustedStream($type); } if (!isset(self::$resources['stream'])) { self::$resources['stream'] = new Smarty_Internal_Resource_Stream(); } - return self::$resources['stream']; + return $smarty->_resource_handlers[$type] = self::$resources['stream']; } // TODO: try default_(template|config)_handler // give up - throw new SmartyException('Unkown resource type \'' . $resource_type . '\''); + throw new SmartyException("Unkown resource type '{$type}'"); } /**