|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace devtransition\rabbitmq\base; |
|
4
|
|
|
|
|
5
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
|
6
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
|
7
|
|
|
use PhpAmqpLib\Connection\AMQPSSLConnection; |
|
8
|
|
|
use yii\base\Component; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Connection represents an advanced instance of PhpAmqpLibs AMQPStreamConnection. |
|
13
|
|
|
* |
|
14
|
|
|
* Connection works together with Channels .... explain.. |
|
15
|
|
|
* |
|
16
|
|
|
* @property string $host RabbitMQ server hostname or IP address. Defaults to `localhost`. |
|
17
|
|
|
* @property integer $port TCP-Port where RabbitMQ is listening. Defaults to `5672`. |
|
18
|
|
|
* @property string $username RabbitMQ server username on connect. Defaults to `guest`. |
|
19
|
|
|
* @property string $password RabbitMQ server password on connect. Defaults to `guest`. |
|
20
|
|
|
* @property string $vhost RabbitMQ server virtual host to use. Defaults to `/`. |
|
21
|
|
|
* @property \PhpAmqpLib\Connection\AMQPStreamConnection $connection The PhpAmqpLib connection instance. This property is read-only. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Nicolas Wild <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Connection extends Component implements ConnectionInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var AMQPStreamConnection |
|
29
|
|
|
*/ |
|
30
|
|
|
public $connection; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
public $host = 'localhost'; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var integer |
|
37
|
|
|
*/ |
|
38
|
|
|
public $port = 5672; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
public $username = 'guest'; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
public $password = ''; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
public $vhost = '/'; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
public $options = []; |
|
55
|
|
|
/** |
|
56
|
|
|
* @var AMQPChannel[] |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $channels = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var AMQPStreamConnection |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $amqpConnection; |
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function init() |
|
68
|
|
|
{ |
|
69
|
|
|
parent::init(); |
|
70
|
|
|
$this->_parseOptions(); |
|
|
|
|
|
|
71
|
|
|
$this->open(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function open() |
|
75
|
|
|
{ |
|
76
|
|
|
/* TODO: Implement options appy and TLS */ |
|
77
|
|
|
|
|
78
|
|
|
$this->amqpConnection = new AMQPStreamConnection( |
|
79
|
|
|
$this->host, |
|
80
|
|
|
$this->port, |
|
81
|
|
|
$this->username, |
|
82
|
|
|
$this->password, |
|
83
|
|
|
$this->vhost, |
|
84
|
|
|
$insist = false, |
|
85
|
|
|
$login_method = 'AMQPLAIN', |
|
86
|
|
|
$login_response = null, |
|
87
|
|
|
$locale = 'en_US', |
|
88
|
|
|
$connection_timeout = 3, |
|
89
|
|
|
$read_write_timeout = 3, |
|
90
|
|
|
$context = null, |
|
91
|
|
|
$keepalive = false, |
|
92
|
|
|
$heartbeat = 0 |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function close() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->amqpConnection->close(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function reconnect() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->amqpConnection-> reconnect(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function isConnected() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->amqpConnection->isConnected(); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns low-level AMQP connection instance |
|
113
|
|
|
* |
|
114
|
|
|
* @return AMQPStreamConnection|AMQPSSLConnection |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getAmqpInstance() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->amqpConnection; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
private function _parseOptions() |
|
123
|
|
|
{ |
|
124
|
|
|
/* TODO: Implement */ |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: