Do on-demand determination of paths in Horde_Themes_Element
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 20 Aug 2010 16:55:45 +0000 (10:55 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 20 Aug 2010 16:55:45 +0000 (10:55 -0600)
Thus, creating Horde_Themes_Image/Horde_Themes_Sound should be very
lightweight - the path determination only happens when the element is
used.

framework/Core/lib/Horde/Themes.php
framework/Core/lib/Horde/Themes/Element.php
framework/Core/lib/Horde/Themes/Image.php
framework/Core/lib/Horde/Themes/Sound.php

index 8a87d76..bd08241 100644 (file)
@@ -288,7 +288,7 @@ class Horde_Themes
      */
     static public function img($name = null, $options = array())
     {
-        return self::_getObject('graphics', $name, $options);
+        return new Horde_Themes_Image($name, $options);
     }
 
     /**
@@ -307,81 +307,12 @@ class Horde_Themes
      * 'theme' - (string) Use this theme instead of the Horde default.
      * </pre>
      *
-     * @return Horde_Themes_Image  An object which contains the URI
+     * @return Horde_Themes_Sound  An object which contains the URI
      *                             and filesystem location of the sound.
      */
     static public function sound($name = null, $options = array())
     {
-        return self::_getObject('sounds', $name, $options);
-    }
-
-    /**
-     * Return the path to a themes element, using the default element if the
-     * image does not exist in the current theme.
-     *
-     * @param string $type    The element type ('graphics', 'sound').
-     * @param string $name    The element name. If null, will return the
-     *                        element directory.
-     * @param mixed $options  Additional options. If a string, is taken to be
-     *                        the 'app' parameter. If an array, the following
-     *                        options are available:
-     * <pre>
-     * 'app' - (string) Use this application instead of the current app.
-     * 'nohorde' - (boolean) If true, do not fallback to horde for element.
-     * 'notheme' - (boolean) If true, do not use themed data.
-     * 'theme' - (string) Use this theme instead of the Horde default.
-     * </pre>
-     *
-     * @return Horde_Themes_Element  An object which contains the URI and
-     *                               filesystem location of the element.
-     */
-    static protected function _getObject($type, $name, $options)
-    {
-        if (is_string($options)) {
-            $app = $options;
-            $options = array();
-        } else {
-            $app = empty($options['app'])
-                ? $GLOBALS['registry']->getApp()
-                : $options['app'];
-        }
-        if ($GLOBALS['registry']->get('status', $app) == 'heading') {
-            $app = 'horde';
-        }
-
-        $app_list = array($app);
-        if ($app != 'horde' && empty($options['nohorde'])) {
-            $app_list[] = 'horde';
-        }
-        $path = '/' . $type . (is_null($name) ? '' : '/' . $name);
-
-        $classname = ($type == 'graphics')
-            ? 'Horde_Themes_Image'
-            : 'Horde_Themes_Sound';
-
-        /* Check themes first. */
-        if (empty($options['notheme']) &&
-            isset($GLOBALS['prefs']) &&
-            (($theme = $GLOBALS['prefs']->getValue('theme')) ||
-             (!empty($options['theme']) && ($theme = $options['theme'])))) {
-            $tpath = '/' . $theme . $path;
-            foreach ($app_list as $app) {
-                $filepath = $GLOBALS['registry']->get('themesfs', $app) . $tpath;
-                if (is_null($name) || file_exists($filepath)) {
-                    return new $classname($GLOBALS['registry']->get('themesuri', $app) . $tpath, $filepath);
-                }
-            }
-        }
-
-        /* Fall back to app/horde defaults. */
-        foreach ($app_list as $app) {
-            $filepath = $GLOBALS['registry']->get('themesfs', $app) . $path;
-            if (file_exists($filepath)) {
-                return new $classname($GLOBALS['registry']->get('themesuri', $app) . $path, $filepath);
-            }
-        }
-
-        return '';
+        return new Horde_Themes_Sound($name, $options);
     }
 
     /**
index 046c791..084c114 100644 (file)
 class Horde_Themes_Element
 {
     /**
-     * The URI.
+     * Current application name.
      *
      * @var string
      */
-    public $uri = '';
+    public $app;
 
     /**
-     * The filesystem path.
+     * The default directory name for this element type.
      *
      * @var string
      */
-    public $fs = '';
+    protected $_dirname = '';
+
+    /**
+     * Element name.
+     *
+     * @var string
+     */
+    protected $_name;
+
+    /**
+     * Options.
+     *
+     * @var array
+     */
+    protected $_opts;
+
+    /**
+     * Cached URI/filesystem path values.
+     *
+     * @var array
+     */
+    protected $_cache = array();
 
     /**
      * Constructor.
      *
-     * @param string $uri  The image URI.
-     * @param string $fs   The image filesystem path.
+     * @param string $name    The element name. If null, will return the
+     *                        element directory.
+     * @param mixed $options  Additional options. If a string, is taken to be
+     *                        the 'app' parameter. If an array, the following
+     *                        options are available:
+     * <pre>
+     * 'app' - (string) Use this application instead of the current app.
+     * 'data' - (array) Contains 2 elements: 'fs' - filesystem path,
+                        'uri' - the element URI. If set, use as the data
+                        values instead of auto determining.
+     * 'nohorde' - (boolean) If true, do not fallback to horde for element.
+     * 'notheme' - (boolean) If true, do not use themed data.
+     * 'theme' - (string) Use this theme instead of the Horde default.
+     * 'uri' - (string) Use this as the URI value.
+     * </pre>
      */
-    public function __construct($uri, $fs)
+    public function __construct($name = '', $options = array())
     {
-        $this->uri = $uri;
-        $this->fs = $fs;
+        $this->_name = $name;
+
+        if (is_string($options)) {
+            $this->app = $options;
+            $this->_opts = array();
+        } else {
+            $this->app = empty($options['app'])
+                ? $GLOBALS['registry']->getApp()
+                : $options['app'];
+            $this->_opts = $options;
+        }
+        if ($GLOBALS['registry']->get('status', $this->app) == 'heading') {
+            $this->app = 'horde';
+        }
+
+        if (isset($this->_opts['data'])) {
+            $this->_cache = $this->_opts['data'];
+            unset($this->_opts['data']);
+        }
     }
 
     /**
@@ -51,6 +102,69 @@ class Horde_Themes_Element
     }
 
     /**
+     * Retrieve URI/filesystem path values.
+     *
+     * @param string $name  Either 'fs' or 'uri'.
+     *
+     * @return string  The requested value.
+     */
+    public function __get($name)
+    {
+        global $registry;
+
+        if (empty($this->_cache)) {
+            $app_list = array($this->app);
+            if (($this->app != 'horde') && empty($this->_opts['nohorde'])) {
+                $app_list[] = 'horde';
+            }
+            $path = '/' . $this->_dirname . (is_null($this->_name) ? '' : '/' . $this->_name);
+
+            /* Check themes first. */
+            if (empty($this->_opts['notheme']) &&
+                isset($GLOBALS['prefs']) &&
+                (($theme = $GLOBALS['prefs']->getValue('theme')) ||
+                 (!empty($this->_opts['theme']) && ($theme = $this->_opts['theme'])))) {
+                $tpath = '/' . $theme . $path;
+                foreach ($app_list as $app) {
+                    $filepath = $registry->get('themesfs', $app) . $tpath;
+                    if (is_null($this->_name) || file_exists($filepath)) {
+                        $this->_cache = array(
+                            'fs' => $filepath,
+                            'uri' => $registry->get('themesuri', $app) . $tpath
+                        );
+                        break;
+                    }
+                }
+            }
+
+            /* Fall back to app/horde defaults. */
+            if (empty($this->_cache)) {
+                foreach ($app_list as $app) {
+                    $filepath = $registry->get('themesfs', $app) . $path;
+                    if (file_exists($filepath)) {
+                        $this->_cache = array(
+                            'fs' => $filepath,
+                            'uri' => $registry->get('themesuri', $app) . $path
+                        );
+                        break;
+                    }
+                }
+            }
+        }
+
+        switch ($name) {
+        case 'fs':
+        case 'uri':
+            return isset($this->_cache[$name])
+                ? $this->_cache[$name]
+                : '';
+
+        default:
+            return null;
+        }
+    }
+
+    /**
      * Convert a URI into a Horde_Themes_Image object.
      *
      * @param string $uri  The URI to convert.
@@ -59,7 +173,14 @@ class Horde_Themes_Element
      */
     static public function fromUri($uri)
     {
-        return new self($uri, realpath($GLOBALS['registry']->get('fileroot', 'horde')) . preg_replace('/^' . preg_quote($GLOBALS['registry']->get('webroot', 'horde'), '/') . '/', '', $uri));
+        global $registry;
+
+        return new self('', array(
+            'data' => array(
+                'fs' => realpath($registry->get('fileroot', 'horde')) . preg_replace('/^' . preg_quote($registry->get('webroot', 'horde'), '/') . '/', '', $uri),
+                'uri' => $uri
+            )
+        ));
     }
 
 }
index 283ac12..f7443f7 100644 (file)
  * @category Horde
  * @package  Core
  */
-class Horde_Themes_Image extends Horde_Themes_Element {}
+class Horde_Themes_Image extends Horde_Themes_Element
+{
+    /**
+     * The default directory name for this element type.
+     *
+     * @var string
+     */
+    protected $_dirname = 'graphics';
+
+}
index 4b1e9a2..40ac593 100644 (file)
  * @category Horde
  * @package  Core
  */
-class Horde_Themes_Sound extends Horde_Themes_Element {}
+class Horde_Themes_Sound extends Horde_Themes_Element
+{
+    /**
+     * The default directory name for this element type.
+     *
+     * @var string
+     */
+    protected $_dirname = 'sound';
+
+}