Download - Symfony2 Components - The Event Dispatcher
![Page 1: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/1.jpg)
![Page 3: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/3.jpg)
SymfonyComponents
![Page 4: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/4.jpg)
“...the foundation of the Symfony full-stack framework, but they can also be used standalone even if you
don't use the framework...”
symfony.com/components
![Page 5: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/5.jpg)
BrowserKit, ClassLoader, Config, Console, CssSelector, Debug, DependencyInjection,
DomCrawler, EventDispatcher, ExpressionLanguage, Filesystem, Finder, Form, HttpFoundation, HttpKernel, Locale, Intl, Icu, OptionsResolver, Process, PropertyAccess,
Routing, Security, Serializer, Stopwatch, Templating, Translation, Validator, Yaml
![Page 6: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/6.jpg)
BrowserKit, ClassLoader, Config, Console, CssSelector, Debug, DependencyInjection,
DomCrawler, EventDispatcher, ExpressionLanguage, Filesystem, Finder, Form, HttpFoundation, HttpKernel, Locale, Intl, Icu, OptionsResolver, Process, PropertyAccess,
Routing, Security, Serializer, Stopwatch, Templating, Translation, Validator, Yaml
![Page 7: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/7.jpg)
Powerful
![Page 8: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/8.jpg)
Sculpin, Symfony Full Stack, phpBB, Drupal, Thelia, phpspec, Silex, Behat,
Guzzle, Laravel, phpDocumentor, Carew
![Page 9: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/9.jpg)
Simple
![Page 10: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/10.jpg)
Getting Started
![Page 11: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/11.jpg)
// Clone directly from github.com/symfony/EventDispatcher
$ git clone https://github.com/symfony/EventDispatcher.git
![Page 12: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/12.jpg)
// Install using Composer
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar require symfony/event-dispatcher
![Page 13: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/13.jpg)
EventsThe Dispatcher
Listeners
![Page 14: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/14.jpg)
1. Events
![Page 15: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/15.jpg)
Instances of
Symfony/Component/EventDispatcher/Event
![Page 16: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/16.jpg)
Events have a unique name
![Page 17: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/17.jpg)
kernel.requestkernel.response
form.bindform.post_set_dataconsole.command
console.exception
![Page 18: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/18.jpg)
namespace Acme\StoreBundle;
final class StoreEvents
{
/**
* The store.order event is thrown each time an order is created
* in the system.
*/
const STORE_ORDER = 'store.order';
}
![Page 19: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/19.jpg)
No need forEvent objects
$dispatcher->dispatch('store.order');
![Page 20: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/20.jpg)
namespace Acme\StoreBundle\Event;
use Symfony\Component\EventDispatcher\Event;
use Acme\StoreBundle\Order;
class StoreOrderEvent extends Event {
protected $order;
public function __construct(Order $order) {
$this->order = $order;
}
public function getOrder() {
return $this->order;
}
}
![Page 21: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/21.jpg)
$event->stopPropagation();
$event->isPropagationStopped();
![Page 22: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/22.jpg)
Event Extras
![Page 23: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/23.jpg)
Symfony\Component\EventDispatcher\
GenericEvent
![Page 24: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/24.jpg)
use Symfony\Component\EventDispatcher\GenericEvent;
$storeOrderEvent = new GenericEvent(
$order,
array('a' => 'b', 'c' => 0)
);
// Implements \ArrayAccess and \IteratorAggregate
echo $event['a'];
$order === $storeOrderEvent->getSubject();
![Page 25: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/25.jpg)
2. The Dispatcher
![Page 26: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/26.jpg)
use Symfony\Component\EventDispatcher\EventDispatcher;
$dispatcher = new EventDispatcher();
![Page 27: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/27.jpg)
Use the Single Instance
![Page 28: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/28.jpg)
// access the dispatcher service in a controller
$dispatcher = $this->container->get('event_dispatcher');
![Page 29: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/29.jpg)
# app/config/config.yml
services:
event_generator:
class: Acme\HelloBundle\EventGenerator
arguments: ["@event_dispatcher"]
![Page 30: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/30.jpg)
namespace Acme\HelloBundle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class EventGenerator
{
protected $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
}
![Page 31: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/31.jpg)
Dispatchingevents
![Page 32: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/32.jpg)
// event name only
$dispatcher->dispatch('store.order');
// create the StoreOrderEvent and dispatch it
$event = new StoreOrderEvent($order);
$dispatcher->dispatch(StoreEvents::STORE_ORDER, $event);
![Page 33: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/33.jpg)
$a = $dispatcher->dispatch('store.event', $b);
$a === $b; // true
// returns the dispatcher created event
$dispatcher->dispatch('foo.event')->isPropagationStopped()
![Page 34: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/34.jpg)
Dispatcher Extras
![Page 35: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/35.jpg)
Symfony\Component\EventDispatcher\
ImmutableEventDispatcher
![Page 36: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/36.jpg)
Symfony\Component\EventDispatcher\
ContainerAwareEventDispatcher
![Page 37: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/37.jpg)
Symfony\Component\EventDispatcher\
Debug\TraceableEventDispatcher
![Page 38: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/38.jpg)
3. The Listeners
![Page 39: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/39.jpg)
Basic Listeners
![Page 40: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/40.jpg)
$dispatcher->addListener(
'event.name',
$listener, // PHP callable
$priority = 0
);
![Page 41: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/41.jpg)
# app/config/config.yml
services:
event_listener:
class: Acme\HelloBundle\EventListener
tags:
- { name: kernel.event_listener,
event: kernel.exception,
method: onKernelException,
priority: 0 }
![Page 42: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/42.jpg)
Subscribers
![Page 43: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/43.jpg)
// Implements EventSubscriberInterface
$subscriber = new StoreSubscriber();
$dispatcher->addSubscriber($subscriber);
![Page 44: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/44.jpg)
class StoreSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return array(
'kernel.request' => 'onKernelRequest',
'store.order' => array('onStoreOrder', 5),
'kernel.response' => array(
array('onKernelResponsePre', 10),
array('onKernelResponsePost', 0),
)
);
}
}
![Page 45: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/45.jpg)
# app/config/config.yml
services:
event_listener:
class: Acme\HelloBundle\EventSubscriber
tags:
- { name: kernel.event_subscriber }
![Page 46: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/46.jpg)
Listener Extras
![Page 47: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/47.jpg)
// Event name and the dispatcher passed to listener
public function onStoreOrder(
Event $event,
$eventName,
EventDispatcherInterface $eventDispatcher
)
![Page 48: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/48.jpg)
What next?
![Page 49: Symfony2 Components - The Event Dispatcher](https://reader033.vdocuments.site/reader033/viewer/2022052822/554bb379b4c9053a298b45ef/html5/thumbnails/49.jpg)
Thank You!Questions?