Skip to content
Merged
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions components/event_dispatcher/immutable_dispatcher.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.. index::
single: Event Dispatcher; Immutable

The Immutable Event Dispatcher
==============================

.. versionadded:: 2.1
This feature was added in Symfony 2.1.

The :class:`Symfony\\Component\\EventDispatcher\\ImmutableEventDispatcher` is
a locked or frozen event dispatcher. The dispatcher cannot register new
listeners or subscribers.

The ``ImmutableEventDispatcher`` takes another event dispatcher with all the
listeners and subscribers. The immutable dispatcher is just a proxy of this
original dispatcher.

To use it, first create a normal dispatcher (``EventDispatcher`` or
``ContainerAwareEventDispatcher``) and register some listeners or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these be API doc links ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrote it, I added links to the docs of these 2 classes. Then I removed them and changed it to api links and finally, just before pushing, I removed it completely.

I think it's irrelevant in this article.

subscribers::

use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();
$dispatcher->addListener('foo.action', function ($event) {
// ...
});

// ...

Now, inject that into an ``ImmutableEventDispatcher``::

use Symfony\Component\EventDispatcher\ImmutableEventDispatcher;
// ...

$immutableDispatcher = new ImmutableEventDispatcher($dispatcher);

You'll need to use this new dispatcher in your project.

If you are trying to execute one of the methods which modifies the dispatcher
(e.g. ``addListener``), a ``BadMethodCallException`` is thrown.
1 change: 1 addition & 0 deletions components/event_dispatcher/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Event Dispatcher
introduction
generic_event
container_aware_dispatcher
immutable_dispatcher
9 changes: 9 additions & 0 deletions components/event_dispatcher/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,15 @@ part of the listener's processing logic::
}
}

Other Dispatchers
-----------------

Besides the commonly used ``EventDispatcher``, the component comes with 2
other dispatchers:

* :doc:`/components/event_dispatcher/container_aware_dispatcher`
* :doc:`/components/event_dispatcher/immutable_dispatcher`

.. _Observer: http://en.wikipedia.org/wiki/Observer_pattern
.. _Closures: http://php.net/manual/en/functions.anonymous.php
.. _PHP callable: http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback
Expand Down
1 change: 1 addition & 0 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

* :doc:`/components/event_dispatcher/introduction`
* :doc:`/components/event_dispatcher/container_aware_dispatcher`
* :doc:`/components/event_dispatcher/immutable_dispatcher`
* :doc:`/components/event_dispatcher/generic_event`

* **Filesystem**
Expand Down