EventManager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Doctrine\Common;
  3. use function spl_object_hash;
  4. /**
  5. * The EventManager is the central point of Doctrine's event listener system.
  6. * Listeners are registered on the manager and events are dispatched through the
  7. * manager.
  8. */
  9. class EventManager
  10. {
  11. /**
  12. * Map of registered listeners.
  13. * <event> => <listeners>
  14. *
  15. * @var object[][]
  16. */
  17. private $_listeners = [];
  18. /**
  19. * Dispatches an event to all registered listeners.
  20. *
  21. * @param string $eventName The name of the event to dispatch. The name of the event is
  22. * the name of the method that is invoked on listeners.
  23. * @param EventArgs|null $eventArgs The event arguments to pass to the event handlers/listeners.
  24. * If not supplied, the single empty EventArgs instance is used.
  25. *
  26. * @return void
  27. */
  28. public function dispatchEvent($eventName, ?EventArgs $eventArgs = null)
  29. {
  30. if (! isset($this->_listeners[$eventName])) {
  31. return;
  32. }
  33. $eventArgs = $eventArgs ?? EventArgs::getEmptyInstance();
  34. foreach ($this->_listeners[$eventName] as $listener) {
  35. $listener->$eventName($eventArgs);
  36. }
  37. }
  38. /**
  39. * Gets the listeners of a specific event or all listeners.
  40. *
  41. * @param string|null $event The name of the event.
  42. *
  43. * @return object[]|object[][] The event listeners for the specified event, or all event listeners.
  44. */
  45. public function getListeners($event = null)
  46. {
  47. return $event ? $this->_listeners[$event] : $this->_listeners;
  48. }
  49. /**
  50. * Checks whether an event has any registered listeners.
  51. *
  52. * @param string $event
  53. *
  54. * @return bool TRUE if the specified event has any listeners, FALSE otherwise.
  55. */
  56. public function hasListeners($event)
  57. {
  58. return ! empty($this->_listeners[$event]);
  59. }
  60. /**
  61. * Adds an event listener that listens on the specified events.
  62. *
  63. * @param string|string[] $events The event(s) to listen on.
  64. * @param object $listener The listener object.
  65. *
  66. * @return void
  67. */
  68. public function addEventListener($events, $listener)
  69. {
  70. // Picks the hash code related to that listener
  71. $hash = spl_object_hash($listener);
  72. foreach ((array) $events as $event) {
  73. // Overrides listener if a previous one was associated already
  74. // Prevents duplicate listeners on same event (same instance only)
  75. $this->_listeners[$event][$hash] = $listener;
  76. }
  77. }
  78. /**
  79. * Removes an event listener from the specified events.
  80. *
  81. * @param string|string[] $events
  82. * @param object $listener
  83. *
  84. * @return void
  85. */
  86. public function removeEventListener($events, $listener)
  87. {
  88. // Picks the hash code related to that listener
  89. $hash = spl_object_hash($listener);
  90. foreach ((array) $events as $event) {
  91. unset($this->_listeners[$event][$hash]);
  92. }
  93. }
  94. /**
  95. * Adds an EventSubscriber. The subscriber is asked for all the events it is
  96. * interested in and added as a listener for these events.
  97. *
  98. * @param EventSubscriber $subscriber The subscriber.
  99. *
  100. * @return void
  101. */
  102. public function addEventSubscriber(EventSubscriber $subscriber)
  103. {
  104. $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
  105. }
  106. /**
  107. * Removes an EventSubscriber. The subscriber is asked for all the events it is
  108. * interested in and removed as a listener for these events.
  109. *
  110. * @param EventSubscriber $subscriber The subscriber.
  111. *
  112. * @return void
  113. */
  114. public function removeEventSubscriber(EventSubscriber $subscriber)
  115. {
  116. $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
  117. }
  118. }