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