Part of the EllisLab Network
   
1 of 5
1
Khaos :: KhEvent
Posted: 24 January 2008 10:02 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  77
Joined  01-24-2008

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.

 Signature 

Khaos :: [ ACL - Cache - Event ] - Contact :: [ - IRC ]

Profile
 
 
Posted: 24 January 2008 10:11 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

How is this different from the hooks functionality?

Profile
 
 
Posted: 24 January 2008 10:44 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  77
Joined  01-24-2008
xwero - 24 January 2008 10:11 AM

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 ..

 Signature 

Khaos :: [ ACL - Cache - Event ] - Contact :: [ - IRC ]

Profile
 
 
Posted: 24 January 2008 01:42 PM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2612
Joined  06-10-2007

Very cool idea, Thanks Neophyte.

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 24 January 2008 10:16 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  837
Joined  02-05-2007

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).

 Signature 

“I am the terror that flaps in the night”

Profile
 
 
Posted: 24 January 2008 10:31 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  716
Joined  06-07-2007

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?

 Signature 

CodeExtinguisher
Download: codex2_rc14.2.zip - 219 KiloBytes of Gloriousness!
Demo: Public preview - login with preview:preview
Temporary Docs: PBWiki

Profile
 
 
Posted: 24 January 2008 11:11 PM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2612
Joined  06-10-2007

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)

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 25 January 2008 03:33 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  77
Joined  01-24-2008

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

 Signature 

Khaos :: [ ACL - Cache - Event ] - Contact :: [ - IRC ]

Profile
 
 
Posted: 25 January 2008 04:35 AM   [ Ignore ]   [ # 8 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2612
Joined  06-10-2007

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.

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 27 January 2008 04:57 PM   [ Ignore ]   [ # 9 ]  
Research Assistant
RankRankRank
Total Posts:  558
Joined  06-17-2006

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

 Signature 

CodeCrafter - Open Source Code Generation for CI

Profile
 
 
Posted: 04 February 2008 01:58 PM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  716
Joined  06-07-2007

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.

 Signature 

CodeExtinguisher
Download: codex2_rc14.2.zip - 219 KiloBytes of Gloriousness!
Demo: Public preview - login with preview:preview
Temporary Docs: PBWiki

Profile
 
 
   
1 of 5
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 114983 Total Logged-in Users: 60
Total Topics: 122429 Total Anonymous Users: 2
Total Replies: 647264 Total Guests: 478
Total Posts: 769693    
Members ( View Memberlist )