Last active
August 29, 2015 14:01
-
-
Save bakura10/b88e3ba70c7a4e8b5391 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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