@@ -22,7 +22,7 @@ answer.
2222Consider the real-world example where you want to provide a plugin system
2323for your project. A plugin should be able to add methods, or do something
2424before or after a method is executed, without interfering with other plugins.
25- This is not an easy problem to solve with single inheritance, and even if
25+ This is not an easy problem to solve with single inheritance, and even if
2626multiple inheritance was possible with PHP, it comes with its own drawbacks.
2727
2828The Symfony EventDispatcher component implements the `Mediator `_ pattern
@@ -187,33 +187,28 @@ event. In many cases, a special event subclass is passed with extra
187187information. You can check the documentation or implementation of each event to
188188determine which instance is passed.
189189
190- .. sidebar :: Registering Event Listeners in the Service Container
190+ .. sidebar :: Registering Event Listeners and Subscribers in the Service Container
191191
192- When you are using the
193- :class: `Symfony\\ Component\\ EventDispatcher\\ ContainerAwareEventDispatcher `
194- and the
195- :doc: `DependencyInjection component </components/dependency_injection >`,
196- you can use the
197- :class: `Symfony\\ Component\\ EventDispatcher\\ DependencyInjection\\ RegisterListenersPass `
198- to tag services as event listeners::
192+ Registering service definitions and tagging them with the
193+ ``kernel.event_listener `` and ``kernel.event_subscriber `` tags is not enough
194+ to enable the event listeners and event subscribers. You must also register
195+ a compiler pass called ``RegisterListenersPass() `` in the container builder::
199196
200197 use Symfony\Component\DependencyInjection\ContainerBuilder;
201198 use Symfony\Component\DependencyInjection\Definition;
202199 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
203200 use Symfony\Component\DependencyInjection\Reference;
204- use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher ;
201+ use Symfony\Component\EventDispatcher\EventDispatcher ;
205202 use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
206203
207204 $containerBuilder = new ContainerBuilder(new ParameterBag());
205+ // register the compiler pass that handles the 'kernel.event_listener'
206+ // and 'kernel.event_subscriber' service tags
208207 $containerBuilder->addCompilerPass(new RegisterListenersPass());
209208
210- // register the event dispatcher service
211- $containerBuilder->setDefinition('event_dispatcher', new Definition(
212- ContainerAwareEventDispatcher::class,
213- array(new Reference('service_container'))
214- ));
209+ $containerBuilder->register('event_dispatcher', EventDispatcher::class);
215210
216- // register your event listener service
211+ // register an event listener
217212 $listener = new Definition(\AcmeListener::class);
218213 $listener->addTag('kernel.event_listener', array(
219214 'event' => 'foo.action',
@@ -441,7 +436,7 @@ EventDispatcher Aware Events and Listeners
441436~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
442437
443438The ``EventDispatcher `` always passes the dispatched event, the event's
444- name and a reference to itself to the listeners. This can lead to some advanced
439+ name and a reference to itself to the listeners. This can lead to some advanced
445440applications of the ``EventDispatcher `` including dispatching other events inside
446441listeners, chaining events or even lazy loading listeners into the dispatcher object.
447442
0 commit comments