- bugfix Smarty_Resource and Smarty_CacheResource runtime caching (Forum Post 75264)

www.smarty.net/forums/viewtopic.php?p=75264#75264
This commit is contained in:
rodneyrehm
2011-10-15 10:46:03 +00:00
parent 318328cd75
commit 131553272a
4 changed files with 69 additions and 36 deletions

View File

@@ -1,4 +1,7 @@
===== trunk ===== ===== trunk =====
15.10.2011
- bugfix Smarty_Resource and Smarty_CacheResource runtime caching (Forum Post 75264)
14.10.2011 14.10.2011
- bugfix unique_resource did not properly apply to compiled resources (Forum Topic 20128) - bugfix unique_resource did not properly apply to compiled resources (Forum Topic 20128)
- add locking to custom resources (Forum Post 75252) - add locking to custom resources (Forum Post 75252)

View File

@@ -493,11 +493,21 @@ class Smarty extends Smarty_Internal_TemplateBase {
*/ */
public $registered_resources = array(); public $registered_resources = array();
/** /**
* resource handler cache
* @var array
*/
public $_resource_handlers = array();
/**
* registered cache resources * registered cache resources
* @var array * @var array
*/ */
public $registered_cache_resources = array(); public $registered_cache_resources = array();
/** /**
* cache resource handler cache
* @var array
*/
public $_cacheresource_handlers = array();
/**
* autoload filter * autoload filter
* @var array * @var array
*/ */
@@ -585,6 +595,7 @@ class Smarty extends Smarty_Internal_TemplateBase {
} }
} }
/** /**
* Class destructor * Class destructor
*/ */

View File

@@ -18,7 +18,7 @@ abstract class Smarty_CacheResource {
* cache for Smarty_CacheResource instances * cache for Smarty_CacheResource instances
* @var array * @var array
*/ */
protected static $resources = array(); public static $resources = array();
/** /**
* resource types provided by the core * resource types provided by the core
@@ -148,25 +148,32 @@ abstract class Smarty_CacheResource {
if (!isset($type)) { if (!isset($type)) {
$type = $smarty->caching_type; $type = $smarty->caching_type;
} }
// try the instance cache
if (isset(self::$resources[$type])) { // try smarty's cache
// FIXME: rodneyrehm need to validate if cache resource may be used in given $smarty. if (isset($smarty->_cacheresource_handlers[$type])) {
return self::$resources[$type]; return $smarty->_cacheresource_handlers[$type];
} }
// try registered resource // try registered resource
if (isset($smarty->registered_cache_resources[$type])) { if (isset($smarty->registered_cache_resources[$type])) {
// do not cache these instances as they may vary from instance to instance // 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 // try sysplugins dir
if (isset(self::$sysplugins[$type])) { if (isset(self::$sysplugins[$type])) {
if (!isset(self::$resources[$type])) {
$cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type); $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
return self::$resources[$type] = new $cache_resource_class(); self::$resources[$type] = new $cache_resource_class();
}
return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
} }
// try plugins dir // try plugins dir
$cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type); $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
if ($smarty->loadPlugin($cache_resource_class)) { 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 // give up
throw new SmartyException("Unable to load cache resource '{$type}'"); throw new SmartyException("Unable to load cache resource '{$type}'");

View File

@@ -332,67 +332,79 @@ abstract class Smarty_Resource {
* Load Resource Handler * Load Resource Handler
* *
* @param Smarty $smarty smarty object * @param Smarty $smarty smarty object
* @param string $resource_type name of the resource * @param string $type name of the resource
* @return Smarty_Resource Resource Handler * @return Smarty_Resource Resource Handler
*/ */
public static function load(Smarty $smarty, $resource_type) public static function load(Smarty $smarty, $type)
{ {
// try the instance cache // try smarty's cache
if (isset(self::$resources[$resource_type])) { if (isset($smarty->_resource_handlers[$type])) {
// FIXME: rodneyrehm need to validate if resource may be used in given $smarty. return $smarty->_resource_handlers[$type];
return self::$resources[$resource_type];
} }
// try registered resource // try registered resource
if (isset($smarty->registered_resources[$resource_type])) { if (isset($smarty->registered_resources[$type])) {
if ($smarty->registered_resources[$resource_type] instanceof Smarty_Resource) { if ($smarty->registered_resources[$type] instanceof Smarty_Resource) {
return self::$resources[$resource_type] = $smarty->registered_resources[$resource_type]; $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'])) { if (!isset(self::$resources['registered'])) {
self::$resources['registered'] = new Smarty_Internal_Resource_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 // try sysplugins dir
if (isset(self::$sysplugins[$resource_type])) { if (isset(self::$sysplugins[$type])) {
$_resource_class = 'Smarty_Internal_Resource_' . ucfirst($resource_type); if (!isset(self::$resources[$type])) {
return self::$resources[$resource_type] = new $_resource_class(); $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type);
self::$resources[$type] = new $_resource_class();
}
return $smarty->_resource_handlers[$type] = self::$resources[$type];
} }
// try plugins dir // try plugins dir
$_resource_class = 'Smarty_Resource_' . ucfirst($resource_type); $_resource_class = 'Smarty_Resource_' . ucfirst($type);
if ($smarty->loadPlugin($_resource_class)) { if ($smarty->loadPlugin($_resource_class)) {
if (isset(self::$resources[$type])) {
return $smarty->_resource_handlers[$type] = self::$resources[$type];
}
if (class_exists($_resource_class, false)) { 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 { } else {
$smarty->registerResource($resource_type, $smarty->registerResource($type, array(
array("smarty_resource_{$resource_type}_source", "smarty_resource_{$type}_source",
"smarty_resource_{$resource_type}_timestamp", "smarty_resource_{$type}_timestamp",
"smarty_resource_{$resource_type}_secure", "smarty_resource_{$type}_secure",
"smarty_resource_{$resource_type}_trusted")); "smarty_resource_{$type}_trusted"
));
// give it another try, now that the resource is registered properly // give it another try, now that the resource is registered properly
return self::load($smarty, $resource_type); return self::load($smarty, $type);
} }
} }
// try streams // try streams
$_known_stream = stream_get_wrappers(); $_known_stream = stream_get_wrappers();
if (in_array($resource_type, $_known_stream)) { if (in_array($type, $_known_stream)) {
// is known stream // is known stream
if (is_object($smarty->security_policy)) { if (is_object($smarty->security_policy)) {
$smarty->security_policy->isTrustedStream($resource_type); $smarty->security_policy->isTrustedStream($type);
} }
if (!isset(self::$resources['stream'])) { if (!isset(self::$resources['stream'])) {
self::$resources['stream'] = new Smarty_Internal_Resource_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 // TODO: try default_(template|config)_handler
// give up // give up
throw new SmartyException('Unkown resource type \'' . $resource_type . '\''); throw new SmartyException("Unkown resource type '{$type}'");
} }
/** /**