From 7b6150f3a2acdedea798fbd6b17f26ca9e0835c2 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 19 Oct 2010 17:00:18 -0600 Subject: [PATCH] Move Horde-specific code out of horde/Prefs and into horde/Core Includes the prefs.php configuration loading and hooks code. --- framework/Core/lib/Horde/Core/Factory/Prefs.php | 4 +- framework/Core/lib/Horde/Core/Prefs.php | 112 +++++++++++++++++++ framework/Core/package.xml | 2 + framework/Prefs/lib/Horde/Prefs.php | 139 ++++++------------------ 4 files changed, 151 insertions(+), 106 deletions(-) create mode 100644 framework/Core/lib/Horde/Core/Prefs.php diff --git a/framework/Core/lib/Horde/Core/Factory/Prefs.php b/framework/Core/lib/Horde/Core/Factory/Prefs.php index 0bf3a4e54..d0ba2cb7e 100644 --- a/framework/Core/lib/Horde/Core/Factory/Prefs.php +++ b/framework/Core/lib/Horde/Core/Factory/Prefs.php @@ -127,7 +127,7 @@ class Horde_Core_Factory_Prefs } try { - $this->_instances[$sig] = new Horde_Prefs($driver, $scope, $opts, $params); + $this->_instances[$sig] = new Horde_Core_Prefs($driver, $scope, $opts, $params); } catch (Horde_Prefs_Exception $e) { if (!$GLOBALS['session']['horde:no_prefs']) { $GLOBALS['session']['horde:no_prefs'] = true; @@ -136,7 +136,7 @@ class Horde_Core_Factory_Prefs } } unset($opts['cache']); - $this->_instances[$sig] = new Horde_Prefs('Horde_Core_Prefs_Storage_Session', $scope, $opts); + $this->_instances[$sig] = new Horde_Core_Prefs('Horde_Core_Prefs_Storage_Session', $scope, $opts); } } diff --git a/framework/Core/lib/Horde/Core/Prefs.php b/framework/Core/lib/Horde/Core/Prefs.php new file mode 100644 index 000000000..92c1125c8 --- /dev/null +++ b/framework/Core/lib/Horde/Core/Prefs.php @@ -0,0 +1,112 @@ + + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @package Core + */ +class Horde_Core_Prefs extends Horde_Prefs +{ + /** + * Hash holding preferences with hook functions defined. + * + * @var array + */ + protected $_hooks = array(); + + /** + */ + protected function _setValue($pref, $val, $convert = true) + { + if (!parent::_setValue($pref, $val, $convert)) { + return false; + } + + /* If this preference has a change hook, call it now. */ + try { + Horde::callHook('prefs_change', array($pref), $this->_getPrefScope($pref)); + } catch (Horde_Exception_HookNotSet $e) {} + + return true; + } + + /** + * Populates the $_scopes hash with the default values as loaded from + * an application's prefs.php file. + */ + protected function _loadScopePre($scope) + { + /* Read the configuration file. The $_prefs array holds the default + * values. */ + try { + $result = Horde::loadConfiguration('prefs.php', array('_prefs'), $scope); + if (empty($result) || !isset($result['_prefs'])) { + return; + } + } catch (Horde_Exception $e) { + return; + } + + foreach ($result['_prefs'] as $name => $pref) { + if (!isset($pref['value'])) { + continue; + } + + $name = str_replace('.', '_', $name); + + $mask = self::IS_DEFAULT; + if (!empty($pref['locked'])) { + $mask |= self::LOCKED; + } + + $this->_scopes[$scope][$name] = array( + 'm' => $mask, + 'v' => $pref['value'] + ); + + if (!empty($pref['hook'])) { + $this->_hooks[$scope][] = $name; + } + } + } + + /** + * After preferences have been loaded, set any locked or empty + * preferences that have hooks to the result of the hook. + */ + protected function _loadScopePost($scope) + { + if (empty($this->_hooks[$scope])) { + return; + } + + foreach ($this->_hooks[$scope] as $name) { + if ($this->isLocked($name) || + $this->isDefault($name) || + empty($this->_scopes[$scope][$name]['v'])) { + try { + $val = Horde::callHook('prefs_init', array($name, $this->getUser()), $scope); + } catch (Horde_Exception_HookNotSet $e) { + continue; + } + + $this->_scopes[$scope][$name]['v'] = $this->isDefault($name) + ? $val + : $this->convertToDriver($val); + + if (!$this->_isLocked($name)) { + $this->setDirty($pref, true); + } + } + } + } + +} diff --git a/framework/Core/package.xml b/framework/Core/package.xml index 59554f775..61a5a22ab 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -222,6 +222,7 @@ Application Framework. + @@ -409,6 +410,7 @@ Application Framework. + diff --git a/framework/Prefs/lib/Horde/Prefs.php b/framework/Prefs/lib/Horde/Prefs.php index ef450d8e9..ac3af0374 100644 --- a/framework/Prefs/lib/Horde/Prefs.php +++ b/framework/Prefs/lib/Horde/Prefs.php @@ -21,7 +21,7 @@ class Horde_Prefs implements ArrayAccess /** Preference value is the application default. * DEFAULT is a reserved PHP constant. */ - const PREFS_DEFAULT = 2; + const IS_DEFAULT = 2; /** * Caching object. @@ -45,13 +45,6 @@ class Horde_Prefs implements ArrayAccess protected $_dirty = array(); /** - * Hash holding preferences with hook functions defined. - * - * @var array - */ - protected $_hooks = array(); - - /** * General library options. * * @var array @@ -269,22 +262,17 @@ class Horde_Prefs implements ArrayAccess return false; } - $result = $this->_setValue($pref, $val, $convert); - - if ($result && $this->isDirty($pref)) { - $this->_cache->store(array( - $scope => array( - $pref => $this->_scopes[$scope][$pref] - ) - )); - - /* If this preference has a change hook, call it now. */ - try { - Horde::callHook('prefs_change', array($pref), $scope); - } catch (Horde_Exception_HookNotSet $e) {} + if (!$this->_setValue($pref, $val, $convert)) { + return false; } - return $result; + $this->_cache->store(array( + $scope => array( + $pref => $this->_scopes[$scope][$pref] + ) + )); + + return true; } /** @@ -451,7 +439,7 @@ class Horde_Prefs implements ArrayAccess */ public function setDefault($pref, $bool) { - $this->_setMask($pref, $bool, self::PREFS_DEFAULT); + $this->_setMask($pref, $bool, self::IS_DEFAULT); } /** @@ -480,7 +468,7 @@ class Horde_Prefs implements ArrayAccess */ public function isDefault($pref) { - return $this->_getMask($pref, self::PREFS_DEFAULT); + return $this->_getMask($pref, self::IS_DEFAULT); } /** @@ -557,6 +545,8 @@ class Horde_Prefs implements ArrayAccess /** * Load a specific preference scope. + * + * @param string $scope The scope to load. */ protected function _loadScope($scope) { @@ -568,8 +558,6 @@ class Horde_Prefs implements ArrayAccess // Basic initialization so _something_ is always set. $this->_scopes[$scope] = array(); - $this->_setDefaults($scope); - // Now check the prefs cache for existing values. try { if (($cached = $this->_cache->get($scope)) !== false) { @@ -578,6 +566,8 @@ class Horde_Prefs implements ArrayAccess } } catch (Horde_Prefs_Exception $e) {} + $this->_loadScopePre($scope); + if (($prefs = $this->_storage->get($scope)) !== false) { foreach ($prefs as $name => $val) { if ($this->isDefault($name)) { @@ -593,13 +583,31 @@ class Horde_Prefs implements ArrayAccess } } - $this->_callHooks($scope); + $this->_loadScopePost($scope); /* Update the cache. */ $this->_cache->store(array($scope => $this->_scopes[$scope])); } /** + * Actions to perform before a scope is loaded from storage. + * + * @param string $scope The scope to load. + */ + protected function _loadScopePre($scope) + { + } + + /** + * Actions to perform after a scope is loaded from storage. + * + * @param string $scope The loaded scope. + */ + protected function _loadScopePost($scope) + { + } + + /** * This function will be run at the end of every request as a shutdown * function (registered by the constructor). All prefs with the * dirty bit set will be saved to the storage backend. @@ -676,83 +684,6 @@ class Horde_Prefs implements ArrayAccess : Horde_String::convertCharset($value, 'UTF-8', $this->getCharset()); } - /** - * Populates the $_scopes hash with new entries and externally - * defined default values. - * - * @param string $scope The scope to load defaults for. - */ - protected function _setDefaults($scope) - { - /* Read the configuration file. The $_prefs array is assumed to hold - * the default values. */ - try { - $result = Horde::loadConfiguration('prefs.php', array('_prefs'), $scope); - if (empty($result) || !isset($result['_prefs'])) { - return; - } - } catch (Horde_Exception $e) { - return; - } - - foreach ($result['_prefs'] as $name => $pref) { - if (!isset($pref['value'])) { - continue; - } - - $name = str_replace('.', '_', $name); - - $mask = self::PREFS_DEFAULT; - if (!empty($pref['locked'])) { - $mask |= self::LOCKED; - } - - $this->_scopes[$scope][$name] = array( - 'm' => $mask, - 'v' => $pref['value'] - ); - - if (!empty($pref['hook'])) { - $this->_hooks[$scope][$name] = $scope; - } - } - } - - /** - * After preferences have been loaded, set any locked or empty - * preferences that have hooks to the result of the hook. - * - * @param string $scope The preferences scope to call hooks for. - */ - protected function _callHooks($scope) - { - if (empty($this->_hooks[$scope])) { - return; - } - - foreach ($this->_hooks[$scope] as $name => $pref_scope) { - if ($this->_scopes[$pref_scope][$name]['m'] & self::LOCKED || - empty($this->_scopes[$pref_scope][$name]['v']) || - $this->_scopes[$pref_scope][$name]['m'] & self::PREFS_DEFAULT) { - - try { - $val = Horde::callHook('prefs_init', array($name, $this->getUser()), $scope); - } catch (Horde_Exception_HookNotSet $e) { - continue; - } - - if ($this->_scopes[$pref_scope][$name]['m'] & self::PREFS_DEFAULT) { - $this->_scopes[$pref_scope][$name]['v'] = $val; - } else { - $this->_scopes[$pref_scope][$name]['v'] = $this->convertToDriver($val); - } - if (!($this->_scopes[$pref_scope][$name]['m'] & self::LOCKED)) { - $this->_scopes[$pref_scope][$name]['m'] |= self::DIRTY; - } - } - } - } - /* ArrayAccess methods. */ public function offsetExists($offset) -- 2.11.0