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
|
|
|
* Contains a list of redundant Transports so when one fails, the next is used. |
13
|
|
|
* |
14
|
|
|
* @author Chris Corbyn |
15
|
|
|
*/ |
16
|
|
|
class Swift_Transport_FailoverTransport extends Swift_Transport_LoadBalancedTransport |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Registered transport currently used. |
20
|
|
|
* |
21
|
|
|
* @var Swift_Transport |
22
|
|
|
*/ |
23
|
|
|
private $_currentTransport; |
24
|
|
|
|
25
|
|
|
// needed as __construct is called from elsewhere explicitly |
26
|
11 |
|
public function __construct() |
27
|
|
|
{ |
28
|
11 |
|
parent::__construct(); |
29
|
11 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check if this Transport mechanism is alive. |
33
|
|
|
* |
34
|
|
|
* If a Transport mechanism session is no longer functional, the method |
35
|
|
|
* returns FALSE. It is the responsibility of the developer to handle this |
36
|
|
|
* case and restart the Transport mechanism manually. |
37
|
|
|
* |
38
|
|
|
* @example |
39
|
|
|
* |
40
|
|
|
* if (!$transport->ping()) { |
41
|
|
|
* $transport->stop(); |
42
|
|
|
* $transport->start(); |
43
|
|
|
* } |
44
|
|
|
* |
45
|
|
|
* The Transport mechanism will be started, if it is not already. |
46
|
|
|
* |
47
|
|
|
* It is undefined if the Transport mechanism attempts to restart as long as |
48
|
|
|
* the return value reflects whether the mechanism is now functional. |
49
|
|
|
* |
50
|
|
|
* @return bool TRUE if the transport is alive |
51
|
|
|
*/ |
52
|
|
|
public function ping() |
53
|
|
|
{ |
54
|
|
|
$maxTransports = count($this->transports); |
|
|
|
|
55
|
|
|
for ($i = 0; $i < $maxTransports |
56
|
|
|
&& $transport = $this->getNextTransport(); ++$i) { |
|
|
|
|
57
|
|
|
if ($transport->ping()) { |
58
|
|
|
return true; |
59
|
|
|
} else { |
60
|
|
|
$this->killCurrentTransport(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return count($this->transports) > 0; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Send the given Message. |
69
|
|
|
* |
70
|
|
|
* Recipient/sender data will be retrieved from the Message API. |
71
|
|
|
* The return value is the number of recipients who were accepted for delivery. |
72
|
|
|
* |
73
|
|
|
* @param Swift_Mime_Message $message |
74
|
|
|
* @param string[] $failedRecipients An array of failures by-reference |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
* @throws Swift_TransportException |
78
|
|
|
*/ |
79
|
8 |
View Code Duplication |
public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
|
|
|
80
|
|
|
{ |
81
|
8 |
|
$maxTransports = count($this->_transports); |
82
|
8 |
|
$sent = 0; |
83
|
8 |
|
$this->_lastUsedTransport = null; |
84
|
|
|
|
85
|
8 |
|
for ($i = 0; $i < $maxTransports |
86
|
8 |
|
&& $transport = $this->_getNextTransport(); ++$i) { |
87
|
|
|
try { |
88
|
8 |
|
if (!$transport->isStarted()) { |
89
|
8 |
|
$transport->start(); |
90
|
|
|
} |
91
|
|
|
|
92
|
8 |
|
$sent = $transport->send($message, $failedRecipients); |
93
|
6 |
|
if ($sent) { |
94
|
4 |
|
$this->_lastUsedTransport = $transport; |
95
|
|
|
|
96
|
6 |
|
return $sent; |
97
|
|
|
} |
98
|
|
|
|
99
|
5 |
|
} catch (Swift_TransportException $e) { |
100
|
5 |
|
$this->_killCurrentTransport(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
if (count($this->_transports) === 0) { |
105
|
3 |
|
throw new Swift_TransportException( |
106
|
3 |
|
'All Transports in FailoverTransport failed, or no Transports available' |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
return $sent; |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
|
protected function _getNextTransport() |
114
|
|
|
{ |
115
|
8 |
|
if (!isset($this->_currentTransport)) { |
116
|
8 |
|
$this->_currentTransport = parent::_getNextTransport(); |
117
|
|
|
} |
118
|
|
|
|
119
|
8 |
|
return $this->_currentTransport; |
120
|
|
|
} |
121
|
|
|
|
122
|
5 |
|
protected function _killCurrentTransport() |
123
|
|
|
{ |
124
|
5 |
|
$this->_currentTransport = null; |
125
|
5 |
|
parent::_killCurrentTransport(); |
126
|
5 |
|
} |
127
|
|
|
} |
128
|
|
|
|
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.