Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Components][EventDispatcher] describe that the event name and the event dispatcher are passed to even... #3523

Merged
merged 1 commit into from
Feb 4, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions components/event_dispatcher/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,30 +441,31 @@ which returns a boolean value::
EventDispatcher aware Events and Listeners
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

Lazy loading listeners::

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Acme\StoreBundle\Event\StoreSubscriber;

class Foo
{
private $started = false;

public function myLazyListener(Event $event)
public function myLazyListener(Event $event, $eventName, EventDispatcherInterface $dispatcher)
{
if (false === $this->started) {
$subscriber = new StoreSubscriber();
$event->getDispatcher()->addSubscriber($subscriber);
$dispatcher->addSubscriber($subscriber);
}

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

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Foo
{
public function myFooListener(Event $event)
public function myFooListener(Event $event, $eventName, EventDispatcherInterface $dispatcher)
{
$event->getDispatcher()->dispatch('log', $event);
$dispatcher->dispatch('log', $event);

// ... more code
}
Expand Down