ContainerAwareEventDispatcher.php 7.0 KB

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