ContainerAwareEventDispatcher.php 7.3 KB

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