EventManager.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. use Doctrine\Common\Events\Event;
  23. /**
  24. * The EventManager is the central point of Doctrine's event listener system.
  25. * Listeners are registered on the manager and events are dispatched through the
  26. * manager.
  27. *
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link www.doctrine-project.org
  30. * @since 2.0
  31. * @version $Revision: 3938 $
  32. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  33. * @author Jonathan Wage <jonwage@gmail.com>
  34. * @author Roman Borschel <roman@code-factory.org>
  35. */
  36. class EventManager
  37. {
  38. /**
  39. * Map of registered listeners.
  40. * <event> => <listeners>
  41. *
  42. * @var array
  43. */
  44. private $_listeners = array();
  45. /**
  46. * Dispatches an event to all registered listeners.
  47. *
  48. * @param string $eventName The name of the event to dispatch. The name of the event is
  49. * the name of the method that is invoked on listeners.
  50. * @param EventArgs $eventArgs The event arguments to pass to the event handlers/listeners.
  51. * If not supplied, the single empty EventArgs instance is used.
  52. * @return boolean
  53. */
  54. public function dispatchEvent($eventName, EventArgs $eventArgs = null)
  55. {
  56. if (isset($this->_listeners[$eventName])) {
  57. $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
  58. foreach ($this->_listeners[$eventName] as $listener) {
  59. $listener->$eventName($eventArgs);
  60. }
  61. }
  62. }
  63. /**
  64. * Gets the listeners of a specific event or all listeners.
  65. *
  66. * @param string $event The name of the event.
  67. * @return array The event listeners for the specified event, or all event listeners.
  68. */
  69. public function getListeners($event = null)
  70. {
  71. return $event ? $this->_listeners[$event] : $this->_listeners;
  72. }
  73. /**
  74. * Checks whether an event has any registered listeners.
  75. *
  76. * @param string $event
  77. * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
  78. */
  79. public function hasListeners($event)
  80. {
  81. return isset($this->_listeners[$event]) && $this->_listeners[$event];
  82. }
  83. /**
  84. * Adds an event listener that listens on the specified events.
  85. *
  86. * @param string|array $events The event(s) to listen on.
  87. * @param object $listener The listener object.
  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. public function removeEventListener($events, $listener)
  106. {
  107. // Picks the hash code related to that listener
  108. $hash = spl_object_hash($listener);
  109. foreach ((array) $events as $event) {
  110. // Check if actually have this listener associated
  111. if (isset($this->_listeners[$event][$hash])) {
  112. unset($this->_listeners[$event][$hash]);
  113. }
  114. }
  115. }
  116. /**
  117. * Adds an EventSubscriber. The subscriber is asked for all the events he is
  118. * interested in and added as a listener for these events.
  119. *
  120. * @param Doctrine\Common\EventSubscriber $subscriber The subscriber.
  121. */
  122. public function addEventSubscriber(EventSubscriber $subscriber)
  123. {
  124. $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
  125. }
  126. }