From: Michael M Slusarz Date: Thu, 4 Nov 2010 18:09:04 +0000 (-0600) Subject: Allow array of storage objects to be passed to Horde_Prefs X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=0959120abadd553307e97517e2fa3f3145a044cf;p=horde.git Allow array of storage objects to be passed to Horde_Prefs --- diff --git a/framework/Core/lib/Horde/Core/Factory/Prefs.php b/framework/Core/lib/Horde/Core/Factory/Prefs.php index e5403df7d..4d0fec07c 100644 --- a/framework/Core/lib/Horde/Core/Factory/Prefs.php +++ b/framework/Core/lib/Horde/Core/Factory/Prefs.php @@ -70,14 +70,13 @@ class Horde_Core_Factory_Prefs !empty($opts['session'])) { $driver = 'Horde_Core_Prefs_Storage_Session'; $params = array(); - unset($opts['session']); } else { - $driver = ucfirst($GLOBALS['conf']['prefs']['driver']); + $driver = 'Horde_Prefs_Storage_' . ucfirst($GLOBALS['conf']['prefs']['driver']); $params = Horde::getDriverConfig('prefs', $driver); } $opts = array_merge(array( - 'cache' => 'Horde_Core_Prefs_Storage_Session', + 'cache' => true, 'charset' => 'UTF-8', 'logger' => $this->_injector->getInstance('Horde_Log_Logger'), 'password' => '', @@ -86,11 +85,6 @@ 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']) && @@ -104,22 +98,32 @@ class Horde_Core_Factory_Prefs $this->_instances[$sig]->retrieve($scope); } else { switch ($driver) { - case 'Ldap': + case 'Horde_Prefs_Storage_Ldap': $params['ldap'] = $this->_injector->getInstance('Horde_Core_Factory_Ldap')->getLdap('horde', 'ldap'); break; - case 'Session': - unset($opts['cache']); + case 'Horde_Prefs_Storage_Session': + $opts['cache'] = false; break; - case 'Sql': + case 'Horde_Prefs_Storage_Sql': $params['db'] = $this->_injector->getInstance('Horde_Db_Adapter'); $opts['charset'] = $params['db']->getOption('charset'); break; } + $drivers = array( + new $driver($opts['user'], $params) + ); + + if ($opts['cache']) { + $opts['cache'] = new Horde_Core_Prefs_Storage_Session($opts['user']); + } else { + unset($opts['cache']); + } + try { - $this->_instances[$sig] = new Horde_Core_Prefs($driver, $scope, $opts, $params); + $this->_instances[$sig] = new Horde_Core_Prefs($scope, $drivers, $opts); } catch (Horde_Prefs_Exception $e) { if (!$GLOBALS['session']->get('horde', 'no_prefs')) { $GLOBALS['session']->set('horde', 'no_prefs', true); @@ -128,7 +132,7 @@ class Horde_Core_Factory_Prefs } } unset($opts['cache']); - $this->_instances[$sig] = new Horde_Core_Prefs('Horde_Core_Prefs_Storage_Session', $scope, $opts); + $this->_instances[$sig] = new Horde_Core_Prefs($scope, new Horde_Core_Prefs_Storage_Session($opts['user']), $opts); } } diff --git a/framework/Prefs/lib/Horde/Prefs.php b/framework/Prefs/lib/Horde/Prefs.php index a9220d58f..d911c1b64 100644 --- a/framework/Prefs/lib/Horde/Prefs.php +++ b/framework/Prefs/lib/Horde/Prefs.php @@ -79,18 +79,19 @@ class Horde_Prefs implements ArrayAccess protected $_scopes = array(); /** - * The storage driver. + * The storage driver(s). * - * @var Horde_Prefs_Storage + * @var array */ protected $_storage; /** * Constructor. * - * @param string $driver THe storage driver name. Either a driver name, - * or the full class name to use. * @param string $scope The scope for this set of preferences. + * @param mixed $storage The storage object(s) to use. Either a single + * Horde_Prefs_Storage object, or an array of + * objects. * @param array $opts Additional confguration options: *
      * REQUIRED:
@@ -111,13 +112,10 @@ class Horde_Prefs implements ArrayAccess
      * user - (string) The name of the user who owns this set of preferences.
      *        DEFAULT: NONE
      * 
- * @param array $params A hash containing any additional configuration - * or connection parameters a subclass might need. * * @throws InvalidArgumentException */ - public function __construct($driver, $scope, array $opts, - array $params = array()) + public function __construct($scope, $storage = null, array $opts = array()) { if (!isset($opts['charset'])) { throw new InvalidArgumentException(__CLASS__ . ': Missing charset parameter.'); @@ -125,43 +123,24 @@ class Horde_Prefs implements ArrayAccess $this->_opts = array_merge($this->_opts, $opts); - $this->_cache = $this->_getStorage($this->_opts['cache']); - $this->_scope = $scope; - $this->_storage = $this->_getStorage($driver, $params); - - register_shutdown_function(array($this, 'store')); - - $this->retrieve($scope); - } + $default = __CLASS__ . '_Storage_Null'; - /** - * Instantiate storage driver. - * - * @param string $driver Storage driver name. - * @param array $params Storage driver parameters. - * - * @return Horde_Prefs_Storage The storage object. - * @throws Horde_Prefs_Exception - */ - protected function _getStorage($driver, $params = array()) - { - if (is_null($driver)) { - $class = __CLASS__ . '_Storage_Null'; + $this->_cache = isset($this->_opts['cache']) + ? $this->_opts['cache'] + : new $default($this->getUser()); + $this->_scope = $scope; + if (is_null($storage)) { + $this->_storage = array(new $default($this->getUser())); } else { - /* Built-in drivers (in Storage/ directory). */ - $class = __CLASS__ . '_Storage_' . $driver; - if (!class_exists($class)) { - /* Explicit class name, */ - $class = $driver; - if (!class_exists($class)) { - throw new Horde_Prefs_Exception(__CLASS__ . ': class definition not found - ' . $class); - } + if (!is_array($storage)) { + $storage = array($storage); } + $this->_storage = $storage; } - $params['user'] = $this->getUser(); + register_shutdown_function(array($this, 'store')); - return new $class($params); + $this->retrieve($scope); } /** @@ -217,10 +196,12 @@ class Horde_Prefs implements ArrayAccess $this->_scopes[$scope][$pref] ); - try { - $this->_storage->remove($scope, $pref); - } catch (Horde_Prefs_Exception $e) { - // TODO: logging + foreach ($this->_storage as $storage) { + try { + $storage->remove($scope, $pref); + } catch (Horde_Prefs_Exception $e) { + // TODO: logging + } } try { @@ -559,19 +540,21 @@ class Horde_Prefs implements ArrayAccess $this->_loadScopePre($scope); - if (($prefs = $this->_storage->get($scope)) !== false) { - foreach ($prefs as $name => $val) { - if (isset($this->_scopes[$scope][$name])) { - if ($this->isDefault($name)) { - $this->_scopes[$scope][$name]['d'] = $this->_scopes[$scope][$name]['v']; + foreach ($this->_storage as $storage) { + if (($prefs = $storage->get($scope)) !== false) { + foreach ($prefs as $name => $val) { + if (isset($this->_scopes[$scope][$name])) { + if ($this->isDefault($name)) { + $this->_scopes[$scope][$name]['d'] = $this->_scopes[$scope][$name]['v']; + } + } else { + $this->_scopes[$scope][$name] = array( + 'm' => 0 + ); } - } else { - $this->_scopes[$scope][$name] = array( - 'm' => 0 - ); + $this->_scopes[$scope][$name]['v'] = $val; + $this->setDefault($name, false); } - $this->_scopes[$scope][$name]['v'] = $val; - $this->setDefault($name, false); } } @@ -607,16 +590,18 @@ class Horde_Prefs implements ArrayAccess public function store() { if (!empty($this->_dirty)) { - try { - $this->_storage->store($this->_dirty); - - /* Clear the dirty flag. */ - foreach ($this->_dirty as $k => $v) { - foreach (array_keys($v) as $name) { - $this->setDirty($name, false); + foreach ($this->_storage as $storage) { + try { + $storage->store($this->_dirty); + + /* Clear the dirty flag. */ + foreach ($this->_dirty as $k => $v) { + foreach (array_keys($v) as $name) { + $this->setDirty($name, false); + } } - } - } catch (Horde_Prefs_Exception $e) {} + } catch (Horde_Prefs_Exception $e) {} + } } } diff --git a/framework/Prefs/lib/Horde/Prefs/Storage.php b/framework/Prefs/lib/Horde/Prefs/Storage.php index 2655dde75..a2c28bd4f 100644 --- a/framework/Prefs/lib/Horde/Prefs/Storage.php +++ b/framework/Prefs/lib/Horde/Prefs/Storage.php @@ -16,6 +16,7 @@ abstract class Horde_Prefs_Storage { /** * Configuration parameters. + * 'user' is always available as an entry. * * @var string */ @@ -24,14 +25,13 @@ abstract class Horde_Prefs_Storage /** * Constructor. * - * @param array $params Configuration parameters. - *
-     * 'user' - (string) The current username.
-     * 
+ * @param string $user The username. + * @param array $params Additional configuration parameters. */ - public function __construct(array $params = array()) + public function __construct($user, array $params = array()) { - $this->_params = $params; + $this->_params = array_merge($this->_params, $params); + $this->_params['user'] = $user; } /** diff --git a/framework/Prefs/lib/Horde/Prefs/Storage/File.php b/framework/Prefs/lib/Horde/Prefs/Storage/File.php index 1815f9aef..e10a30fff 100644 --- a/framework/Prefs/lib/Horde/Prefs/Storage/File.php +++ b/framework/Prefs/lib/Horde/Prefs/Storage/File.php @@ -34,6 +34,7 @@ class Horde_Prefs_Storage_File extends Horde_Prefs_Storage /** * Constructor. * + * @param string $user The username. * @param array $params Configuration parameters: *
      * 'directory' - (string) [REQUIRED] Preference storage directory.
@@ -41,7 +42,7 @@ class Horde_Prefs_Storage_File extends Horde_Prefs_Storage
      *
      * @throws InvalidArgumentException
      */
-    public function __construct(array $params = array())
+    public function __construct($user, array $params = array())
     {
         // Sanity check for directory
         if (empty($params['directory']) || !is_dir($params['directory'])) {
@@ -51,7 +52,7 @@ class Horde_Prefs_Storage_File extends Horde_Prefs_Storage
             throw new InvalidArgumentException(sprintf('Directory %s is not writeable.', $params['directory']));
         }
 
-        parent::__construct($scope, $opts, $params);
+        parent::__construct($user, $params);
 
         $this->_fullpath = $this->params['directory'] . '/' . basename($this->_params['user']) . '.prefs';
     }
diff --git a/framework/Prefs/lib/Horde/Prefs/Storage/Ldap.php b/framework/Prefs/lib/Horde/Prefs/Storage/Ldap.php
index 84598c94b..9dc0735ff 100644
--- a/framework/Prefs/lib/Horde/Prefs/Storage/Ldap.php
+++ b/framework/Prefs/lib/Horde/Prefs/Storage/Ldap.php
@@ -39,6 +39,7 @@ class Horde_Prefs_Storage_Ldap extends Horde_Prefs_Storage
     /**
      * Constructor.
      *
+     * @param string $user   The username.
      * @param array $params  Configuration options:
      * 
      * basedn - (string) [REQUIRED] The base DN for the LDAP server.
@@ -61,7 +62,7 @@ class Horde_Prefs_Storage_Ldap extends Horde_Prefs_Storage
      *           DEFAULT: NONE (system default will be used)
      * 
*/ - public function __construct(array $params = array()) + public function __construct($user, array $params = array()) { /* If a valid server port has not been specified, set the default. */ if (!isset($params['port']) || !is_integer($params['port'])) { diff --git a/framework/Prefs/lib/Horde/Prefs/Storage/Session.php b/framework/Prefs/lib/Horde/Prefs/Storage/Session.php index 7a55524a9..265320e73 100644 --- a/framework/Prefs/lib/Horde/Prefs/Storage/Session.php +++ b/framework/Prefs/lib/Horde/Prefs/Storage/Session.php @@ -23,11 +23,11 @@ class Horde_Prefs_Storage_Session extends Horde_Prefs_Storage /** */ - public function __construct($user) + public function __construct($user, array $params = array()) { - parent::__construct($user); + parent::__construct($user, $params); - $this->_key = 'horde_prefs_' . $this->_user; + $this->_key = 'horde_prefs_' . $this->_params['user']; } /** diff --git a/framework/Prefs/lib/Horde/Prefs/Storage/Sql.php b/framework/Prefs/lib/Horde/Prefs/Storage/Sql.php index f3d0b0345..fdca35dd3 100644 --- a/framework/Prefs/lib/Horde/Prefs/Storage/Sql.php +++ b/framework/Prefs/lib/Horde/Prefs/Storage/Sql.php @@ -25,6 +25,7 @@ class Horde_Prefs_Storage_Sql extends Horde_Prefs_Storage /** * Constructor. * + * @param string $user The username. * @param array $params Configuration parameters. *
      * 'db' - (Horde_Db_Adapter) [REQUIRED] The DB instance.
@@ -34,7 +35,7 @@ class Horde_Prefs_Storage_Sql extends Horde_Prefs_Storage
      *
      * @throws InvalidArgumentException
      */
-    public function __construct(array $params = array())
+    public function __construct($user, array $params = array())
     {
         if (!isset($params['db'])) {
             throw new InvalidArgumentException('Missing db parameter.');
@@ -46,7 +47,7 @@ class Horde_Prefs_Storage_Sql extends Horde_Prefs_Storage
             'table' => 'horde_prefs'
         ), $params);
 
-        parent::__construct($params);
+        parent::__construct($user, $params);
     }
 
     /**