Skip to content

Commit 9676f2c

Browse files
committed
feature symfony#3523 [Components][EventDispatcher] describe that the event name and the event dispatcher are passed to even... (xabbuh)
This PR was merged into the 2.4 branch. Discussion ---------- [Components][EventDispatcher] describe that the event name and the event dispatcher are passed to even... ...t listeners by the event dispatcher | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.4+ | Fixed tickets | symfony#3521 Commits ------- 269c166 describe that the event name and the event dispatcher are passed to event listeners by the event dispatcher
2 parents a43f15a + 269c166 commit 9676f2c

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

components/event_dispatcher/introduction.rst

+14-12
Original file line numberDiff line numberDiff line change
@@ -441,30 +441,31 @@ which returns a boolean value::
441441
EventDispatcher aware Events and Listeners
442442
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
443443

444-
The ``EventDispatcher`` always injects a reference to itself in the passed event
445-
object. This means that all listeners have direct access to the
446-
``EventDispatcher`` object that notified the listener via the passed ``Event``
447-
object's :method:`Symfony\\Component\\EventDispatcher\\Event::getDispatcher`
448-
method.
444+
.. versionadded:: 2.4
445+
Since Symfony 2.4 the current event name and the ``EventDispatcher``
446+
itself are passed to the listeners as additional arguments.
449447

450-
This can lead to some advanced applications of the ``EventDispatcher`` including
451-
letting listeners dispatch other events, event chaining or even lazy loading of
452-
more listeners into the dispatcher object. Examples follow:
448+
The ``EventDispatcher`` always passes the dispatched event, the event's name
449+
and a reference to itself to the listeners. This can be used in some advanced
450+
usages of the ``EventDispatcher`` like dispatching other events in listeners,
451+
event chaining or even lazy loading of more listeners into the dispatcher
452+
object as shown in the following examples.
453453

454454
Lazy loading listeners::
455455

456456
use Symfony\Component\EventDispatcher\Event;
457+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
457458
use Acme\StoreBundle\Event\StoreSubscriber;
458459

459460
class Foo
460461
{
461462
private $started = false;
462463

463-
public function myLazyListener(Event $event)
464+
public function myLazyListener(Event $event, $eventName, EventDispatcherInterface $dispatcher)
464465
{
465466
if (false === $this->started) {
466467
$subscriber = new StoreSubscriber();
467-
$event->getDispatcher()->addSubscriber($subscriber);
468+
$dispatcher->addSubscriber($subscriber);
468469
}
469470

470471
$this->started = true;
@@ -476,12 +477,13 @@ Lazy loading listeners::
476477
Dispatching another event from within a listener::
477478

478479
use Symfony\Component\EventDispatcher\Event;
480+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
479481

480482
class Foo
481483
{
482-
public function myFooListener(Event $event)
484+
public function myFooListener(Event $event, $eventName, EventDispatcherInterface $dispatcher)
483485
{
484-
$event->getDispatcher()->dispatch('log', $event);
486+
$dispatcher->dispatch('log', $event);
485487

486488
// ... more code
487489
}

0 commit comments

Comments
 (0)