event plugin system

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: event plugin system

Post by josh »

arborint wrote:Chris and Luke, how do you see an events system working with a standard controller based framework? Is this a completely independent system that would be accessed statically, or could an events system be integrated into the controllers in some way?
What about the ways Zend / Doctrine do it. You create event methods on the controller like preDispatch() for instance. Plugins are just objects that override one or more events and form a filter before the controller even methods get called.. I'm probably mis-understanding though
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: event plugin system

Post by Christopher »

Maybe that is part of my question. Are these Events things that just need to be accessible and happen (without perhaps needing to know the details about how to make them happen), but are separate and independent from the normal framework dispatching? Or are do they need to be knitted into the controller dispatch pre/post?
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: event plugin system

Post by Luke »

I haven't yet figured out all the details, but basically the idea is to work these into my framework so that I can dispatch events when and where I want. I can use Zend's plugins for things like preDispatch and stuff. I don't intend to recreate Zend's plugin system.

If I want a certain event available to plugins, I simply dispatch it in my controller.

Code: Select all

class AuthController extends ImpSoft_Controller_Action {
 
    .. // snip
 
    public function loginAction() {
 
        $post = array();
        $errors = array();
        if ($this->isPost()) {
            $post = $this->getPost();
            $rules = with(new Q_Rule_Set())
                ->add(new Q_Rule_NotNull(), array('username', 'password'), 'Please enter your username and password');
            if ($rules->validate($post)) {
                // authenticate this fella...
                try {
                    $user = $this->verifyLogin($post['username'], $post['password']);
 
                    // dispatch login event
                    $event = new ImpSoft_Plugin_Event_Login($user);
                    $this->getPluginManager()->dispatchEvent($event);
 
                    $this->flash('Well congratulations on the successful login!', 'success');
                    $this->goToRoute('home');
                } catch (ImpSoft_Auth_Exception $e) {
                    Zend_Auth::getInstance()->clearIdentity();
                    $this->flash($e->getMessage());
                    $this->goToRoute('login');
                } catch (Zend_Db_Exception $e) {
                    $this->flash("We are experiencing some technical difficulties at the moment. Please try again later.");
                    $this->goToRoute('login');
                }
            } else {
                $errors = $rules->getErrors();
            }
        }
        $this->view->post = $post;
        $this->view->errors = $errors;
 
    }
 
    .. // snip
 
}
For each event, like Chris said, I create an event object. I initiate the object with data and objects relevant to the event (in this case, the newly logged in user). This creates a clear and concise API to my plugins: plugins which need to hook into the login event, will have access to the newly logged in user. The part I'm still working out is how plugins communicate back to the application to, for instance, cancel the authorization.

What I'm considering is allowing the plugins to throw exceptions for that type of situation. Look what happens in the above code if ImpSoft_Auth_Exception is thrown.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: event plugin system

Post by josh »

arborint wrote:Maybe that is part of my question. Are these Events things that just need to be accessible and happen (without perhaps needing to know the details about how to make them happen), but are separate and independent from the normal framework dispatching? Or are do they need to be knitted into the controller dispatch pre/post?
I think its the FC that chains the events and calls them before the template method on the controller gets called, thus if a plugin was written that changed the action about to be dispatched then the preDispatch() method on that action controller would never get called, instead of passing by parameter you get event info from the request object which is available to plugins
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: event plugin system

Post by Christopher »

Luke wrote:For each event, like Chris said, I create an event object. I initiate the object with data and objects relevant to the event (in this case, the newly logged in user). This creates a clear and concise API to my plugins: plugins which need to hook into the login event, will have access to the newly logged in user. The part I'm still working out is how plugins communicate back to the application to, for instance, cancel the authorization.
So I guess I am wondering what the difference between this:

Code: Select all

                    // dispatch login event
                     $event = new ImpSoft_Plugin_Event_Login($user);
                     $this->getPluginManager()->dispatchEvent($event);
And this is? (if they had the same functionality)

Code: Select all

                    // dispatch login event
                     $event = new ImpSoft_Plugin_Event_Login($user);
                     $event->dispatch();
I am under the impression that there are some global filter settings that determine if the event will actually be dispatched. That is a lot like what a Logger does. It seem like there is the possibility to build a single system that allows you to run objects under the control of some global settings. This system could be static or passed around or in a Registry. Is it the filtering that makes it unique?
josh wrote:I think its the FC that chains the events and calls them before the template method on the controller gets called, thus if a plugin was written that changed the action about to be dispatched then the preDispatch() method on that action controller would never get called, instead of passing by parameter you get event info from the request object which is available to plugins
I get the impression that the Events being discussed here are not Actions.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: event plugin system

Post by josh »

arborint wrote:I get the impression that the Events being discussed here are not Actions.
No but all events are coupled to some sort of action ( not in a MVC context necessarily ), my point was exactly what you just said though, that in a lot of cases there's no need for the even object because your event dispatcher can just pass the plugin objects the delegate objects they need at construction time, or in your example how the event object is not necessarily needed
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: event plugin system

Post by Christopher »

josh wrote:No but all events are coupled to some sort of action ( not in a MVC context necessarily ),
I think that was part of my question -- is there a 1:1 coupling with actions and events, or is it like logging where many actions can access them?
josh wrote:my point was exactly what you just said though, that in a lot of cases there's no need for the even object because your event dispatcher can just pass the plugin objects the delegate objects they need at construction time, or in your example how the event object is not necessarily needed
Huh?
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: event plugin system

Post by josh »

arborint wrote:I think that was part of my question -- is there a 1:1 coupling with actions and events, or is it like logging where many actions can access them?
Well technically a *:* coupling but yes, controller plugins are notified before and after things happen in the dispatch cycle, for instance dispatchLoopStartup()
josh wrote:Huh?
I meant if an object already has access to the data it will need, there is no need to redundantly pass that data as an event object.

For the observer pattern there's lots of frameworks that have implemented the observer pattern with various bells and whistles
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: event plugin system

Post by Christopher »

josh wrote:Well technically a *:* coupling but yes, controller plugins are notified before and after things happen in the dispatch cycle, for instance dispatchLoopStartup()
Yeah, I understand how controller plugins work ... but I don't think that has much to do with the events system being discussed.
josh wrote:I meant if an object already has access to the data it will need, there is no need to redundantly pass that data as an event object.

For the observer pattern there's lots of frameworks that have implemented the observer pattern with various bells and whistles
Again, I think the idea here is to have a system that may or may not run code based on configuration set elsewhere. You give it an event and it sorts how whether to run it.

And yeah, the Observer pattern is used lots of places...
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: event plugin system

Post by Luke »

So far I can't really give a good reason for the manager. I'm assuming it will be helpful in the way that you've mentioned. It will give me an opportunity to run some logic before an event is actually dispatched. Also, like Chris said, an event object isn't necessarily needed, I could just pass in an array or something, but using an object is a bit more robust and extensible.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: event plugin system

Post by Christopher »

I was just wondering if it would be better to make it explicit that there was "some logic before an event is actually dispatched" with something like this:

Code: Select all

abstract class Event_Abstract {
     protected static $preLogic = array();
 
     public method dispatch () {
          if ($this->checkPreLogic() ) {
               $this->run();     // run your code
          }
     } 
     private method checkPreLogic () {
          // code here to check if we should dispatch
     } 
     public method addPreLogic ($something) {
          // add some sort of logic thingy to self::preLogic
     } 
     abstract protected method run ();    // must be declared
}
I am almost thinking that you could create a more primitive base class to handle these basics, and then extend it to create Logging and Events classes. Both do something or not based on some globally settable logic/rules.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: event plugin system

Post by josh »

arborint wrote:Yeah, I understand how controller plugins work ... but I don't think that has much to do with the events system being discussed.
I was directly answering your question though :dubious:
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: event plugin system

Post by inghamn »

This is sounding a whole lot like Aspect Oriented Programming. Has there been a good AOP solution, yet, done in PHP?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: event plugin system

Post by Chris Corbyn »

inghamn wrote:This is sounding a whole lot like Aspect Oriented Programming. Has there been a good AOP solution, yet, done in PHP?
There's AspectPHP, but it's a bit of a hack IMHO.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: event plugin system

Post by josh »

Its actually different then AOP from my understanding, doesn't AOP "mix" code statically? Whereas events are dynamic, meaning that they can change during runtime without having to generate / weave code?
Post Reply