SimpleEventDispatcher.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * The EventDispatcher which handles the event dispatching layer.
  11. *
  12. * @package Swift
  13. * @subpackage Events
  14. * @author Chris Corbyn
  15. */
  16. class Swift_Events_SimpleEventDispatcher implements Swift_Events_EventDispatcher
  17. {
  18. /** A map of event types to their associated listener types */
  19. private $_eventMap = array();
  20. /** Event listeners bound to this dispatcher */
  21. private $_listeners = array();
  22. /** Listeners queued to have an Event bubbled up the stack to them */
  23. private $_bubbleQueue = array();
  24. /**
  25. * Create a new EventDispatcher.
  26. */
  27. public function __construct()
  28. {
  29. $this->_eventMap = array(
  30. 'Swift_Events_CommandEvent' => 'Swift_Events_CommandListener',
  31. 'Swift_Events_ResponseEvent' => 'Swift_Events_ResponseListener',
  32. 'Swift_Events_SendEvent' => 'Swift_Events_SendListener',
  33. 'Swift_Events_TransportChangeEvent' => 'Swift_Events_TransportChangeListener',
  34. 'Swift_Events_TransportExceptionEvent' => 'Swift_Events_TransportExceptionListener'
  35. );
  36. }
  37. /**
  38. * Create a new SendEvent for $source and $message.
  39. *
  40. * @param Swift_Transport $source
  41. * @param Swift_Mime_Message
  42. * @return Swift_Events_SendEvent
  43. */
  44. public function createSendEvent(Swift_Transport $source,
  45. Swift_Mime_Message $message)
  46. {
  47. return new Swift_Events_SendEvent($source, $message);
  48. }
  49. /**
  50. * Create a new CommandEvent for $source and $command.
  51. *
  52. * @param Swift_Transport $source
  53. * @param string $command That will be executed
  54. * @param array $successCodes That are needed
  55. * @return Swift_Events_CommandEvent
  56. */
  57. public function createCommandEvent(Swift_Transport $source,
  58. $command, $successCodes = array())
  59. {
  60. return new Swift_Events_CommandEvent($source, $command, $successCodes);
  61. }
  62. /**
  63. * Create a new ResponseEvent for $source and $response.
  64. *
  65. * @param Swift_Transport $source
  66. * @param string $response
  67. * @param boolean $valid If the response is valid
  68. * @return Swift_Events_ResponseEvent
  69. */
  70. public function createResponseEvent(Swift_Transport $source,
  71. $response, $valid)
  72. {
  73. return new Swift_Events_ResponseEvent($source, $response, $valid);
  74. }
  75. /**
  76. * Create a new TransportChangeEvent for $source.
  77. *
  78. * @param Swift_Transport $source
  79. * @return Swift_Events_TransportChangeEvent
  80. */
  81. public function createTransportChangeEvent(Swift_Transport $source)
  82. {
  83. return new Swift_Events_TransportChangeEvent($source);
  84. }
  85. /**
  86. * Create a new TransportExceptionEvent for $source.
  87. *
  88. * @param Swift_Transport $source
  89. * @param Swift_TransportException $ex
  90. * @return Swift_Events_TransportExceptionEvent
  91. */
  92. public function createTransportExceptionEvent(Swift_Transport $source,
  93. Swift_TransportException $ex)
  94. {
  95. return new Swift_Events_TransportExceptionEvent($source, $ex);
  96. }
  97. /**
  98. * Bind an event listener to this dispatcher.
  99. *
  100. * @param Swift_Events_EventListener $listener
  101. */
  102. public function bindEventListener(Swift_Events_EventListener $listener)
  103. {
  104. foreach ($this->_listeners as $l)
  105. {
  106. //Already loaded
  107. if ($l === $listener)
  108. {
  109. return;
  110. }
  111. }
  112. $this->_listeners[] = $listener;
  113. }
  114. /**
  115. * Dispatch the given Event to all suitable listeners.
  116. *
  117. * @param Swift_Events_EventObject $evt
  118. * @param string $target method
  119. */
  120. public function dispatchEvent(Swift_Events_EventObject $evt, $target)
  121. {
  122. $this->_prepareBubbleQueue($evt);
  123. $this->_bubble($evt, $target);
  124. }
  125. // -- Private methods
  126. /** Queue listeners on a stack ready for $evt to be bubbled up it */
  127. private function _prepareBubbleQueue(Swift_Events_EventObject $evt)
  128. {
  129. $this->_bubbleQueue = array();
  130. $evtClass = get_class($evt);
  131. foreach ($this->_listeners as $listener)
  132. {
  133. if (array_key_exists($evtClass, $this->_eventMap)
  134. && ($listener instanceof $this->_eventMap[$evtClass]))
  135. {
  136. $this->_bubbleQueue[] = $listener;
  137. }
  138. }
  139. }
  140. /** Bubble $evt up the stack calling $target() on each listener */
  141. private function _bubble(Swift_Events_EventObject $evt, $target)
  142. {
  143. if (!$evt->bubbleCancelled() && $listener = array_shift($this->_bubbleQueue))
  144. {
  145. $listener->$target($evt);
  146. $this->_bubble($evt, $target);
  147. }
  148. }
  149. }