Bug #9404: Fix sound listing
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 24 Nov 2010 15:54:22 +0000 (08:54 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 24 Nov 2010 15:58:38 +0000 (08:58 -0700)
This changes the serialized cache format so you will need to run:

horde/bin/themes --expirecache

(Not going to bother with internal cache invalidation until themes code
stabilizes).

framework/Core/lib/Horde/Themes.php
framework/Core/lib/Horde/Themes/Cache.php

index 49f4ffd..7d56859 100644 (file)
@@ -97,42 +97,31 @@ class Horde_Themes
     }
 
     /**
-     * Returns a list of available sounds for a theme.
+     * Returns a list of available sounds.
      *
      * @param string $app  The app to search in.
+     * @param string $theme  The app to search in.
      *
      * @return array  An array of Horde_Themes_Sound objects. Keys are the
      *                base filenames.
      */
-    static public function soundList($app = null)
+    static public function soundList($app = null, $theme = null)
     {
         if (is_null($app)) {
             $app = $GLOBALS['registry']->getApp();
         }
 
-        /* Do search in reverse order - app + theme sounds have the highest
-         * priority and will overwrite previous sound definitions. */
-        $locations = array(
-            self::sound(null, array('app' => 'horde', 'theme' => null)),
-            // Placeholder for app
-            null,
-            self::sound(null, 'horde')
-        );
-
-        if ($app != 'horde') {
-            $locations[1] = self::sound(null, array('app' => $app, 'theme' => null));
-            $locations[3] = self::sound(null, $app);
+        if (is_null($theme)) {
+            $theme = $GLOBALS['prefs']->getValue('theme');
         }
 
+        $cache = $GLOBALS['injector']->getInstance('Horde_Core_Factory_ThemesCache')->create($app, $theme);
+
         $sounds = array();
-        foreach ($locations as $val) {
-            if ($val) {
-                foreach (glob($val->fs . '/*.wav') as $file) {
-                    $file = basename($file);
-                    if (!isset($sounds[$file])) {
-                        $sounds[$file] = self::sound($file);
-                    }
-                }
+        foreach ($cache->build() as $val) {
+            if ((strpos($val, 'sounds/') === 0) &&
+                (substr(strrchr($val, '.'), 1) == 'wav')) {
+                $sounds[basename($val)] = self::sound(substr($val, 7));
             }
         }
 
index fc12886..b343dc1 100644 (file)
@@ -43,6 +43,13 @@ class Horde_Themes_Cache implements Serializable
     protected $_cacheid;
 
     /**
+     * Is this a complete representation of the theme?
+     *
+     * @var boolean
+     */
+    protected $_complete = false;
+
+    /**
      * Theme data.
      *
      * @var array
@@ -71,20 +78,25 @@ class Horde_Themes_Cache implements Serializable
     /**
      * Build the entire theme data structure.
      *
+     * @return array  The list of theme files.
      * @throws UnexpectedValueException
      */
     public function build()
     {
-        $this->_data = array();
+        if (!$this->_complete) {
+            $this->_data = array();
 
-        $this->_build('horde', 'default', self::HORDE_DEFAULT);
-        $this->_build('horde', $this->_theme, self::HORDE_THEME);
-        if ($this->_app != 'horde') {
-            $this->_build($this->_app, 'default', self::APP_DEFAULT);
-            $this->_build($this->_app, $this->_theme, self::APP_THEME);
+            $this->_build('horde', 'default', self::HORDE_DEFAULT);
+            $this->_build('horde', $this->_theme, self::HORDE_THEME);
+            if ($this->_app != 'horde') {
+                $this->_build($this->_app, 'default', self::APP_DEFAULT);
+                $this->_build($this->_app, $this->_theme, self::APP_THEME);
+            }
+
+            $this->changed = $this->_complete = true;
         }
 
-        $this->changed = true;
+        return array_keys($this->_data);
     }
 
     /**
@@ -246,10 +258,11 @@ class Horde_Themes_Cache implements Serializable
     public function serialize()
     {
         return serialize(array(
-            $this->getCacheId(),
-            $this->_app,
-            $this->_data,
-            $this->_theme
+            'a' => $this->_app,
+            'c' => $this->_complete,
+            'd' => $this->_data,
+            'id' => $this->getCacheId(),
+            't' => $this->_theme
         ));
     }
 
@@ -257,16 +270,16 @@ class Horde_Themes_Cache implements Serializable
      */
     public function unserialize($data)
     {
-        list(
-            $expire_id,
-            $this->_app,
-            $this->_data,
-            $this->_theme
-        ) = unserialize($data);
-
-        if ($expire_id && ($expire_id != $this->getCacheId())) {
+        $out = @unserialize($data);
+
+        if (isset($out['id']) && ($out['id'] != $this->getCacheId())) {
             throw new Exception('Cache invalidated');
         }
+
+        $this->_app = $out['a'];
+        $this->_complete = $out['c'];
+        $this->_data = $out['d'];
+        $this->_theme = $out['t'];
     }
 
 }