Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between event listerners and subscribers in symfony2 [duplicate]

I'm working in the Symfony2 framework and wondering when would one use a Doctrine subscriber versus a listener. Doctrine's documentation for listeners is very clear, however subscribers are rather glossed over. Symfony's cookbook entry is similar.

like image 963
nurikabe Avatar asked May 24 '12 02:05

nurikabe


People also ask

How do event listeners work?

Often an event listener is registered with the object that generates the event. When the event occurs, the object iterates through all listeners registered with it informing them of the event.

Are Symfony events Async?

Symfony events are currently processed synchronously, enabling the async configuration for EnqueueBundle causes tagged listeners to defer action to a consumer asynchronously.


5 Answers

From my point of view, there is only one major difference:

  • The Listener is signed up specifying the events on which it listens.
  • The Subscriber has a method telling the dispatcher what events it is listening to

This might not seem like a big difference, but if you think about it, there are some cases when you want to use one over the other:

  • You can assign one listener to many dispatchers with different events, as they are set at registration time. You only need to make sure every method is in place in the listener
  • You can change the events a subscriber is registered for at runtime and even after registering the subscriber by changing the return value of getSubscribedEvents (Think about a time where you listen to a very noisy event and you only want to execute something one time)

There might be other differences I'm not aware of though!

like image 200
Sgoettschkes Avatar answered Oct 21 '22 19:10

Sgoettschkes


Don't know whether it is done accidentally or intentionally.. But subscribers have higher priority that listeners - https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPass.php#L73-L98

From doctrine side, it doesn't care what it is (listener or subscriber), eventually both are registered as listeners - https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/EventManager.php#L137-L140

This is what I spotted.

like image 43
Ruslan Polutsygan Avatar answered Oct 21 '22 21:10

Ruslan Polutsygan


You should use event subscriber when you want to deal with multiple events in one class, for example in this symfony2 doc page article, one may notice that event listener can only manage one event, but lets say you want to deal with several events for one entity, prePersist, preUpdate, postPersist etc... if you use event listener you would have to code several event listener, one for each event, but if you go with event subscriber you just have to code one class the event susbcriber, look that with the event subscriber you can manage more than one event in one class, well thats the way i use it, i preffer to code focused in what the model business need, one example of this may be went you want to handle several lifecycle events globaly only for a group of your entities, to do that you can code a parent class and defined those global methods in it, then make your entities inherit that class and later in your event susbcriber you subscribe every event you want, prePersist, preUpdate, postPersist etc... and then ask for that parent class and execute those global methods.

like image 32
metalvarez Avatar answered Oct 21 '22 21:10

metalvarez


Another important thing: Doctrine EventSubscribers do not allow you to set a priority.

Read more on this issue here

like image 31
Wilt Avatar answered Oct 21 '22 21:10

Wilt


Both allow you to execute something on a particular event pre / post persist etc.

However listeners only allow you to execute behaviours encapsulated within your Entity. So an example might be updating a "date_edited" timestamp.

If you need to move outside the context of your Entity, then you'll need a subscriber. A good example might be for calling an external API, or if you need to use / inspect data not directly related to your Entity.

like image 42
Lee Davis Avatar answered Oct 21 '22 20:10

Lee Davis