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 thougharborint 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?
event plugin system
Moderator: General Moderators
Re: event plugin system
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: event plugin system
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)
Re: event plugin system
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.
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.
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
}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.
Re: event plugin system
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 pluginsarborint 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?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: event plugin system
So I guess I am wondering what the difference between this: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.
Code: Select all
// dispatch login event
$event = new ImpSoft_Plugin_Event_Login($user);
$this->getPluginManager()->dispatchEvent($event);Code: Select all
// dispatch login event
$event = new ImpSoft_Plugin_Event_Login($user);
$event->dispatch();I get the impression that the Events being discussed here are not Actions.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
(#10850)
Re: event plugin system
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 neededarborint wrote:I get the impression that the Events being discussed here are not Actions.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: event plugin system
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:No but all events are coupled to some sort of action ( not in a MVC context necessarily ),
Huh?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
(#10850)
Re: event plugin system
Well technically a *:* coupling but yes, controller plugins are notified before and after things happen in the dispatch cycle, for instance dispatchLoopStartup()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?
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.josh wrote:Huh?
For the observer pattern there's lots of frameworks that have implemented the observer pattern with various bells and whistles
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: event plugin system
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:Well technically a *:* coupling but yes, controller plugins are notified before and after things happen in the dispatch cycle, for instance dispatchLoopStartup()
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.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
And yeah, the Observer pattern is used lots of places...
(#10850)
Re: event plugin system
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: event plugin system
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:
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.
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
}(#10850)
Re: event plugin system
I was directly answering your question thougharborint wrote:Yeah, I understand how controller plugins work ... but I don't think that has much to do with the events system being discussed.
- inghamn
- Forum Contributor
- Posts: 174
- Joined: Mon Apr 16, 2007 10:33 am
- Location: Bloomington, IN, USA
Re: event plugin system
This is sounding a whole lot like Aspect Oriented Programming. Has there been a good AOP solution, yet, done in PHP?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: event plugin system
There's AspectPHP, but it's a bit of a hack IMHO.inghamn wrote:This is sounding a whole lot like Aspect Oriented Programming. Has there been a good AOP solution, yet, done in PHP?
Re: event plugin system
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?