ContainerAwareEventDispatcher.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. private $container;
  23. /**
  24. * The service IDs of the event listeners and subscribers.
  25. */
  26. private $listenerIds = array();
  27. /**
  28. * The services registered as listeners.
  29. */
  30. private $listeners = array();
  31. public function __construct(ContainerInterface $container)
  32. {
  33. $this->container = $container;
  34. }
  35. /**
  36. * Adds a service as event listener.
  37. *
  38. * @param string $eventName Event for which the listener is added
  39. * @param array $callback The service ID of the listener service & the method
  40. * name that has to be called
  41. * @param int $priority The higher this value, the earlier an event listener
  42. * will be triggered in the chain.
  43. * Defaults to 0.
  44. *
  45. * @throws \InvalidArgumentException
  46. */
  47. public function addListenerService($eventName, $callback, $priority = 0)
  48. {
  49. if (!is_array($callback) || 2 !== count($callback)) {
  50. throw new \InvalidArgumentException('Expected an array("service", "method") argument');
  51. }
  52. $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
  53. }
  54. public function removeListener($eventName, $listener)
  55. {
  56. $this->lazyLoad($eventName);
  57. if (isset($this->listenerIds[$eventName])) {
  58. foreach ($this->listenerIds[$eventName] as $i => $args) {
  59. list($serviceId, $method) = $args;
  60. $key = $serviceId.'.'.$method;
  61. if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
  62. unset($this->listeners[$eventName][$key]);
  63. if (empty($this->listeners[$eventName])) {
  64. unset($this->listeners[$eventName]);
  65. }
  66. unset($this->listenerIds[$eventName][$i]);
  67. if (empty($this->listenerIds[$eventName])) {
  68. unset($this->listenerIds[$eventName]);
  69. }
  70. }
  71. }
  72. }
  73. parent::removeListener($eventName, $listener);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function hasListeners($eventName = null)
  79. {
  80. if (null === $eventName) {
  81. return $this->listenerIds || $this->listeners || parent::hasListeners();
  82. }
  83. if (isset($this->listenerIds[$eventName])) {
  84. return true;
  85. }
  86. return parent::hasListeners($eventName);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getListeners($eventName = null)
  92. {
  93. if (null === $eventName) {
  94. foreach ($this->listenerIds as $serviceEventName => $args) {
  95. $this->lazyLoad($serviceEventName);
  96. }
  97. } else {
  98. $this->lazyLoad($eventName);
  99. }
  100. return parent::getListeners($eventName);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getListenerPriority($eventName, $listener)
  106. {
  107. $this->lazyLoad($eventName);
  108. return parent::getListenerPriority($eventName, $listener);
  109. }
  110. /**
  111. * Adds a service as event subscriber.
  112. *
  113. * @param string $serviceId The service ID of the subscriber service
  114. * @param string $class The service's class name (which must implement EventSubscriberInterface)
  115. */
  116. public function addSubscriberService($serviceId, $class)
  117. {
  118. foreach ($class::getSubscribedEvents() as $eventName => $params) {
  119. if (is_string($params)) {
  120. $this->listenerIds[$eventName][] = array($serviceId, $params, 0);
  121. } elseif (is_string($params[0])) {
  122. $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0);
  123. } else {
  124. foreach ($params as $listener) {
  125. $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0);
  126. }
  127. }
  128. }
  129. }
  130. public function getContainer()
  131. {
  132. return $this->container;
  133. }
  134. /**
  135. * Lazily loads listeners for this event from the dependency injection
  136. * container.
  137. *
  138. * @param string $eventName The name of the event to dispatch. The name of
  139. * the event is the name of the method that is
  140. * invoked on listeners.
  141. */
  142. protected function lazyLoad($eventName)
  143. {
  144. if (isset($this->listenerIds[$eventName])) {
  145. foreach ($this->listenerIds[$eventName] as $args) {
  146. list($serviceId, $method, $priority) = $args;
  147. $listener = $this->container->get($serviceId);
  148. $key = $serviceId.'.'.$method;
  149. if (!isset($this->listeners[$eventName][$key])) {
  150. $this->addListener($eventName, array($listener, $method), $priority);
  151. } elseif ($this->listeners[$eventName][$key] !== $listener) {
  152. parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method));
  153. $this->addListener($eventName, array($listener, $method), $priority);
  154. }
  155. $this->listeners[$eventName][$key] = $listener;
  156. }
  157. }
  158. }
  159. }