ContainerAwareEventDispatcher.php 7.3 KB

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