AbstractHasDispatcher.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Guzzle\Common;
  3. use Symfony\Component\EventDispatcher\EventDispatcher;
  4. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. /**
  7. * Class that holds an event dispatcher
  8. */
  9. class AbstractHasDispatcher implements HasDispatcherInterface
  10. {
  11. /** @var EventDispatcherInterface */
  12. protected $eventDispatcher;
  13. public static function getAllEvents()
  14. {
  15. return array();
  16. }
  17. public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
  18. {
  19. $this->eventDispatcher = $eventDispatcher;
  20. return $this;
  21. }
  22. public function getEventDispatcher()
  23. {
  24. if (!$this->eventDispatcher) {
  25. $this->eventDispatcher = new EventDispatcher();
  26. }
  27. return $this->eventDispatcher;
  28. }
  29. public function dispatch($eventName, array $context = array())
  30. {
  31. return $this->getEventDispatcher()->dispatch($eventName, new Event($context));
  32. }
  33. public function addSubscriber(EventSubscriberInterface $subscriber)
  34. {
  35. $this->getEventDispatcher()->addSubscriber($subscriber);
  36. return $this;
  37. }
  38. }