[dev] Hooks config change proposal

Michael M Slusarz slusarz at horde.org
Mon Jul 27 06:21:52 UTC 2009


Quoting Chuck Hagenbuch <chuck at horde.org>:

> Quoting Michael M Slusarz <slusarz at horde.org>:
>
>> For hooks, what about changing the config file to a class.  So an  
>> app's config file would look like:
>>
>> class App_Hooks
>> {
>>    // Example Hook
>>    // static public function hook_name
>>    // {
>>    // }
>> }
>>
>> Then Horde::callHook() just needs to include the config file and do  
>> a method_exists check.  This would get rid of the function_exists()  
>> calls that litter hooks.php, gets rid of the static $_hooksLoaded  
>> variable in Horde::callHook (we can rely on require_once instead,  
>> or configure Autoloader to look for *_Hooks classes in the config  
>> directory), and would also get rid of the need to have a config  
>> entry to activate the hooks.  IMHO, defining the hook in the  
>> hooks.php file should be sufficient to activate.
>
> I agree. One request: make the methods instance methods (not static)  
> and create an instance of the App_Hooks class to use it. I haven't  
> quite figured it out yet, but this could let us do some interesting  
> things for making a config-like class using PHP 5.3's closures.  
> Something like:
>
> class Imp_Hooks
> {
>     public $hook_name = new Hook_Strategy_Awesome();
> }
>
> that doesn't parse, but we could have a Horde_Application_Hooks base  
> class that translated strings into callable variables.
>
> That would allow people to distribute classes that implemented a  
> hook, while keeping the main body of code out of the config file.

Done.  Might be useful to read horde/config/hooks.php.dist as I tried  
to do a better job of documenting the format there.

Hooks now throw Horde_Exceptions only on fatal errors.  if the hook is  
not set, a Horde_Exception_HookNotSet exception is thrown.

Here's an example from imp (setting compose message trailer text),  
showing how to deal with non-existent hooks:

try {
     $trailer = Horde::callHook('trailer', array($trailer), 'imp');
} catch (Horde_Exception_HookNotSet $e) {}

If the hook is not set, $trailer will remain what it was before the  
callHook() call was made.

An example of code useful to
Converted the preauth/postauth/pushapp hooks to run in each individual  
application.

That's pretty much it.

michael

-- 
___________________________________
Michael Slusarz [slusarz at horde.org]



More information about the dev mailing list