@@ -441,30 +441,31 @@ which returns a boolean value::
441
441
EventDispatcher aware Events and Listeners
442
442
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
443
443
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.
449
447
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.
453
453
454
454
Lazy loading listeners::
455
455
456
456
use Symfony\Component\EventDispatcher\Event;
457
+ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
457
458
use Acme\StoreBundle\Event\StoreSubscriber;
458
459
459
460
class Foo
460
461
{
461
462
private $started = false;
462
463
463
- public function myLazyListener(Event $event)
464
+ public function myLazyListener(Event $event, $eventName, EventDispatcherInterface $dispatcher )
464
465
{
465
466
if (false === $this->started) {
466
467
$subscriber = new StoreSubscriber();
467
- $event->getDispatcher() ->addSubscriber($subscriber);
468
+ $dispatcher ->addSubscriber($subscriber);
468
469
}
469
470
470
471
$this->started = true;
@@ -476,12 +477,13 @@ Lazy loading listeners::
476
477
Dispatching another event from within a listener::
477
478
478
479
use Symfony\Component\EventDispatcher\Event;
480
+ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
479
481
480
482
class Foo
481
483
{
482
- public function myFooListener(Event $event)
484
+ public function myFooListener(Event $event, $eventName, EventDispatcherInterface $dispatcher )
483
485
{
484
- $event->getDispatcher() ->dispatch('log', $event);
486
+ $dispatcher ->dispatch('log', $event);
485
487
486
488
// ... more code
487
489
}
0 commit comments