説明なし

ContainerAwareEventDispatcher.php 6.1KB

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