symfony2 components - the event dispatcher

49

Upload: sarah-el-atm

Post on 08-May-2015

684 views

Category:

Internet


4 download

TRANSCRIPT

Page 1: Symfony2 Components - The Event Dispatcher
Page 2: Symfony2 Components - The Event Dispatcher

Matt AgarTechnical Director at August

[email protected]

Page 3: Symfony2 Components - The Event Dispatcher

SymfonyComponents

Page 4: Symfony2 Components - The Event Dispatcher

“...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

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

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

Powerful

Page 8: Symfony2 Components - The Event Dispatcher

Sculpin, Symfony Full Stack, phpBB, Drupal, Thelia, phpspec, Silex, Behat,

Guzzle, Laravel, phpDocumentor, Carew

Page 9: Symfony2 Components - The Event Dispatcher

Simple

Page 10: Symfony2 Components - The Event Dispatcher

Getting Started

Page 11: Symfony2 Components - The Event Dispatcher

// Clone directly from github.com/symfony/EventDispatcher

$ git clone https://github.com/symfony/EventDispatcher.git

Page 12: Symfony2 Components - The Event Dispatcher

// Install using Composer

$ curl -s http://getcomposer.org/installer | php

$ php composer.phar require symfony/event-dispatcher

Page 13: Symfony2 Components - The Event Dispatcher

EventsThe Dispatcher

Listeners

Page 14: Symfony2 Components - The Event Dispatcher

1. Events

Page 15: Symfony2 Components - The Event Dispatcher

Instances of

Symfony/Component/EventDispatcher/Event

Page 16: Symfony2 Components - The Event Dispatcher

Events have a unique name

Page 17: Symfony2 Components - The Event Dispatcher

kernel.requestkernel.response

form.bindform.post_set_dataconsole.command

console.exception

Page 18: Symfony2 Components - The Event Dispatcher

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

No need forEvent objects

$dispatcher->dispatch('store.order');

Page 20: Symfony2 Components - The Event Dispatcher

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

$event->stopPropagation();

$event->isPropagationStopped();

Page 22: Symfony2 Components - The Event Dispatcher

Event Extras

Page 23: Symfony2 Components - The Event Dispatcher

Symfony\Component\EventDispatcher\

GenericEvent

Page 24: Symfony2 Components - The Event Dispatcher

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

2. The Dispatcher

Page 26: Symfony2 Components - The Event Dispatcher

use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();

Page 27: Symfony2 Components - The Event Dispatcher

Use the Single Instance

Page 28: Symfony2 Components - The Event Dispatcher

// access the dispatcher service in a controller

$dispatcher = $this->container->get('event_dispatcher');

Page 29: Symfony2 Components - The Event Dispatcher

# app/config/config.yml

services:

event_generator:

class: Acme\HelloBundle\EventGenerator

arguments: ["@event_dispatcher"]

Page 30: Symfony2 Components - The Event Dispatcher

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

Dispatchingevents

Page 32: Symfony2 Components - The Event Dispatcher

// 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

$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

Dispatcher Extras

Page 35: Symfony2 Components - The Event Dispatcher

Symfony\Component\EventDispatcher\

ImmutableEventDispatcher

Page 36: Symfony2 Components - The Event Dispatcher

Symfony\Component\EventDispatcher\

ContainerAwareEventDispatcher

Page 37: Symfony2 Components - The Event Dispatcher

Symfony\Component\EventDispatcher\

Debug\TraceableEventDispatcher

Page 38: Symfony2 Components - The Event Dispatcher

3. The Listeners

Page 39: Symfony2 Components - The Event Dispatcher

Basic Listeners

Page 40: Symfony2 Components - The Event Dispatcher

$dispatcher->addListener(

'event.name',

$listener, // PHP callable

$priority = 0

);

Page 41: Symfony2 Components - The Event Dispatcher

# 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

Subscribers

Page 43: Symfony2 Components - The Event Dispatcher

// Implements EventSubscriberInterface

$subscriber = new StoreSubscriber();

$dispatcher->addSubscriber($subscriber);

Page 44: Symfony2 Components - The Event Dispatcher

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

# app/config/config.yml

services:

event_listener:

class: Acme\HelloBundle\EventSubscriber

tags:

- { name: kernel.event_subscriber }

Page 46: Symfony2 Components - The Event Dispatcher

Listener Extras

Page 47: Symfony2 Components - The Event Dispatcher

// Event name and the dispatcher passed to listener

public function onStoreOrder(

Event $event,

$eventName,

EventDispatcherInterface $eventDispatcher

)

Page 48: Symfony2 Components - The Event Dispatcher

What next?

Page 49: Symfony2 Components - The Event Dispatcher

Thank You!Questions?