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
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
22 |
|
} |
45
|
|
|
|
46
|
22 |
|
/** |
47
|
22 |
|
* Set $transports to delegate to. |
48
|
22 |
|
* |
49
|
|
|
* @param Swift_Transport[] $transports |
50
|
|
|
*/ |
51
|
|
|
public function setTransports(array $transports) |
52
|
|
|
{ |
53
|
|
|
$this->_transports = $transports; |
54
|
|
|
$this->_deadTransports = array(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get $transports to delegate to. |
59
|
|
|
* |
60
|
|
|
* @return Swift_Transport[] |
61
|
|
|
*/ |
62
|
|
|
public function getTransports() |
63
|
|
|
{ |
64
|
|
|
return array_merge($this->_transports, $this->_deadTransports); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the Transport used in the last successful send operation. |
69
|
|
|
* |
70
|
|
|
* @return Swift_Transport |
71
|
|
|
*/ |
72
|
|
|
public function getLastUsedTransport() |
73
|
|
|
{ |
74
|
|
|
return $this->_lastUsedTransport; |
75
|
4 |
|
} |
76
|
|
|
|
77
|
4 |
|
/** |
78
|
|
|
* Test if this Transport mechanism has started. |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isStarted() |
83
|
20 |
|
{ |
84
|
|
|
return count($this->_transports) > 0; |
85
|
20 |
|
} |
86
|
20 |
|
|
87
|
|
|
/** |
88
|
|
|
* Start this Transport mechanism. |
89
|
|
|
*/ |
90
|
|
|
public function start() |
91
|
2 |
|
{ |
92
|
|
|
$this->_transports = array_merge($this->_transports, $this->_deadTransports); |
93
|
2 |
|
} |
94
|
2 |
|
|
95
|
2 |
|
/** |
96
|
2 |
|
* Stop this Transport mechanism. |
97
|
|
|
*/ |
98
|
|
|
public function stop() |
99
|
|
|
{ |
100
|
|
|
foreach ($this->_transports as $transport) { |
101
|
|
|
$transport->stop(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Send the given Message. |
107
|
|
|
* |
108
|
|
|
* Recipient/sender data will be retrieved from the Message API. |
109
|
|
|
* The return value is the number of recipients who were accepted for delivery. |
110
|
|
|
* |
111
|
10 |
|
* @param Swift_Mime_Message $message |
112
|
|
|
* @param string[] $failedRecipients An array of failures by-reference |
113
|
10 |
|
* |
114
|
10 |
|
* @return int |
115
|
10 |
|
* |
116
|
|
|
* @throws Swift_TransportException |
117
|
10 |
|
*/ |
118
|
10 |
View Code Duplication |
public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
|
|
|
119
|
|
|
{ |
120
|
10 |
|
$maxTransports = count($this->_transports); |
121
|
10 |
|
$sent = 0; |
122
|
10 |
|
$this->_lastUsedTransport = null; |
123
|
|
|
|
124
|
10 |
|
for ($i = 0; $i < $maxTransports |
125
|
8 |
|
&& $transport = $this->_getNextTransport(); ++$i) { |
126
|
7 |
|
try { |
127
|
7 |
|
if (!$transport->isStarted()) { |
128
|
|
|
$transport->start(); |
129
|
|
|
} |
130
|
7 |
|
|
131
|
5 |
|
$sent = $transport->send($message, $failedRecipients); |
132
|
|
|
if ($sent) { |
133
|
7 |
|
$this->_lastUsedTransport = $transport; |
134
|
|
|
break; |
135
|
10 |
|
} |
136
|
3 |
|
|
137
|
|
|
} catch (Swift_TransportException $e) { |
138
|
3 |
|
$this->_killCurrentTransport(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
8 |
|
|
142
|
|
|
if (count($this->_transports) === 0) { |
143
|
|
|
throw new Swift_TransportException( |
144
|
|
|
'All Transports in LoadBalancedTransport failed, or no Transports available' |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $sent; |
149
|
2 |
|
} |
150
|
|
|
|
151
|
2 |
|
/** |
152
|
2 |
|
* Register a plugin. |
153
|
2 |
|
* |
154
|
2 |
|
* @param Swift_Events_EventListener $plugin |
155
|
|
|
*/ |
156
|
|
|
public function registerPlugin(Swift_Events_EventListener $plugin) |
157
|
|
|
{ |
158
|
|
|
foreach ($this->_transports as $transport) { |
159
|
|
|
$transport->registerPlugin($plugin); |
160
|
|
|
} |
161
|
18 |
|
} |
162
|
|
|
|
163
|
18 |
|
/** |
164
|
18 |
|
* Rotates the transport list around and returns the first instance. |
165
|
18 |
|
* |
166
|
18 |
|
* @return Swift_Transport |
167
|
|
|
*/ |
168
|
18 |
|
protected function _getNextTransport() |
169
|
|
|
{ |
170
|
|
|
$next = array_shift($this->_transports); |
171
|
|
|
if ($next) { |
172
|
|
|
$this->_transports[] = $next; |
173
|
|
|
} |
174
|
10 |
|
|
175
|
|
|
return $next; |
176
|
10 |
|
} |
177
|
10 |
|
|
178
|
|
|
/** |
179
|
|
|
* Tag the currently used (top of stack) transport as dead/useless. |
180
|
10 |
|
*/ |
181
|
10 |
|
protected function _killCurrentTransport() |
182
|
|
|
{ |
183
|
|
|
$transport = array_pop($this->_transports); |
184
|
10 |
|
if ($transport) { |
185
|
10 |
|
|
186
|
10 |
|
try { |
187
|
|
|
$transport->stop(); |
188
|
|
|
} catch (Exception $e) { |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->_deadTransports[] = $transport; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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.