Move Horde-specific code out of horde/Prefs and into horde/Core
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 19 Oct 2010 23:00:18 +0000 (17:00 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 20 Oct 2010 16:07:15 +0000 (10:07 -0600)
Includes the prefs.php configuration loading and hooks code.

framework/Core/lib/Horde/Core/Factory/Prefs.php
framework/Core/lib/Horde/Core/Prefs.php [new file with mode: 0644]
framework/Core/package.xml
framework/Prefs/lib/Horde/Prefs.php

index 0bf3a4e..d0ba2cb 100644 (file)
@@ -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 (file)
index 0000000..92c1125
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+/**
+ * The Horde_Core_Prefs class extends the base Horde_Prefs class by adding
+ * support for the prefs.php configuration file and Horde hooks.
+ *
+ * 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 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);
+                }
+            }
+        }
+    }
+
+}
index 59554f7..61a5a22 100644 (file)
@@ -222,6 +222,7 @@ Application Framework.</description>
        <file name="Widget.php" role="php" />
       </dir> <!-- /lib/Horde/Core/Ui -->
       <file name="Browser.php" role="php" />
+      <file name="Prefs.php" role="php" />
       <file name="Sidebar.php" role="php" />
      </dir> <!-- /lib/Horde/Core -->
      <dir name="Exception">
@@ -409,6 +410,7 @@ Application Framework.</description>
    <install as="Horde/Themes.php" name="lib/Horde/Themes.php" />
    <install as="Horde/Config/Form.php" name="lib/Horde/Config/Form.php" />
    <install as="Horde/Core/Browser.php" name="lib/Horde/Core/Browser.php" />
+   <install as="Horde/Core/Prefs.php" name="lib/Horde/Core/Prefs.php" />
    <install as="Horde/Core/Sidebar.php" name="lib/Horde/Core/Sidebar.php" />
    <install as="Horde/Core/Ajax/Application.php" name="lib/Horde/Core/Ajax/Application.php" />
    <install as="Horde/Core/Ajax/Imple.php" name="lib/Horde/Core/Ajax/Imple.php" />
index ef450d8..ac3af03 100644 (file)
@@ -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)