!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' => '',
), $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']) &&
$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);
}
}
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);
}
}
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:
* <pre>
* REQUIRED:
* user - (string) The name of the user who owns this set of preferences.
* DEFAULT: NONE
* </pre>
- * @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.');
$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);
}
/**
$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 {
$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);
}
}
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) {}
+ }
}
}