Indium framework has one point of execution of all runnable objects, the Indium Event. It’s a core object and loaded from start. The idea behind the event it to unify all running events to one point to organise and dynamically change events on the run (with restriction obviously).
Events starts with these default runlevels:
- system
- precontroller
- controller
- postrcontroller
- postsystem
Each runlevel is treated as an ArrayObject for easy manipulation. Another array with the default runlevels registered (not connected to the ArrayObject). That’s the simple buildup of the event. The general idea is that when dispatchEvents() is called it will run through the levels registered and execute all objects. It can be closures or classes which implements any of the InterfaceEvents interfaces.
If you want to add a runLevel during runtime, you can call registerHookLevel($levelName, $hookToLevel).
$this->event = new Events();
$this->event->registerHookLevel('foo', 'system');
First parameter is the identifier for the new level and second parameter is which default level it should hook after (note that it’s only after a level a hook will be run). This is good if you want to extend the runtime dynamically.
Registrering an event is quite simple.
$this->event = new Events();
$this->event->registerEvent('Foo', function(){echo "Hello World!";} ,'precontroller');
First parameter is the name of the event, next is an object (either a closure or a class that implementens either InterfaceEvents or InterfaceEventsParam) and the last parameter is which level you want to register it to. You can add events to a running level (since it will always, for now, put it last) or whatever level that comes after the level you are running. You can never register to a level that’s been done, you’ll get a nice exception when that happens.
You can also register parameters to an event.
$this->
;event
= new Events
();
$this->
;registerEventParam
('Foo', array('bar', 'hello', 'world'));
$this->
;event
->
;registerEvent
('Foo', function(){echo "Hello World!";} ,'precontroller');
First parameter is as guessed wich event to register the params to. And the second one parameter is either an array och a string with the parameter you want to send in to the closure or class. Note, for it too work with classes, you have to implement InterfaceEventParam to the runnable class.
There’s way to unregister events as well on the run, if you want to remove an event that isn’t needed anymore.
$this->event = new Events();
$this->event->unregisterEvent('Foo', 'postcontroller');
This will unregister “Foo” from the postcontroller level. If there isn’t a event with that name an exception will be thrown.
There are some future plans to be able to register events on levels that hasn’t been executed and reorder the execution order for those events.
This is at least the Indium Event handler in it’s simplest form. As you can see, you can have full controll on what will happen and when. Making profiling and optimization easier to work with.