EventManager.php 4.7 KB

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