symfony/symfony — pull request #9941 by lemoinem (2014-01-04 22:38:40+00:00)
Title: [WIP][EventDispatcher] Allow per tag EventDispatcher in RegisterListenersPass

| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | yes
| Tests pass? | yes
| Fixed tickets | N/A
| License | MIT
| Doc PR |

- [ ] submit changes to the documentation

Since the RegisterListenersPass is now in EventDispatcher, I think it could be useful to allow specifying dispatchers on a per-tag basis.

In HttpKernel, the main purpose of this pass was to register Kernel listeners. However, in EventDispatcher, it could be used to register Events on any EventDispatcher Service (or anything quacking like it, I'm not enforcing the interface check).

The new deprecation is simple: Currently, if the pass is called even though the `$dispatcherService` is not available, then it will abort silently. This behaviour is here expanded to any dispatcher specified (If the service doesn't exist, I just let it go). However, starting with Symfony 3.0, I think it would be better to throw an Exception in such cases (This is more consistent behaviour for all errors the RegistrationListenerPass could meet).

Performance consideration: Previously, as soon as the `$dispatcherService` wasn't available, we could abort the RegistrationListenerPass. Now, we still have to process every tag in order to make sure to register listeners and subscribers on others dispatcher. Similarly, if previously a service had several `$subscriberTag` on it, we were doing the processing only once. It is now done for every tag since each one could potentially define a different dispatcher.

BWC concern: Now that serveral subscriber tags are processed several times, a Subscriber will be injected as many times as there are tags for a given dispatcher. This might result in duplication of Listeners. However, the risk is already present and ignored for Listeners. If this is deemed as a BWC break, I could easily mitigate it and ensure every dispatcher is processed only once. Once in 3.0, I'd however recommend to either expand this new behaviour to Listeners too, or revert Subscribers subscription to the simple behaviour, just for consistence and least surprise's sake.