On Sunday, February 23, 2014 1:37:09 AM UTC-5, Sven Versteegen wrote:
In all the Framework examples out there we can see how to fire an Event, so how is the best way to get plugins in?
What I have so far:
Plugin Folder structure like Joomla! CMS
Don't include loading in triggering. Moreover, don't assume what the dispatcher is called, just trigger it, ie:
triggering:
// @event onBeforeExecute
$this->triggerEvent(with(new Event('onBeforeExecute'))->setArgument('app', $this));
// Perform application routines.
$this->doExecute();
// @event onAfterExecute
$this->triggerEvent(with(new Event('onAfterExecute'))->setArgument('app', $this));
For safe usage, use:
// @event onBeforeExecute if ($this instanceof Joomla\Event\DispatcherInterface)
{
$this->triggerEvent(with(new Event('onBeforeExecute'))->setArgument('app', $this));
}
// Perform application routines.
$this->doExecute();
// @event onAfterExecute
if ($this instanceof Joomla\Event\DispatcherInterface)
{
$this->triggerEvent(with(new Event('onAfterExecute'))->setArgument('app', $this));
}
So for a theoretical application:
class MyApplication extends Joomla\Application\Web\ WebClient implements Joomla\Event\DispatcherInterface
public function triggerEvent($event)
{
if is_null($this->dispatcher)
{
$this->dispatcher = $this->initDispatcher();
}
return $this->dispatcher->triggerEvent($event);
}
}
It is completely up to the application to create the "true" dispatcher and send the event to it. It is completely up the the application to initialize the dispatcher with events based on whatever it feels should be loaded.
It also means there is no reason to ever go looking for the "application" dispatcher. To trigger an event, just call $this->triggerEvent() or if you want to be safe, use instanceof checking to ensure that it is legal. Ie a so called "component" would call $this->triggerEvent. The component is free to chain that plugin event to use the application dispatcher[which would be the assumed default], or it could run it's own independent dispatcher, or even have multiple independent dispatchers.