-
Notifications
You must be signed in to change notification settings - Fork 61
Question - Best way for modules to attach listeners #40
Description
Hello
First my apologies if this is not the right place to ask questions.
Please let me know if there is a better place -
Thank you
Here is my issue
I was wondering what would be the best way to attach listeners to an Event Manager from a Different Module
Here is my scenario, I have a user form lets call it Application\UserForm
In its class factory I inject an Event Manager and a Shared Event Manager
factory:
$userForm = new UserForm();
$sharedEventManager = $container->get('FormSharedEventManager');
$eventManager = new EventManager($sharedEventManager);
$userForm->setEventManager($eventManager);
What is the Best way for another module (lets call it Module X) to attach its listener to this Form (and any other forms really that use the same FormSharedEventManager ) ?
Here are my choices
Delegators
I could add Delegators in the Module X Config
Option 1: at the UserForm level
--> so that everytime UserForm is created My delegator class would fetch the event manager and directly attach listeners
Option 2
-> Same idea but at the shared event manager level
Everytime FormSharedEventManager is created i would attach the listeners
(That also allow me to use the identifiers )
Config file parameters
Option 3:
i could create a new config parameter
return [
'shared_event_manager_listeners' => [
'FormSharedEventManager::class => [
ModuleXFormListenersAggregate::class
]
]
];
And I would have a to use a new Factory to create FormSharedEventManager
'factories' => [
FormSharedEventManager::class => AggregateAwareSharedEventManagerFACTORY::class
]
My new Factory (AggregateAwareSharedEventManagerFACTORY)
Would then fetch all the listeners aggregates from the "shared_event_manager_listers" config attached to the requested class (FormSharedEventManager::class)
I want to use the fastest or cleanest way to attach those listeners
Ideally the Module would also add listeners to other part of the process
Pros and Cons
Option 1:
- Pros: The listeners are only attached when we need create UserForm class
- Cons: Need to create a Delegator file for each forms that the module want to listen to
Option 2:
- Pros: Using the SharedEvent Manager I can use identifiers and thus attach all my Event from ONE delegator file
- Cons: ?
Option 3:
- Pros: We are not using delegators - so it might be faster ? (am I right to assume so ?)
- Cons:
Need to create new parameter in the config, Need to create a new factory
Ideally Module X would also want to listen to other events like Controller Events
For e.g When a user submit the form Module X will want to send an email
Would it be smarter to Inject the default Shared Event Manager into my Form instead of creating a new FormSharedEventManager
and attaching the listeners into the Default shared event manager ?