Skip to content

Instantly share code, notes, and snippets.

@bakura10
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakura10/b88e3ba70c7a4e8b5391 to your computer and use it in GitHub Desktop.
Save bakura10/b88e3ba70c7a4e8b5391 to your computer and use it in GitHub Desktop.
<?php
// ListenerAggregateInterface would change, and it would have... a static method (yeah yeah):
class UserListenerAggregate implements ListenerAggregateInterface
{
static function getListenersConfiguration()
{
// This would be a standardized configuration
return [
'listeners' => [
'eventName' => [static::class, 'callbackToCall', 2], // 2 is priority
],
'shared_listeners' => [
'identifier' => [
'eventName' => [static::class, 'anotherMethod', 3] // 3 is priority
]
]
];
}
}
// Here is how you wire up in your Module class
class Module
{
public function onBootstrap(MvcEvent $event)
{
$eventManager = $event->getEventManager();
$eventManager->attachAggregate(UserListenerAggregate::class); // Note that there is no instantiation here
}
}
// What the attachAggregate will do is actually something like that:
public function attachAggregate($aggregate)
{
$configuration = $aggregate::getListenersConfiguration();
foreach ($configuration['listeners'] as $eventName => $listeners) {
$this->attachListeners($eventName, $listeners);
}
foreach ($configuration['shared_listeners'] as $identifier => $listeners) {
foreach ($identifiers as $eventName => $listeners) {
$this->attachSharedListeners($identifier, $eventName, $identifiers)
}
}
}
// Now, the problem is that obviously, none of those listeners are instantiated... So what we do is having
// a ListenerPluginManager. This is a standard plugin manager for listeners. So before actually triggering
// the event, we get all the listeners, and initialize them
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment