ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Khaos :: KhEvent

January 24, 2008 12:02pm

Subscribe [20]
  • #1 / Jan 24, 2008 12:02pm

    Neophyte's avatar

    Neophyte

    63 posts

    Updated - 21/04/2008 - Download - Khaos://:KhEvent - 0.2

    Introduction
    This library lets you have various events throughout your application for anything you want, some common examples would be events such as user login, logout, authenticate etc.. Once these events are in place you can create observers which are automatically called whenether the event is triggered. This lets you easilly extend your application without having to change your core code as it doesnt need to know any specifics about the observers.

    Sample Usage
    The best example is probably a user system where you can trigger events for user login, logout, creation, deletion and modification. Observers can then be created utilising these events such as an observer for phpbb allowing you to login, logout, create and delete users from phpbb without having to edit your controller or model files.

    Quick Reference

    /*
     * KhEvent
     *
     * array  trigger ( string $event[, array $args] )
     * void   register ( string $event, callback $handler )
     */
    
    // Example Trigger
    $results = $this->khevent->trigger('onLogin', array('username', 'password'));
    
    // Example Register
    $this->khevent->register('onLogin', array(&$this, 'login'));

    Pre-Defined Events
    onPreController
    onPostControllerConstructor
    onPostController
    onPostSystem

    Configuration
    KhEvent will work right out the box aslong as the system/cache folder is writable. However if you wish to change the behaviour of KhEvent then create the config file khaos.php.

    Below is a sample config which represents the default behaviour of KhCache.

    $config['event'] = array(
        'directory' => 'observers',
        'autoscan'  => true
    );

    everything in the config file is optional so options only need to be specified if you wish to override the default behaviour.

    directory - Directory relative to the application path where all the observer files are held (the directory can contain sub folders to aid with organisation)

    autoscan - When set to true if you make a change to an observer the observer map cache is automatically updated when set to false you need to manually delete the kh_event_map file in the cache folder if you make a change to the observers directory.

  • #2 / Jan 24, 2008 12:11pm

    xwero

    4145 posts

    How is this different from the hooks functionality?

  • #3 / Jan 24, 2008 12:44pm

    Neophyte's avatar

    Neophyte

    63 posts

    How is this different from the hooks functionality?

    bare in mind ive only been using CI for a few days so am far from familiar with the hooks system but to me the hooks system appears to be a way to adjust application flow and not an usable way of dealing with events. eg. in a user library i want to let other scripts be notified of certain events, user login, logout, creation, deletion, modification etc ... so below is how i’d go about it with the hooks approach and the above approach.

    Hooks
    I wish to log a user in so i do my usual login code and now i want to give other applications a chance to login using the same credentials say phpbb so i invoke $this->hooks->_call_hook(‘user_login’); and thats where i get stuck, how would you pass the user credentials as the params are fixed in the config/hooks.php config file for each hook, as best as i can see anyway. Assuming this wasnt a problem i’d want to see how each of the scripts phpbb etc responded and if any failed maybe abort the login or log a message though just scanning the code the return data doesnt appear to be captured or returned when you call a hook.

    Events
    The login method of the user library is called and again i do any usual login stuff and then use the following line to notify any interested scripts that a user is logging in

    $results = $this->event->trigger('onUserLogin', array($username, $password));

    each of the plugins listening for this event is now called and passed the users login credentials and passes back whether or not they where successful in logging in. an example might be a plugin called phpbb.php with the method onUserLogin which creates the phpbb session record.

    The user library login method now iterates over the results array looks for any problems in logging the user in if no problems arise it continues as normal if any of the plugin methods return an error i can choose to log the user out or just report a message or raise another event asking scripts to re-create/fix the users login etc ..

  • #4 / Jan 24, 2008 3:42pm

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Very cool idea, Thanks Neophyte.

  • #5 / Jan 25, 2008 12:16am

    Rick Jolly's avatar

    Rick Jolly

    729 posts

    Very interesting. This is what I think I understand: Instead of calling a plugin method directly from your application, you trigger an event and it is the job of the plugin to implement a method to accept that event if desired. That isolates the application from the plugin(s). Excellent way to make a more modular application.

    Could you explain how the pre-registered events work? Wouldn’t I have to register a class and method to use any of those pre-registered events much like your example to register a custom event. In that case, couldn’t the same thing be done using hooks (albeit in a less transparent way).

  • #6 / Jan 25, 2008 12:31am

    Majd Taby's avatar

    Majd Taby

    637 posts

    From what I picked up, the idea seemed interesting, i’m not exactly sure how it’s useful though. What gets notified of an event, what’s an event?

  • #7 / Jan 25, 2008 1:11am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    hooks/khaos/plugins.php is autoloaded and intercepts CI’s standard hooks as above, but you can register your own user events and trigger them at some stage later.

    libraries/khaos/events.php is include()‘d above and each plugin is loaded via the config/khaos.php $config[‘plugins’] array and registers itself with the event dispatcher.

    The event listener intercepts the events and the appropriate plugin/functions() will run when a system_event (hook) is triggered or a user registered event is triggered by you.

    Hope this helps. (this was just a quick look over the code, I may have missed something)

  • #8 / Jan 25, 2008 5:33am

    Neophyte's avatar

    Neophyte

    63 posts

    My explanations always let me down heh

    Could you explain how the pre-registered events work? Wouldn’t I have to register a class and method to use any of those pre-registered events much like your example to register a custom event. In that case, couldn’t the same thing be done using hooks (albeit in a less transparent way).

    wiredesignz post just above mine gives an excellent run down of whats going on but just to clarify the power of the library comes in setting up your own events (as you pointed out) and it was only a small amount of code to add the CI hooks as events so i did.

    As a quick example of how it could come in handy the SMF forum lets you track whos online accross your entire site just by calling one of their functions so if you had an smf plugin listening to the usual sort of events of your user system, login, logout, create etc ... you could also add the method onPreController() to your plugin which would be called every page view in which you could update the whos online of SMF, this would keep everything to do with integrating your application with SMF in the same smf.php file and save you setting up another hook.

    In response to Zaatar i think Rick Jolly’s first paragraph sums it up nicely

    Very interesting. This is what I think I understand: Instead of calling a plugin method directly from your application, you trigger an event and it is the job of the plugin to implement a method to accept that event if desired. That isolates the application from the plugin(s). Excellent way to make a more modular application.

    and again wiredesignz more technical explanation should help

  • #9 / Jan 25, 2008 6:35am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    I think its a great addition to my CI armoury, I havent really seen anyone use Plugins with CI yet, this might generate some new ideas.

  • #10 / Jan 27, 2008 6:57pm

    Crafter

    148 posts

    This looks great. I see a lot of potential here and I plan to look at it further shortly. Thanks for sharing.

  • #11 / Feb 04, 2008 3:58pm

    Majd Taby's avatar

    Majd Taby

    637 posts

    I’m thinking this could be great for CodeExtinguisher, it makes it really easy to make things happen on insert, update, delete etc. Right now I have basic callbacks, but this is much better.

  • #12 / Feb 13, 2008 12:53pm

    Majd Taby's avatar

    Majd Taby

    637 posts

    is it possible to have multiple classes handle the same event?

    i.e. can I have two files in khaos/plugins/ which both define a ‘onUserLogin’, and both which get called automatically when the onUserLogin event is triggered?

  • #13 / Feb 13, 2008 2:04pm

    Neophyte's avatar

    Neophyte

    63 posts

    is it possible to have multiple classes handle the same event?

    i.e. can I have two files in khaos/plugins/ which both define a ‘onUserLogin’, and both which get called automatically when the onUserLogin event is triggered?

    Yup.

    The disptacher keeps track of everything so when you trigger an event it knows all the classes/plugins which have a method to handle that event and it calls each of them (passing any optional args you want and then returns an indexed array of the responses received from each of the plugins which responded)

  • #14 / Feb 13, 2008 2:07pm

    Majd Taby's avatar

    Majd Taby

    637 posts

    Ok, my next question was why it returns an indexed array…i see now..this is fantastic neophyte…it’ll be in the next version of codeExtinguisher 😊

  • #15 / Feb 14, 2008 3:10pm

    WolfgangA

    46 posts

    @Neophyte
    Is your event system based on Observer Design Pattern?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases