From 64178ddd573c8814e8c4564c04e085638885a0c6 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Wed, 22 Sep 2010 17:33:42 +0200 Subject: [PATCH] Re-add hookExists(). Speeds up loading my portal by factor 4. --- framework/Core/lib/Horde.php | 56 +++++++++++++++++++++++++++++++++----------- turba/lib/Object.php | 17 +++++++------- 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/framework/Core/lib/Horde.php b/framework/Core/lib/Horde.php index d60053730..38923fc33 100644 --- a/framework/Core/lib/Horde.php +++ b/framework/Core/lib/Horde.php @@ -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. * diff --git a/turba/lib/Object.php b/turba/lib/Object.php index a49f55d01..cb9463cde 100644 --- a/turba/lib/Object.php +++ b/turba/lib/Object.php @@ -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; } /** -- 2.11.0