New hook handling code.
authorMichael M Slusarz <slusarz@curecanti.org>
Sun, 26 Jul 2009 23:48:50 +0000 (17:48 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 27 Jul 2009 04:57:03 +0000 (22:57 -0600)
framework/Core/lib/Horde.php
framework/Core/lib/Horde/Exception/HookNotSet.php [new file with mode: 0644]
framework/Core/package.xml

index 1468322..4ce257c 100644 (file)
@@ -1774,40 +1774,50 @@ HTML;
     }
 
     /**
-     * Provides a standardised function to call a Horde hook, checking whether
-     * a hook config file exists and whether the function itself exists. If
-     * these two conditions are not satisfied it will return the specified
-     * value (by default a PEAR error).
+     * Call a Horde hook, handling all of the necessary lookups and parsing
+     * of the hook code.
      *
      * @param string $hook  The function to call.
      * @param array  $args  An array of any arguments to pass to the hook
      *                      function.
-     * @param string $app   If specified look for hooks in the config directory
-     *                      of this app.
+     * @param string $app   The hook application.
      *
      * @return mixed  The results of the hook.
+     * @throws Horde_Exception  Thrown on error from hook code.
+     * @throws Horde_Exception_HookNotSet  Thrown if hook is not active.
      */
     static public function callHook($hook, $args = array(), $app = 'horde')
     {
-        if (!isset(self::$_hooksLoaded[$app])) {
+        $error = false;
+        $hook_class = $app . '_Hooks';
+
+        if (!class_exists($hook_class)) {
             try {
                 self::loadConfiguration('hooks.php', null, $app);
-                self::$_hooksLoaded[$app] = true;
             } catch (Horde_Exception $e) {
-                self::logMessage($e, __FILE__, __LINE__, PEAR_LOG_DEBUG);
-                self::$_hooksLoaded[$app] = false;
+                $error = true;
             }
         }
 
-        if (!function_exists($hook)) {
-            self::logMessage(sprintf('Hook %s in application %s not called.', $hook, $app), __FILE__, __LINE__, PEAR_LOG_DEBUG);
-            return null;
+        if (class_exists($hook_class)) {
+            $hook_ob = new $hook_class;
+            $error = !method_exists($hook_ob, $hook);
+        } else {
+            $error = true;
+        }
+
+        if ($error) {
+            $error = sprintf('Hook %s in application %s not called.', $hook, $app);
+            self::logMessage($error, __FILE__, __LINE__, PEAR_LOG_DEBUG);
+            throw new Horde_Exception_HookNotSet($error);
         }
 
         try {
-            return call_user_func_array($hook, $args);
+            self::logMessage(sprintf('Hook %s in application %s called.', $hook, $app), __FILE__, __LINE__, PEAR_LOG_DEBUG);
+            return call_user_func_array(array($hook_ob, $hook), $args);
         } catch (Horde_Exception $e) {
             self::logMessage($e, __FILE__, __LINE__, PEAR_LOG_ERR);
+            throw $e;
         }
     }
 
diff --git a/framework/Core/lib/Horde/Exception/HookNotSet.php b/framework/Core/lib/Horde/Exception/HookNotSet.php
new file mode 100644 (file)
index 0000000..ff6b38d
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+/**
+ * Horde_Exception_HookNotSet is thrown when a called Horde hook does
+ * not exist.
+ *
+ * @category Horde
+ * @package  Core
+ */
+class Horde_Exception_HookNotSet extends Exception
+{
+}
index c86cad5..f7d6ec5 100644 (file)
@@ -49,6 +49,9 @@ Application Framework.
     <dir name="Horde">
      <file name="Config.php" role="php" />
      <file name="ErrorHandler.php" role="php" />
+     <dir name="Exception">
+      <file name="HookNotSet.php" role="php" />
+     </dir> <!-- /lib/Horde/Exception -->
      <file name="Help.php" role="php" />
      <file name="Menu.php" role="php" />
      <file name="Registry.php" role="php" />
@@ -118,6 +121,7 @@ Application Framework.
   <filelist>
    <install name="lib/Horde/Config.php" as="Horde/Config.php" />
    <install name="lib/Horde/ErrorHandler.php" as="Horde/ErrorHandler.php" />
+   <install name="lib/Horde/Exception/HookNotSet.php" as="Horde/Exception/HookNotSet.php" />
    <install name="lib/Horde/Help.php" as="Horde/Help.php" />
    <install name="lib/Horde/Menu.php" as="Horde/Menu.php" />
    <install name="lib/Horde/Registry.php" as="Horde/Registry.php" />