Add/use Horde_Session based prefs caching driver
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 15 Oct 2010 19:06:38 +0000 (13:06 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 15 Oct 2010 19:56:38 +0000 (13:56 -0600)
framework/Core/lib/Horde/Core/Factory/Prefs.php
framework/Core/lib/Horde/Core/Prefs/Cache/Session.php [new file with mode: 0644]
framework/Core/lib/Horde/Registry.php
framework/Core/package.xml

index af1ccd2..b9bce66 100644 (file)
@@ -84,7 +84,7 @@ class Horde_Core_Factory_Prefs
         }
 
         $opts = array_merge(array(
-            'cache' => 'Horde_Prefs_Cache_Session',
+            'cache' => 'Horde_Core_Prefs_Cache_Session',
             'charset' => 'UTF-8',
             'logger' => $this->_injector->getInstance('Horde_Log_Logger'),
             'password' => '',
@@ -93,6 +93,11 @@ class Horde_Core_Factory_Prefs
         ), $opts);
         ksort($opts);
 
+        /* Allow no caching to be specified as false-y value. */
+        if (!$opts['cache']) {
+            unset($opts['cache']);
+        }
+
         /* If $params['user_hook'] is defined, use it to retrieve the value to
          * use for the username. */
         if (!empty($params['user_hook']) &&
diff --git a/framework/Core/lib/Horde/Core/Prefs/Cache/Session.php b/framework/Core/lib/Horde/Core/Prefs/Cache/Session.php
new file mode 100644 (file)
index 0000000..2ae9f26
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Session storage cache driver (using Horde_Session) for the preferences
+ * system.
+ *
+ * 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.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  Core
+ */
+class Horde_Core_Prefs_Cache_Session extends Horde_Prefs_Cache
+{
+    /**
+     * Session key.
+     *
+     * @var string
+     */
+    protected $_key;
+
+    /**
+     */
+    public function __construct($user)
+    {
+        parent::__construct($user);
+
+        $this->_key = 'horde:prefs_' . $this->_user . '/';
+    }
+
+    /**
+     */
+    public function get($scope)
+    {
+        global $session;
+
+        return isset($session[$this->_key . $scope])
+            ? $session[$this->_key . $scope]
+            : false;
+    }
+
+    /**
+     */
+    public function update($scope, $prefs)
+    {
+        if (($cached = $this->get($scope)) === false) {
+            $cached = array();
+        }
+        $cached = array_merge($cached, $prefs);
+        $GLOBALS['session'][$this->_key . $scope] = $cached;
+    }
+
+    /**
+     */
+    public function clear($scope = null, $pref = null)
+    {
+        global $session;
+
+        if (is_null($scope)) {
+            unset($session[$this->_key]);
+        } elseif (is_null($pref)) {
+            unset($session[$this->_key . $scope]);
+        } elseif ((($cached = $this->get($scope)) !== false) &&
+                  isset($cached[$pref])) {
+            unset($cached[$pref]);
+            $session[$this->_key . $scope] = $cached;
+        }
+    }
+
+}
index 7e59ede..1e1e432 100644 (file)
@@ -1345,7 +1345,7 @@ class Horde_Registry
             /* If there is no logged in user, return an empty Horde_Prefs::
              * object with just default preferences. */
             $opts = array(
-                'cache' => false,
+                'cache' => 'Horde_Prefs_Cache_Null',
                 'session' => true
             );
         }
index 01e3e0b..f781d4a 100644 (file)
@@ -184,6 +184,9 @@ Application Framework.</description>
        <file name="Ui.php" role="php" />
       </dir> <!-- /lib/Horde/Core/Perms -->
       <dir name="Prefs">
+       <dir name="Cache">
+        <file name="Session.php" role="php" />
+       </dir> <!-- /lib/Horde/Core/Prefs/Cache -->
        <dir name="Ui">
         <file name="Widgets.php" role="php" />
        </dir> <!-- /lib/Horde/Core/Prefs/Ui -->
@@ -479,6 +482,7 @@ Application Framework.</description>
    <install as="Horde/Core/Notification/Hordelog.php" name="lib/Horde/Core/Notification/Hordelog.php" />
    <install as="Horde/Core/Notification/Status.php" name="lib/Horde/Core/Notification/Status.php" />
    <install as="Horde/Core/Perms/Ui.php" name="lib/Horde/Core/Perms/Ui.php" />
+   <install as="Horde/Core/Prefs/Cache/Session.php" name="lib/Horde/Core/Prefs/Cache/Session.php" />
    <install as="Horde/Core/Prefs/Identity.php" name="lib/Horde/Core/Prefs/Identity.php" />
    <install as="Horde/Core/Prefs/Session.php" name="lib/Horde/Core/Prefs/Session.php" />
    <install as="Horde/Core/Prefs/Ui.php" name="lib/Horde/Core/Prefs/Ui.php" />