Re-add hookExists(). Speeds up loading my portal by factor 4.
authorJan Schneider <jan@horde.org>
Wed, 22 Sep 2010 15:33:42 +0000 (17:33 +0200)
committerJan Schneider <jan@horde.org>
Wed, 22 Sep 2010 15:33:42 +0000 (17:33 +0200)
framework/Core/lib/Horde.php
turba/lib/Object.php

index d600537..38923fc 100644 (file)
@@ -1681,23 +1681,12 @@ HTML;
      */
     static public function callHook($hook, $args = array(), $app = 'horde')
     {
-        $error = false;
-        $hook_class = $app . '_Hooks';
-
-        if (empty(self::$_hooksLoaded[$app]) &&
-            !class_exists($hook_class, false)) {
-            try {
-                self::loadConfiguration('hooks.php', null, $app);
-            } catch (Horde_Exception $e) {}
-            self::$_hooksLoaded[$app] = true;
-        }
-
-        if (!class_exists($hook_class, false) ||
-            !($hook_ob = new $hook_class) ||
-            !method_exists($hook_ob, $hook)) {
+        if (!self::hookExists($hook, $app)) {
             throw new Horde_Exception_HookNotSet();
         }
 
+        $hook_class = $app . '_Hooks';
+        $hook_ob = new $hook_class;
         try {
             self::logMessage(sprintf('Hook %s in application %s called.', $hook, $app), 'DEBUG');
             return call_user_func_array(array($hook_ob, $hook), $args);
@@ -1708,6 +1697,45 @@ HTML;
     }
 
     /**
+     * Returns whether a hook exists.
+     *
+     * Use this if you have to call a hook many times and expect the hook to
+     * not exist.
+     *
+     * @param string $hook  The function to call.
+     * @param string $app   The hook application.
+     *
+     * @return boolean  True if the hook exists.
+     */
+    static public function hookExists($hook, $app = 'horde')
+    {
+        $hook_class = $app . '_Hooks';
+
+        if (!isset(self::$_hooksLoaded[$app])) {
+            self::$_hooksLoaded[$app] = false;
+            if (!class_exists($hook_class, false)) {
+                try {
+                    self::loadConfiguration('hooks.php', null, $app);
+                    self::$_hooksLoaded[$app] = array();
+                } catch (Horde_Exception $e) {}
+            }
+        }
+
+        if (self::$_hooksLoaded[$app] === false) {
+            return false;
+        }
+
+        if (!isset(self::$_hooksLoaded[$app][$hook])) {
+            self::$_hooksLoaded[$app][$hook] =
+                class_exists($hook_class, false) &&
+                ($hook_ob = new $hook_class) &&
+                method_exists($hook_ob, $hook);
+        }
+
+        return self::$_hooksLoaded[$app][$hook];
+    }
+
+    /**
      * Utility function to send redirect headers to browser, handling any
      * browser quirks.
      *
index a49f55d..cb9463c 100644 (file)
@@ -90,10 +90,10 @@ class Turba_Object {
      */
     public function getValue($attribute)
     {
-        if (isset($this->attributes[$attribute])) {
+        if (isset($this->attributes[$attribute]) &&
+            Horde::hookExists('decode_attribute', 'turba')) {
             try {
                 return Horde::callHook('decode_attribute', array($attribute, $this->attributes[$attribute]), 'turba');
-            } catch (Horde_Exception_HookNotSet $e) {
             } catch (Turba_Exception $e) {}
         }
 
@@ -129,19 +129,20 @@ class Turba_Object {
      */
     function setValue($attribute, $value)
     {
-        try {
-            $value = Horde::callHook('encode_attribute', array($attribute, $value, isset($this->attributes[$attribute]) ? $this->attributes[$attribute] : null, $this), 'turba');
-        } catch (Horde_Exception_HookNotSet $e) {
-        } catch (Turba_Exception $e) {}
+        if (Horde::hookExists('encode_attribute', 'turba')) {
+            try {
+                $value = Horde::callHook('encode_attribute', array($attribute, $value, isset($this->attributes[$attribute]) ? $this->attributes[$attribute] : null, $this), 'turba');
+            } catch (Turba_Exception $e) {}
+        }
 
         if (isset($this->driver->map[$attribute]) &&
             is_array($this->driver->map[$attribute]) &&
             !isset($this->driver->map[$attribute]['attribute'])) {
-            return false;
+            return;
         }
 
         $this->attributes[$attribute] = $value;
-        return true;
+        return;
     }
 
     /**