ZF-2468: using own frontends and/or backends
Description
Now the way to use own frontend or backend objects this objects must named Zend_Cache_[Frontend|Backend], because the static arrays in Zend_Cache only contain the last class name (e.g 'File' for Zend_Cache_[Frontend|Backend]_File )
I think it is better to store the last class name as lower case and the full class name in this arrays like:
public static $standardFrontends = array(
'core' => 'Zend_Cache_Core',
'output' => 'Zend_Cache_Frontend_Output',
'class' => 'Zend_Cache_Frontend_Class',
// ...
);
and the same for the backend array.
Then in the method Zend_Cache::factory you can handle it as follow:
$frontendLower = strtolower($frontend);
if (isset(self::$standardFrontends[$frontendLower])) {
$frontendClass = self::$standardFrontends[$frontendLower];
} else {
$frontendClass = self::_normalizeName($frontend);
}
// self::$standardFrontends is public and can therefore be change from the outside
Zend_Loader::loadClass($frontendClass);
$frontendObject = new $frontendClass($frontendOptions);
$backendLower = strtolower($backend);
if (isset(self::$standardBackends[$backendLower])) {
$backendClass = self::$standardBackends[$backendLower];
} else {
$backendClass = self::_normalizeName($backend);
}
// self::$standardBackends is public and can therefore be change from the outside
Zend_Loader::loadClass($backendClass);
$backendObject = new $backendClass($backendOptions);
// ...
I use the self::$standard* arrays because on the self::$available* arrays is the comment "Only for backward compatibily (will be removed in 1.2.0)"
Now you can use the factory methode as follow:
Zend_Cache::factory('Core', 'File');
// or
Zend_Cache::factory('My_Own_Frontend', 'My_Own_Backend');
// or
Zend_Cache::$standardFrontends['Core'] = 'My_Own_Frontend';
Zend_Cache::$standardBackends['File'] = 'My_Own_Backend';
Zend_Cache::factory('core', 'file');
Comments
Posted by Vincent de Lau (vdelau) on 2008-03-20T04:47:56.000+0000
I realy like the array approach for determining standard classes in the factory, but a cleaner solution to modify the array might be to add:
Another approach might be to move the classname resolution to seperate protected functions, so that subclassing Zend_Cache is easier.
Posted by Marc Bennewitz (GIATA mbH) (mben) on 2008-03-20T17:59:27.000+0000
this sounds good
Posted by Fabien MARTY (fab) on 2008-04-04T17:29:39.000+0000
I just commited something in trunk SVN.
Can you have a look at it ?
So, to use a completly custom frontend, you can use something like :
Zend_Cache::factory('MyCustomFrontend','MyCustomBackend',array(...),array(...), true, true, false); (without autoload)
Zend_Cache will do a require_once "My/Custom/Frontend.php" (the include_path must be ok for that)
or :
Zend_Cache::factory('MyCustomFrontend','MyCustomBackend',array(...),array(...), true, true, true); (with autoload)
Zend_Cache won't do any require_once at all (but the autoload has to work for MyCustomFrontend)
Posted by Vincent de Lau (vdelau) on 2008-04-05T04:04:24.000+0000
In my current project, i had to have my dummy backend. I subclassed Zend_Cache like this:
Of course I used the occasion to use the Zend_Loader instead of require, although I also use an autoloader. I would like to see this add* interface and only a fifth argument on the factory to enable autoloading. Default classed should be in the assoc. array, instead of using this workaround. This makes loading custom classes transparent and keeps the argumentlist clean.
Posted by Wil Sinclair (wil) on 2008-09-02T10:39:37.000+0000
Updating for the 1.6.0 release.