ContainerAwareEventDispatcher.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Bundle\FrameworkBundle;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\EventDispatcher\EventDispatcher;
  13. use Symfony\Component\EventDispatcher\Event;
  14. /**
  15. * Lazily loads listeners and subscribers from the dependency injection
  16. * container
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  20. */
  21. class ContainerAwareEventDispatcher extends EventDispatcher
  22. {
  23. /**
  24. * The container from where services are loaded
  25. * @var ContainerInterface
  26. */
  27. private $container;
  28. /**
  29. * The service IDs of the event listeners and subscribers
  30. * @var array
  31. */
  32. private $listenerIds = array();
  33. /**
  34. * The services registered as listeners
  35. * @var array
  36. */
  37. private $listeners = array();
  38. /**
  39. * Constructor.
  40. *
  41. * @param ContainerInterface $container A ContainerInterface instance
  42. */
  43. public function __construct(ContainerInterface $container)
  44. {
  45. $this->container = $container;
  46. }
  47. /**
  48. * Adds a service as event listener
  49. *
  50. * @param string $eventName Event for which the listener is added
  51. * @param array $callback The service ID of the listener service & the method
  52. * name that has to be called
  53. * @param integer $priority The higher this value, the earlier an event listener
  54. * will be triggered in the chain.
  55. * Defaults to 0.
  56. */
  57. public function addListenerService($eventName, $callback, $priority = 0)
  58. {
  59. if (!is_array($callback) || 2 !== count($callback)) {
  60. throw new \InvalidArgumentException('Expected an array("service", "method") argument');
  61. }
  62. $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
  63. }
  64. /**
  65. * @see EventDispatcherInterface::hasListeners
  66. */
  67. public function hasListeners($eventName = null)
  68. {
  69. if (null === $eventName) {
  70. return (Boolean) count($this->listenerIds) || (Boolean) count($this->listeners);
  71. }
  72. if (isset($this->listenerIds[$eventName])) {
  73. return true;
  74. }
  75. return parent::hasListeners($eventName);
  76. }
  77. /**
  78. * @see EventDispatcherInterface::getListeners
  79. */
  80. public function getListeners($eventName = null)
  81. {
  82. if (null === $eventName) {
  83. foreach (array_keys($this->listenerIds) as $serviceEventName) {
  84. $this->lazyLoad($serviceEventName);
  85. }
  86. } else {
  87. $this->lazyLoad($eventName);
  88. }
  89. return parent::getListeners($eventName);
  90. }
  91. /**
  92. * {@inheritDoc}
  93. *
  94. * Lazily loads listeners for this event from the dependency injection
  95. * container.
  96. *
  97. * @throws \InvalidArgumentException if the service is not defined
  98. */
  99. public function dispatch($eventName, Event $event = null)
  100. {
  101. $this->lazyLoad($eventName);
  102. parent::dispatch($eventName, $event);
  103. }
  104. /**
  105. * Lazily loads listeners for this event from the dependency injection
  106. * container.
  107. *
  108. * @param string $eventName The name of the event to dispatch. The name of
  109. * the event is the name of the method that is
  110. * invoked on listeners.
  111. */
  112. protected function lazyLoad($eventName)
  113. {
  114. if (isset($this->listenerIds[$eventName])) {
  115. foreach ($this->listenerIds[$eventName] as $args) {
  116. list($serviceId, $method, $priority) = $args;
  117. $listener = $this->container->get($serviceId);
  118. $key = $serviceId.'.'.$method;
  119. if (!isset($this->listeners[$eventName][$key])) {
  120. $this->addListener($eventName, array($listener, $method), $priority);
  121. } elseif ($listener !== $this->listeners[$eventName][$key]) {
  122. $this->removeListener($eventName, array($this->listeners[$eventName][$key], $method));
  123. $this->addListener($eventName, array($listener, $method), $priority);
  124. }
  125. $this->listeners[$eventName][$key] = $listener;
  126. }
  127. }
  128. }
  129. }