ContainerAwareEventDispatcher.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\EventDispatcher;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Lazily loads listeners and subscribers from the dependency injection
  14. * container
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. * @author Jordan Alliot <jordan.alliot@gmail.com>
  19. */
  20. class ContainerAwareEventDispatcher extends EventDispatcher
  21. {
  22. /**
  23. * The container from where services are loaded
  24. * @var ContainerInterface
  25. */
  26. private $container;
  27. /**
  28. * The service IDs of the event listeners and subscribers
  29. * @var array
  30. */
  31. private $listenerIds = array();
  32. /**
  33. * The services registered as listeners
  34. * @var array
  35. */
  36. private $listeners = array();
  37. /**
  38. * Constructor.
  39. *
  40. * @param ContainerInterface $container A ContainerInterface instance
  41. */
  42. public function __construct(ContainerInterface $container)
  43. {
  44. $this->container = $container;
  45. }
  46. /**
  47. * Adds a service as event listener
  48. *
  49. * @param string $eventName Event for which the listener is added
  50. * @param array $callback The service ID of the listener service & the method
  51. * name that has to be called
  52. * @param integer $priority The higher this value, the earlier an event listener
  53. * will be triggered in the chain.
  54. * Defaults to 0.
  55. *
  56. * @throws \InvalidArgumentException
  57. */
  58. public function addListenerService($eventName, $callback, $priority = 0)
  59. {
  60. if (!is_array($callback) || 2 !== count($callback)) {
  61. throw new \InvalidArgumentException('Expected an array("service", "method") argument');
  62. }
  63. $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
  64. }
  65. public function removeListener($eventName, $listener)
  66. {
  67. $this->lazyLoad($eventName);
  68. if (isset($this->listeners[$eventName])) {
  69. foreach ($this->listeners[$eventName] as $key => $l) {
  70. foreach ($this->listenerIds[$eventName] as $i => $args) {
  71. list($serviceId, $method, $priority) = $args;
  72. if ($key === $serviceId.'.'.$method) {
  73. if ($listener === array($l, $method)) {
  74. unset($this->listeners[$eventName][$key]);
  75. if (empty($this->listeners[$eventName])) {
  76. unset($this->listeners[$eventName]);
  77. }
  78. unset($this->listenerIds[$eventName][$i]);
  79. if (empty($this->listenerIds[$eventName])) {
  80. unset($this->listenerIds[$eventName]);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. parent::removeListener($eventName, $listener);
  88. }
  89. /**
  90. * @see EventDispatcherInterface::hasListeners
  91. */
  92. public function hasListeners($eventName = null)
  93. {
  94. if (null === $eventName) {
  95. return (Boolean) count($this->listenerIds) || (Boolean) count($this->listeners);
  96. }
  97. if (isset($this->listenerIds[$eventName])) {
  98. return true;
  99. }
  100. return parent::hasListeners($eventName);
  101. }
  102. /**
  103. * @see EventDispatcherInterface::getListeners
  104. */
  105. public function getListeners($eventName = null)
  106. {
  107. if (null === $eventName) {
  108. foreach (array_keys($this->listenerIds) as $serviceEventName) {
  109. $this->lazyLoad($serviceEventName);
  110. }
  111. } else {
  112. $this->lazyLoad($eventName);
  113. }
  114. return parent::getListeners($eventName);
  115. }
  116. /**
  117. * Adds a service as event subscriber
  118. *
  119. * @param string $serviceId The service ID of the subscriber service
  120. * @param string $class The service's class name (which must implement EventSubscriberInterface)
  121. */
  122. public function addSubscriberService($serviceId, $class)
  123. {
  124. foreach ($class::getSubscribedEvents() as $eventName => $params) {
  125. if (is_string($params)) {
  126. $this->listenerIds[$eventName][] = array($serviceId, $params, 0);
  127. } elseif (is_string($params[0])) {
  128. $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
  129. } else {
  130. foreach ($params as $listener) {
  131. $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * {@inheritDoc}
  138. *
  139. * Lazily loads listeners for this event from the dependency injection
  140. * container.
  141. *
  142. * @throws \InvalidArgumentException if the service is not defined
  143. */
  144. public function dispatch($eventName, Event $event = null)
  145. {
  146. $this->lazyLoad($eventName);
  147. return parent::dispatch($eventName, $event);
  148. }
  149. public function getContainer()
  150. {
  151. return $this->container;
  152. }
  153. /**
  154. * Lazily loads listeners for this event from the dependency injection
  155. * container.
  156. *
  157. * @param string $eventName The name of the event to dispatch. The name of
  158. * the event is the name of the method that is
  159. * invoked on listeners.
  160. */
  161. protected function lazyLoad($eventName)
  162. {
  163. if (isset($this->listenerIds[$eventName])) {
  164. foreach ($this->listenerIds[$eventName] as $args) {
  165. list($serviceId, $method, $priority) = $args;
  166. $listener = $this->container->get($serviceId);
  167. $key = $serviceId.'.'.$method;
  168. if (!isset($this->listeners[$eventName][$key])) {
  169. $this->addListener($eventName, array($listener, $method), $priority);
  170. } elseif ($listener !== $this->listeners[$eventName][$key]) {
  171. parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
  172. $this->addListener($eventName, array($listener, $method), $priority);
  173. }
  174. $this->listeners[$eventName][$key] = $listener;
  175. }
  176. }
  177. }
  178. }