Add Horde_Cache factory object
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 3 Sep 2010 18:41:04 +0000 (12:41 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 3 Sep 2010 19:59:29 +0000 (13:59 -0600)
Provides a way to default to the Session cache driver, instead of the
Null driver.

framework/Core/lib/Horde/Core/Binder/Cache.php
framework/Core/lib/Horde/Core/Binder/CacheFactory.php [new file with mode: 0644]
framework/Core/lib/Horde/Core/Factory/Cache.php [new file with mode: 0644]
framework/Core/lib/Horde/Registry.php
framework/Core/package.xml

index 19c02d9..b8366d9 100644 (file)
@@ -7,52 +7,8 @@ class Horde_Core_Binder_Cache implements Horde_Injector_Binder
 {
     public function create(Horde_Injector $injector)
     {
-        $driver = empty($GLOBALS['conf']['cache']['driver'])
-            ? 'Null'
-            : $GLOBALS['conf']['cache']['driver'];
-        if (strcasecmp($driver, 'None') === 0) {
-            $driver = 'Null';
-        }
-
-        $params = Horde::getDriverConfig('cache', $driver);
-        if (isset($GLOBALS['conf']['cache']['default_lifetime'])) {
-            $params['lifetime'] = $GLOBALS['conf']['cache']['default_lifetime'];
-        }
-
-        $logger = $injector->getInstance('Horde_Log_Logger');
-        $params['logger'] = $logger;
-
-        $base_params = $params;
-
-        if (strcasecmp($driver, 'Memcache') === 0) {
-            $params['memcache'] = $injector->getInstance('Horde_Memcache');
-        } elseif (strcasecmp($driver, 'Sql') === 0) {
-            $params['db'] = $injector->getInstance('Horde_Db')->getDb('horde', 'cache');
-        }
-
-        if (!empty($GLOBALS['conf']['cache']['use_memorycache']) &&
-            ((strcasecmp($driver, 'Sql') === 0) ||
-             (strcasecmp($driver, 'File') === 0))) {
-            if (strcasecmp($GLOBALS['conf']['cache']['use_memorycache'], 'Memcache') === 0) {
-                $base_params['memcache'] = $injector->getInstance('Horde_Memcache');
-            }
-
-            $params = array(
-                'stack' => array(
-                    array(
-                        'driver' => $GLOBALS['conf']['cache']['use_memorycache'],
-                        'params' => $base_params
-                    ),
-                    array(
-                        'driver' => $driver,
-                        'params' => $params
-                    )
-                )
-            );
-            $driver = 'Stack';
-        }
-
-        return Horde_Cache::factory($driver, $params);
+        $cache = new Horde_Core_Factory_Cache($injector);
+        return $cache->getCache();
     }
 
     public function equals(Horde_Injector_Binder $binder)
diff --git a/framework/Core/lib/Horde/Core/Binder/CacheFactory.php b/framework/Core/lib/Horde/Core/Binder/CacheFactory.php
new file mode 100644 (file)
index 0000000..53f7d44
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+/**
+ * @category Horde
+ * @package  Core
+ */
+class Horde_Core_Binder_CacheFactory implements Horde_Injector_Binder
+{
+    public function create(Horde_Injector $injector)
+    {
+        return new Horde_Core_Factory_Cache($injector);
+    }
+
+    public function equals(Horde_Injector_Binder $binder)
+    {
+        return false;
+    }
+
+}
diff --git a/framework/Core/lib/Horde/Core/Factory/Cache.php b/framework/Core/lib/Horde/Core/Factory/Cache.php
new file mode 100644 (file)
index 0000000..518bb5c
--- /dev/null
@@ -0,0 +1,125 @@
+<?php
+/**
+ * A Horde_Injector:: based Horde_Cache:: factory.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Core
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Core
+ */
+
+/**
+ * A Horde_Injector:: based Horde_Cache:: factory.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Core
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Core
+ */
+class Horde_Core_Factory_Cache
+{
+    /**
+     * The injector.
+     *
+     * @var Horde_Injector
+     */
+    private $_injector;
+
+    /**
+     * Singleton instances.
+     *
+     * @var array
+     */
+    private $_instances = array();
+
+    /**
+     * Constructor.
+     *
+     * @param Horde_Injector $injector  The injector to use.
+     */
+    public function __construct(Horde_Injector $injector)
+    {
+        $this->_injector = $injector;
+    }
+
+    /**
+     * Return the Horde_Cache:: instance.
+     *
+     * @param array $opts  Options:
+     * <pre>
+     * 'session' - (boolean) Fallback to session driver, instead of null
+     *             driver, if no cache config is found.
+     *             DEFAULT: false
+     * </pre>
+     *
+     * @return Horde_Cache_Base  The singleton instance.
+     * @throws Horde_Cache_Exception
+     */
+    public function getCache(array $opts = array())
+    {
+        $driver = empty($GLOBALS['conf']['cache']['driver'])
+            ? 'Null'
+            : $GLOBALS['conf']['cache']['driver'];
+        if (strcasecmp($driver, 'None') === 0) {
+            $driver = 'Null';
+        }
+
+        if (($driver == 'Null') && !empty($opts['session'])) {
+            $driver = 'Session';
+        }
+
+        if (!isset($this->_instances[$driver])) {
+            $params = Horde::getDriverConfig('cache', $driver);
+            if (isset($GLOBALS['conf']['cache']['default_lifetime'])) {
+                $params['lifetime'] = $GLOBALS['conf']['cache']['default_lifetime'];
+            }
+
+            $logger = $this->_injector->getInstance('Horde_Log_Logger');
+            $params['logger'] = $logger;
+
+            $base_params = $params;
+
+            if (strcasecmp($driver, 'Memcache') === 0) {
+                $params['memcache'] = $this->_injector->getInstance('Horde_Memcache');
+            } elseif (strcasecmp($driver, 'Sql') === 0) {
+                $params['db'] = $this->_injector->getInstance('Horde_Db')->getDb('horde', 'cache');
+            }
+
+            if (!empty($GLOBALS['conf']['cache']['use_memorycache']) &&
+                ((strcasecmp($driver, 'Sql') === 0) ||
+                 (strcasecmp($driver, 'File') === 0))) {
+                if (strcasecmp($GLOBALS['conf']['cache']['use_memorycache'], 'Memcache') === 0) {
+                    $base_params['memcache'] = $this->_injector->getInstance('Horde_Memcache');
+                }
+
+                $params = array(
+                    'stack' => array(
+                        array(
+                            'driver' => $GLOBALS['conf']['cache']['use_memorycache'],
+                            'params' => $base_params
+                        ),
+                        array(
+                            'driver' => $driver,
+                            'params' => $params
+                        )
+                    )
+                );
+                $driver = 'Stack';
+            }
+
+            $this->_instances[$driver] = Horde_Cache::factory($driver, $params);
+        }
+
+        return $this->_instances[$driver];
+    }
+
+}
index e9b912a..1e9265b 100644 (file)
@@ -266,6 +266,7 @@ implements Horde_Interfaces_Registry_Auth
             'Horde_Auth' => 'Horde_Core_Binder_Auth',
             // 'Horde_Browser' - initialized below
             'Horde_Cache' => 'Horde_Core_Binder_Cache',
+            'Horde_Cache_Factory' => 'Horde_Core_Binder_CacheFactory',
             'Horde_Core_Auth_Signup' => 'Horde_Core_Binder_AuthSignup',
             'Horde_Crypt' => 'Horde_Core_Binder_Crypt',
             'Horde_Data' => 'Horde_Core_Binder_Data',
index 6be6da2..ba2b3e8 100644 (file)
@@ -112,6 +112,7 @@ Application Framework.</description>
        <file name="Auth.php" role="php" />
        <file name="AuthSignup.php" role="php" />
        <file name="Cache.php" role="php" />
+       <file name="CacheFactory.php" role="php" />
        <file name="Crypt.php" role="php" />
        <file name="Data.php" role="php" />
        <file name="Db.php" role="php" />
@@ -153,6 +154,7 @@ Application Framework.</description>
       <dir name="Factory">
        <file name="Ajax.php" role="php" />
        <file name="Auth.php" role="php" />
+       <file name="Cache.php" role="php" />
        <file name="Crypt.php" role="php" />
        <file name="Data.php" role="php" />
        <file name="Db.php" role="php" />
@@ -437,6 +439,7 @@ Application Framework.</description>
    <install as="Horde/Core/Binder/Auth.php" name="lib/Horde/Core/Binder/Auth.php" />
    <install as="Horde/Core/Binder/AuthSignup.php" name="lib/Horde/Core/Binder/AuthSignup.php" />
    <install as="Horde/Core/Binder/Cache.php" name="lib/Horde/Core/Binder/Cache.php" />
+   <install as="Horde/Core/Binder/CacheFactory.php" name="lib/Horde/Core/Binder/CacheFactory.php" />
    <install as="Horde/Core/Binder/Crypt.php" name="lib/Horde/Core/Binder/Crypt.php" />
    <install as="Horde/Core/Binder/Data.php" name="lib/Horde/Core/Binder/Data.php" />
    <install as="Horde/Core/Binder/Db.php" name="lib/Horde/Core/Binder/Db.php" />
@@ -474,6 +477,7 @@ Application Framework.</description>
    <install as="Horde/Core/Controller/SettingsFinder.php" name="lib/Horde/Core/Controller/SettingsFinder.php" />
    <install as="Horde/Core/Factory/Ajax.php" name="lib/Horde/Core/Factory/Ajax.php" />
    <install as="Horde/Core/Factory/Auth.php" name="lib/Horde/Core/Factory/Auth.php" />
+   <install as="Horde/Core/Factory/Cache.php" name="lib/Horde/Core/Factory/Cache.php" />
    <install as="Horde/Core/Factory/Crypt.php" name="lib/Horde/Core/Factory/Crypt.php" />
    <install as="Horde/Core/Factory/Data.php" name="lib/Horde/Core/Factory/Data.php" />
    <install as="Horde/Core/Factory/Db.php" name="lib/Horde/Core/Factory/Db.php" />