EventManager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common;
  20. /**
  21. * The EventManager is the central point of Doctrine's event listener system.
  22. * Listeners are registered on the manager and events are dispatched through the
  23. * manager.
  24. *
  25. * @link www.doctrine-project.org
  26. * @since 2.0
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. */
  31. class EventManager
  32. {
  33. /**
  34. * Map of registered listeners.
  35. * <event> => <listeners>
  36. *
  37. * @var array
  38. */
  39. private $_listeners = array();
  40. /**
  41. * Dispatches an event to all registered listeners.
  42. *
  43. * @param string $eventName The name of the event to dispatch. The name of the event is
  44. * the name of the method that is invoked on listeners.
  45. * @param EventArgs|null $eventArgs The event arguments to pass to the event handlers/listeners.
  46. * If not supplied, the single empty EventArgs instance is used.
  47. *
  48. * @return boolean
  49. */
  50. public function dispatchEvent($eventName, EventArgs $eventArgs = null)
  51. {
  52. if (isset($this->_listeners[$eventName])) {
  53. $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
  54. foreach ($this->_listeners[$eventName] as $listener) {
  55. $listener->$eventName($eventArgs);
  56. }
  57. }
  58. }
  59. /**
  60. * Gets the listeners of a specific event or all listeners.
  61. *
  62. * @param string|null $event The name of the event.
  63. *
  64. * @return array The event listeners for the specified event, or all event listeners.
  65. */
  66. public function getListeners($event = null)
  67. {
  68. return $event ? $this->_listeners[$event] : $this->_listeners;
  69. }
  70. /**
  71. * Checks whether an event has any registered listeners.
  72. *
  73. * @param string $event
  74. *
  75. * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
  76. */
  77. public function hasListeners($event)
  78. {
  79. return isset($this->_listeners[$event]) && $this->_listeners[$event];
  80. }
  81. /**
  82. * Adds an event listener that listens on the specified events.
  83. *
  84. * @param string|array $events The event(s) to listen on.
  85. * @param object $listener The listener object.
  86. *
  87. * @return void
  88. */
  89. public function addEventListener($events, $listener)
  90. {
  91. // Picks the hash code related to that listener
  92. $hash = spl_object_hash($listener);
  93. foreach ((array) $events as $event) {
  94. // Overrides listener if a previous one was associated already
  95. // Prevents duplicate listeners on same event (same instance only)
  96. $this->_listeners[$event][$hash] = $listener;
  97. }
  98. }
  99. /**
  100. * Removes an event listener from the specified events.
  101. *
  102. * @param string|array $events
  103. * @param object $listener
  104. *
  105. * @return void
  106. */
  107. public function removeEventListener($events, $listener)
  108. {
  109. // Picks the hash code related to that listener
  110. $hash = spl_object_hash($listener);
  111. foreach ((array) $events as $event) {
  112. // Check if actually have this listener associated
  113. if (isset($this->_listeners[$event][$hash])) {
  114. unset($this->_listeners[$event][$hash]);
  115. }
  116. }
  117. }
  118. /**
  119. * Adds an EventSubscriber. The subscriber is asked for all the events it is
  120. * interested in and added as a listener for these events.
  121. *
  122. * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
  123. *
  124. * @return void
  125. */
  126. public function addEventSubscriber(EventSubscriber $subscriber)
  127. {
  128. $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
  129. }
  130. /**
  131. * Removes an EventSubscriber. The subscriber is asked for all the events it is
  132. * interested in and removed as a listener for these events.
  133. *
  134. * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
  135. *
  136. * @return void
  137. */
  138. public function removeEventSubscriber(EventSubscriber $subscriber)
  139. {
  140. $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
  141. }
  142. }