Skip to content

Commit 7ce9cfe

Browse files
javiereguiluzxabbuh
authored andcommitted
Deprecated ContainerAwareEventDispatcher
1 parent c45a7f2 commit 7ce9cfe

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

components/event_dispatcher.rst

+12-17
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ answer.
2222
Consider the real-world example where you want to provide a plugin system
2323
for your project. A plugin should be able to add methods, or do something
2424
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
2626
multiple inheritance was possible with PHP, it comes with its own drawbacks.
2727

2828
The Symfony EventDispatcher component implements the `Mediator`_ pattern
@@ -187,33 +187,28 @@ event. In many cases, a special event subclass is passed with extra
187187
information. You can check the documentation or implementation of each event to
188188
determine 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

443438
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
445440
applications of the ``EventDispatcher`` including dispatching other events inside
446441
listeners, chaining events or even lazy loading listeners into the dispatcher object.
447442

components/event_dispatcher/container_aware_dispatcher.rst

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
The Container Aware Event Dispatcher
55
====================================
66

7+
.. versionadded:: 3.3
8+
The ``ContainerAwareEventDispatcher`` class has been deprecated in Symfony 3.3
9+
and it will be removed in Symfony 4.0. Use ``EventDispatcher`` with
10+
closure-proxy injection instead.
11+
712
Introduction
813
------------
914

components/event_dispatcher/immutable_dispatcher.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ The ``ImmutableEventDispatcher`` takes another event dispatcher with all
1212
the listeners and subscribers. The immutable dispatcher is just a proxy
1313
of this original dispatcher.
1414

15-
To use it, first create a normal dispatcher (``EventDispatcher`` or
16-
``ContainerAwareEventDispatcher``) and register some listeners or
17-
subscribers::
15+
To use it, first create a normal ``EventDispatcher`` dispatcher and register
16+
some listeners or subscribers::
1817

1918
use Symfony\Component\EventDispatcher\EventDispatcher;
2019

0 commit comments

Comments
 (0)