Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update EventDispatcher Doc

  • Loading branch information...
commit e96a72a3cf86e9703e01f20a4932ddce3da8e0cb 1 parent 67c9804
Javier Lopez authored
5 EventDispatcher/EventSubscriber.php
@@ -7,7 +7,10 @@ class EventSubscriber implements EventSubscriberInterface
7 7 {
8 8 public static function getSubscribedEvents()
9 9 {
10   - return array('event.a' => 'trigger');
  10 + return array(
  11 + 'event.a' => 'trigger'
  12 + 'event.a' => function(\Event $e){ echo "Lambda function executed"; }
  13 + );
11 14 }
12 15
13 16 public function trigger(Event $e)
18 EventDispatcher/example1.php
... ... @@ -1,5 +1,23 @@
1 1 <?php
2 2
  3 +/*
  4 + * Components Playground: EventDispatcher (Example 1)
  5 + *
  6 + * Description:
  7 + * EventDispatcher is an implementation of the Observer Pattern. There are four important
  8 + * concepts you should understand properly: events, listeners, subscribers and the event
  9 + * dispatcher.
  10 + *
  11 + * The last one is the glue between the others and is responsible for triggering the events.
  12 + *
  13 + * In this example we create a listener (EventListener) that will do something whenever
  14 + * a event is dispatched by the EventDispatcher.
  15 + *
  16 + * Inspired by http://fabien.potencier.org/article/49/what-is-symfony2
  17 + *
  18 + * @author: Javier Lopez <f12loalf@gmail.com>
  19 + */
  20 +
3 21 require_once __DIR__."/../autoload.php";
4 22
5 23 use Symfony\Component\EventDispatcher\Event;
19 EventDispatcher/example2.php
... ... @@ -1,5 +1,22 @@
1 1 <?php
2 2
  3 +/*
  4 + * Components Playground: EventDispatcher (Example 2)
  5 + *
  6 + * Description:
  7 + * If you want to give priority to one listener over other you have to
  8 + * assign its priority when adding it to the EventDispatcher.
  9 + *
  10 + * The higher the priority is the sooner it is called.
  11 + *
  12 + * From this example we can also see that one object can listen to various
  13 + * events and one event can be listened by various listeners.
  14 + *
  15 + * Inspired by http://fabien.potencier.org/article/49/what-is-symfony2
  16 + *
  17 + * @author: Javier Lopez <f12loalf@gmail.com>
  18 + */
  19 +
3 20 require_once __DIR__."/../autoload.php";
4 21
5 22 use Symfony\Component\EventDispatcher\Event;
@@ -9,8 +26,6 @@
9 26 $dispatcher->addListener('event.a', array('EventAListener', 'trigger'), 1);
10 27 $dispatcher->addListener('event.a', array('EventBListener', 'trigger'), 2);
11 28
12   -// The listener with higher priority is executed before
13   -
14 29 $dispatcher->dispatch('event.a');
15 30
16 31 class EventBListener
14 EventDispatcher/example3.php
... ... @@ -1,5 +1,19 @@
1 1 <?php
2 2
  3 +/*
  4 + * Components Playground: Command (Example 3)
  5 + *
  6 + * Description:
  7 + * The event can be stopped for any of the listeners listening to this event
  8 + *
  9 + * In this example, EventAListener will never be able to execute its code
  10 + * because EventBListener stops it.
  11 + *
  12 + * Inspired by http://fabien.potencier.org/article/49/what-is-symfony2
  13 + *
  14 + * @author: Javier Lopez <f12loalf@gmail.com>
  15 + */
  16 +
3 17 require_once __DIR__."/../autoload.php";
4 18
5 19 use Symfony\Component\EventDispatcher\Event;
15 EventDispatcher/example4.php
... ... @@ -1,5 +1,20 @@
1 1 <?php
2 2
  3 +/*
  4 + * Components Playground: DependencyInjection (Example 4)
  5 + *
  6 + * Description:
  7 + * Listeners can do much more than just stopping it. They can modify it.
  8 + *
  9 + * In this example we see EventBListener changes the name of the event.
  10 + * This could be usefull if any listener add some information to the event
  11 + * and the last one to be executed process it.
  12 + *
  13 + * Inspired by http://fabien.potencier.org/article/49/what-is-symfony2
  14 + *
  15 + * @author: Javier Lopez <f12loalf@gmail.com>
  16 + */
  17 +
3 18 require_once __DIR__."/../autoload.php";
4 19
5 20 use Symfony\Component\EventDispatcher\Event;
15 EventDispatcher/example5.php
... ... @@ -1,5 +1,20 @@
1 1 <?php
2 2
  3 +/*
  4 + * Components Playground: Command (Example 1)
  5 + *
  6 + * Description:
  7 + * Here we see another way to append listeners to the dispatcher, this time
  8 + * we are using Subscribers. Take a look at EventSubscriber to see how it
  9 + * works.
  10 + *
  11 + * Take a look at EventSubscriber.php to see a subscriber implementation.
  12 + *
  13 + * Inspired by http://fabien.potencier.org/article/49/what-is-symfony2
  14 + *
  15 + * @author: Javier Lopez <f12loalf@gmail.com>
  16 + */
  17 +
3 18 require_once __DIR__."/../autoload.php";
4 19 require_once __DIR__."/EventSubscriber.php";
5 20

0 comments on commit e96a72a

Please sign in to comment.
Something went wrong with that request. Please try again.