|
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
|
|
|
/** |
|
12
|
|
|
* Redundantly and rotationally uses several Transports when sending. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Chris Corbyn |
|
15
|
|
|
*/ |
|
16
|
|
|
class Swift_Transport_LoadBalancedTransport implements Swift_Transport |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Transports which are deemed useless. |
|
20
|
|
|
* |
|
21
|
|
|
* @var Swift_Transport[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $_deadTransports = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The Transports which are used in rotation. |
|
27
|
|
|
* |
|
28
|
|
|
* @var Swift_Transport[] |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_transports = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The Transport used in the last successful send operation. |
|
34
|
|
|
* |
|
35
|
|
|
* @var Swift_Transport |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $_lastUsedTransport; |
|
38
|
|
|
|
|
39
|
|
|
// needed as __construct is called from elsewhere explicitly |
|
40
|
23 |
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
23 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set $transports to delegate to. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Swift_Transport[] $transports |
|
48
|
|
|
*/ |
|
49
|
22 |
|
public function setTransports(array $transports) |
|
50
|
|
|
{ |
|
51
|
22 |
|
$this->_transports = $transports; |
|
52
|
22 |
|
$this->_deadTransports = array(); |
|
53
|
22 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get $transports to delegate to. |
|
57
|
|
|
* |
|
58
|
|
|
* @return Swift_Transport[] |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getTransports() |
|
61
|
|
|
{ |
|
62
|
|
|
return array_merge($this->_transports, $this->_deadTransports); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the Transport used in the last successful send operation. |
|
67
|
|
|
* |
|
68
|
|
|
* @return Swift_Transport |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getLastUsedTransport() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->_lastUsedTransport; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Test if this Transport mechanism has started. |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
4 |
|
public function isStarted() |
|
81
|
|
|
{ |
|
82
|
4 |
|
return count($this->_transports) > 0; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Start this Transport mechanism. |
|
87
|
|
|
*/ |
|
88
|
20 |
|
public function start() |
|
89
|
|
|
{ |
|
90
|
20 |
|
$this->_transports = array_merge($this->_transports, $this->_deadTransports); |
|
91
|
20 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Stop this Transport mechanism. |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function stop() |
|
97
|
|
|
{ |
|
98
|
2 |
|
foreach ($this->_transports as $transport) { |
|
99
|
2 |
|
$transport->stop(); |
|
100
|
|
|
} |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Check if this Transport mechanism is alive. |
|
105
|
|
|
* |
|
106
|
|
|
* If a Transport mechanism session is no longer functional, the method |
|
107
|
|
|
* returns FALSE. It is the responsibility of the developer to handle this |
|
108
|
|
|
* case and restart the Transport mechanism manually. |
|
109
|
|
|
* |
|
110
|
|
|
* @example |
|
111
|
|
|
* |
|
112
|
|
|
* if (!$transport->ping()) { |
|
113
|
|
|
* $transport->stop(); |
|
114
|
|
|
* $transport->start(); |
|
115
|
|
|
* } |
|
116
|
|
|
* |
|
117
|
|
|
* The Transport mechanism will be started, if it is not already. |
|
118
|
|
|
* |
|
119
|
|
|
* It is undefined if the Transport mechanism attempts to restart as long as |
|
120
|
|
|
* the return value reflects whether the mechanism is now functional. |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool TRUE if the transport is alive |
|
123
|
|
|
*/ |
|
124
|
|
|
public function ping() |
|
125
|
|
|
{ |
|
126
|
|
|
foreach ($this->transports as $transport) { |
|
|
|
|
|
|
127
|
|
|
if (!$transport->ping()) { |
|
128
|
|
|
$this->killCurrentTransport(); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return count($this->transports) > 0; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Send the given Message. |
|
137
|
|
|
* |
|
138
|
|
|
* Recipient/sender data will be retrieved from the Message API. |
|
139
|
|
|
* The return value is the number of recipients who were accepted for delivery. |
|
140
|
|
|
* |
|
141
|
|
|
* @param Swift_Mime_Message $message |
|
142
|
|
|
* @param string[] $failedRecipients An array of failures by-reference |
|
143
|
|
|
* |
|
144
|
|
|
* @return int |
|
145
|
|
|
* |
|
146
|
|
|
* @throws Swift_TransportException |
|
147
|
|
|
*/ |
|
148
|
10 |
View Code Duplication |
public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
10 |
|
$maxTransports = count($this->_transports); |
|
151
|
10 |
|
$sent = 0; |
|
152
|
10 |
|
$this->_lastUsedTransport = null; |
|
153
|
|
|
|
|
154
|
10 |
|
for ($i = 0; $i < $maxTransports |
|
155
|
10 |
|
&& $transport = $this->_getNextTransport(); ++$i) { |
|
156
|
|
|
try { |
|
157
|
10 |
|
if (!$transport->isStarted()) { |
|
158
|
10 |
|
$transport->start(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
10 |
|
$sent = $transport->send($message, $failedRecipients); |
|
162
|
8 |
|
if ($sent) { |
|
163
|
7 |
|
$this->_lastUsedTransport = $transport; |
|
164
|
8 |
|
break; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
5 |
|
} catch (Swift_TransportException $e) { |
|
168
|
5 |
|
$this->_killCurrentTransport(); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
10 |
|
if (count($this->_transports) === 0) { |
|
173
|
3 |
|
throw new Swift_TransportException( |
|
174
|
3 |
|
'All Transports in LoadBalancedTransport failed, or no Transports available' |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
8 |
|
return $sent; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Register a plugin. |
|
183
|
|
|
* |
|
184
|
|
|
* @param Swift_Events_EventListener $plugin |
|
185
|
|
|
*/ |
|
186
|
2 |
|
public function registerPlugin(Swift_Events_EventListener $plugin) |
|
187
|
|
|
{ |
|
188
|
2 |
|
foreach ($this->_transports as $transport) { |
|
189
|
2 |
|
$transport->registerPlugin($plugin); |
|
190
|
|
|
} |
|
191
|
2 |
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Rotates the transport list around and returns the first instance. |
|
195
|
|
|
* |
|
196
|
|
|
* @return Swift_Transport |
|
197
|
|
|
*/ |
|
198
|
18 |
|
protected function _getNextTransport() |
|
199
|
|
|
{ |
|
200
|
18 |
|
$next = array_shift($this->_transports); |
|
201
|
18 |
|
if ($next) { |
|
202
|
18 |
|
$this->_transports[] = $next; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
18 |
|
return $next; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Tag the currently used (top of stack) transport as dead/useless. |
|
210
|
|
|
*/ |
|
211
|
10 |
|
protected function _killCurrentTransport() |
|
212
|
|
|
{ |
|
213
|
10 |
|
$transport = array_pop($this->_transports); |
|
214
|
10 |
|
if ($transport) { |
|
215
|
|
|
|
|
216
|
|
|
try { |
|
217
|
10 |
|
$transport->stop(); |
|
218
|
10 |
|
} catch (Exception $e) { |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
10 |
|
$this->_deadTransports[] = $transport; |
|
222
|
|
|
} |
|
223
|
10 |
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.