@@ -22,7 +22,7 @@ answer.
22
22
Consider the real-world example where you want to provide a plugin system
23
23
for your project. A plugin should be able to add methods, or do something
24
24
before 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
26
26
multiple inheritance was possible with PHP, it comes with its own drawbacks.
27
27
28
28
The Symfony EventDispatcher component implements the `Mediator `_ pattern
@@ -198,7 +198,6 @@ determine which instance is passed.
198
198
to tag services as event listeners::
199
199
200
200
use Symfony\Component\DependencyInjection\ContainerBuilder;
201
- use Symfony\Component\DependencyInjection\Definition;
202
201
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
203
202
use Symfony\Component\DependencyInjection\Reference;
204
203
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
@@ -208,23 +207,19 @@ determine which instance is passed.
208
207
$containerBuilder->addCompilerPass(new RegisterListenersPass());
209
208
210
209
// register the event dispatcher service
211
- $containerBuilder->setDefinition('event_dispatcher', new Definition(
212
- ContainerAwareEventDispatcher::class,
213
- array(new Reference('service_container'))
214
- ));
210
+ $containerBuilder->register('event_dispatcher', ContainerAwareEventDispatcher::class)
211
+ ->addArgument(new Reference('service_container'));
215
212
216
213
// register your event listener service
217
- $listener = new Definition(\AcmeListener::class);
218
- $listener->addTag('kernel.event_listener', array(
219
- 'event' => 'acme.foo.action',
220
- 'method' => 'onFooAction',
221
- ));
222
- $containerBuilder->setDefinition('listener_service_id', $listener);
214
+ $containerBuilder->register('listener_service_id', \AcmeListener::class)
215
+ ->addTag('kernel.event_listener', array(
216
+ 'event' => 'acme.foo.action',
217
+ 'method' => 'onFooAction',
218
+ ));
223
219
224
220
// register an event subscriber
225
- $subscriber = new Definition(\AcmeSubscriber::class);
226
- $subscriber->addTag('kernel.event_subscriber');
227
- $containerBuilder->setDefinition('subscriber_service_id', $subscriber);
221
+ $containerBuilder->register('subscriber_service_id', \AcmeSubscriber::class)
222
+ ->addTag('kernel.event_subscriber');
228
223
229
224
By default, the listeners pass assumes that the event dispatcher's service
230
225
id is ``event_dispatcher ``, that event listeners are tagged with the
@@ -441,7 +436,7 @@ EventDispatcher Aware Events and Listeners
441
436
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
442
437
443
438
The ``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
445
440
applications of the ``EventDispatcher `` including dispatching other events inside
446
441
listeners, chaining events or even lazy loading listeners into the dispatcher object.
447
442
0 commit comments