Symfony2 Components - The Event Dispatcher

2,156 views

Published on

Published in: Internet, Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,156
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
28
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Symfony2 Components - The Event Dispatcher

  1. 1. Matt Agar Technical Director at August matt.agar@august.com.au
  2. 2. Symfony Components
  3. 3. “...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
  4. 4. 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
  5. 5. 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
  6. 6. Powerful
  7. 7. Sculpin, Symfony Full Stack, phpBB, Drupal, Thelia, phpspec, Silex, Behat, Guzzle, Laravel, phpDocumentor, Carew
  8. 8. Simple
  9. 9. Getting Started
  10. 10. // Clone directly from github.com/symfony/EventDispatcher $ git clone https://github.com/symfony/EventDispatcher.git
  11. 11. // Install using Composer $ curl -s http://getcomposer.org/installer | php $ php composer.phar require symfony/event-dispatcher
  12. 12. Events The Dispatcher Listeners
  13. 13. 1. Events
  14. 14. Instances of Symfony/Component/EventDispatcher/Event
  15. 15. Events have a unique name
  16. 16. kernel.request kernel.response form.bind form.post_set_data console.command console.exception
  17. 17. namespace AcmeStoreBundle; final class StoreEvents { /** * The store.order event is thrown each time an order is created * in the system. */ const STORE_ORDER = 'store.order'; }
  18. 18. No need for Event objects $dispatcher->dispatch('store.order');
  19. 19. namespace AcmeStoreBundleEvent; use SymfonyComponentEventDispatcherEvent; use AcmeStoreBundleOrder; class StoreOrderEvent extends Event { protected $order; public function __construct(Order $order) { $this->order = $order; } public function getOrder() { return $this->order; } }
  20. 20. $event->stopPropagation(); $event->isPropagationStopped();
  21. 21. Event Extras
  22. 22. SymfonyComponentEventDispatcher GenericEvent
  23. 23. use SymfonyComponentEventDispatcherGenericEvent; $storeOrderEvent = new GenericEvent( $order, array('a' => 'b', 'c' => 0) ); // Implements ArrayAccess and IteratorAggregate echo $event['a']; $order === $storeOrderEvent->getSubject();
  24. 24. 2. The Dispatcher
  25. 25. use SymfonyComponentEventDispatcherEventDispatcher; $dispatcher = new EventDispatcher();
  26. 26. Use the Single Instance
  27. 27. // access the dispatcher service in a controller $dispatcher = $this->container->get('event_dispatcher');
  28. 28. # app/config/config.yml services: event_generator: class: AcmeHelloBundleEventGenerator arguments: ["@event_dispatcher"]
  29. 29. namespace AcmeHelloBundle; use SymfonyComponentEventDispatcherEventDispatcherInterface; class EventGenerator { protected $dispatcher; public function __construct(EventDispatcherInterface $dispatcher) { $this->dispatcher = $dispatcher; } }
  30. 30. Dispatching events
  31. 31. // event name only $dispatcher->dispatch('store.order'); // create the StoreOrderEvent and dispatch it $event = new StoreOrderEvent($order); $dispatcher->dispatch(StoreEvents::STORE_ORDER, $event);
  32. 32. $a = $dispatcher->dispatch('store.event', $b); $a === $b; // true // returns the dispatcher created event $dispatcher->dispatch('foo.event')->isPropagationStopped()
  33. 33. Dispatcher Extras
  34. 34. SymfonyComponentEventDispatcher ImmutableEventDispatcher
  35. 35. SymfonyComponentEventDispatcher ContainerAwareEventDispatcher
  36. 36. SymfonyComponentEventDispatcher DebugTraceableEventDispatcher
  37. 37. 3. The Listeners
  38. 38. Basic Listeners
  39. 39. $dispatcher->addListener( 'event.name', $listener, // PHP callable $priority = 0 );
  40. 40. # app/config/config.yml services: event_listener: class: AcmeHelloBundleEventListener tags: - { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 0 }
  41. 41. Subscribers
  42. 42. // Implements EventSubscriberInterface $subscriber = new StoreSubscriber(); $dispatcher->addSubscriber($subscriber);
  43. 43. 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), ) ); } }
  44. 44. # app/config/config.yml services: event_listener: class: AcmeHelloBundleEventSubscriber tags: - { name: kernel.event_subscriber }
  45. 45. Listener Extras
  46. 46. // Event name and the dispatcher passed to listener public function onStoreOrder( Event $event, $eventName, EventDispatcherInterface $eventDispatcher )
  47. 47. What next?
  48. 48. Thank You! Questions?

×