1
|
|
|
<?php |
2
|
|
|
namespace NeedleProject\LaravelRabbitMq; |
3
|
|
|
|
4
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
5
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
6
|
|
|
use PhpAmqpLib\Connection\AMQPSocketConnection; |
7
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AMQPConnection |
11
|
|
|
* |
12
|
|
|
* @package NeedleProject\LaravelRabbitMq |
13
|
|
|
* @author Adrian Tilita <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class AMQPConnection |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @const array Default connections parameters |
19
|
|
|
*/ |
20
|
|
|
const DEFAULTS = [ |
21
|
|
|
'hostname' => '127.0.0.1', |
22
|
|
|
'port' => 5672, |
23
|
|
|
'username' => 'guest', |
24
|
|
|
'password' => 'guest', |
25
|
|
|
'vhost' => '/', |
26
|
|
|
|
27
|
|
|
# whether the connection should be lazy |
28
|
|
|
'lazy' => true, |
29
|
|
|
|
30
|
|
|
# More info about timeouts can be found on https://www.rabbitmq.com/networking.html |
31
|
|
|
'read_write_timeout' => 3, // default timeout for writing/reading (in seconds) |
32
|
|
|
'connect_timeout' => 3, |
33
|
|
|
'heartbeat' => 0, |
34
|
|
|
'keep_alive' => false |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $connectionDetails = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $aliasName = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var null|AbstractConnection |
49
|
|
|
*/ |
50
|
|
|
private $connection = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var null|AMQPChannel |
54
|
|
|
*/ |
55
|
|
|
private $channel = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $aliasName |
59
|
|
|
* @param array $connectionDetails |
60
|
|
|
* @return AMQPConnection |
61
|
|
|
*/ |
62
|
10 |
|
public static function createConnection(string $aliasName, array $connectionDetails) |
63
|
|
|
{ |
64
|
10 |
|
if ($diff = array_diff(array_keys($connectionDetails), array_keys(self::DEFAULTS))) { |
65
|
1 |
|
throw new \InvalidArgumentException( |
66
|
|
|
sprintf( |
67
|
1 |
|
"Cannot create connection %s, received unknown arguments: %s!", |
68
|
1 |
|
(string)$aliasName, |
69
|
1 |
|
implode(', ', $diff) |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
9 |
|
return new static( |
75
|
|
|
$aliasName, |
76
|
9 |
|
array_merge(self::DEFAULTS, $connectionDetails) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* AMQPConnection constructor. |
82
|
|
|
* |
83
|
|
|
* @param string $aliasName |
84
|
|
|
* @param array $connectionDetails |
85
|
|
|
*/ |
86
|
11 |
|
public function __construct(string $aliasName, array $connectionDetails = []) |
87
|
|
|
{ |
88
|
11 |
|
$this->aliasName = $aliasName; |
89
|
11 |
|
$this->connectionDetails = $connectionDetails; |
90
|
11 |
|
if (isset($connectionDetails['lazy']) && $connectionDetails['lazy'] === false) { |
91
|
|
|
// dummy call |
92
|
1 |
|
$this->getConnection(); |
93
|
|
|
} |
94
|
11 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return AbstractConnection |
98
|
|
|
*/ |
99
|
|
|
protected function getConnection(): AbstractConnection |
100
|
|
|
{ |
101
|
|
|
if (is_null($this->connection)) { |
102
|
|
|
if (!isset($this->connection['type'])) { |
103
|
|
|
$this->connection['type'] = AMQPStreamConnection::class; |
104
|
|
|
} |
105
|
|
|
switch ($this->connection['type']) { |
106
|
|
|
case AMQPStreamConnection::class: |
107
|
|
|
case 'stream': |
108
|
|
|
$type = AMQPStreamConnection::class; |
109
|
|
|
break; |
110
|
|
|
default: |
111
|
|
|
$type = AMQPSocketConnection::class; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->connection = $this->createConnectionByType($type); |
115
|
|
|
} |
116
|
|
|
return $this->connection; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $type |
121
|
|
|
* @return mixed |
|
|
|
|
122
|
|
|
*/ |
123
|
|
|
private function createConnectionByType($type) |
124
|
|
|
{ |
125
|
|
|
return new $type( |
126
|
|
|
$this->connectionDetails['hostname'], |
127
|
|
|
$this->connectionDetails['port'], |
128
|
|
|
$this->connectionDetails['username'], |
129
|
|
|
$this->connectionDetails['password'], |
130
|
|
|
$this->connectionDetails['vhost'], |
131
|
|
|
/** insist */ |
132
|
|
|
false, |
133
|
|
|
/** login method */ |
134
|
|
|
'AMQPLAIN', |
135
|
|
|
/** login_response */ |
136
|
|
|
null, |
137
|
|
|
/** locale */ |
138
|
|
|
'en_US', |
139
|
|
|
$this->connectionDetails['connect_timeout'], |
140
|
|
|
$this->connectionDetails['read_write_timeout'], |
141
|
|
|
null, |
142
|
|
|
$this->connectionDetails['keep_alive'], |
143
|
|
|
$this->connectionDetails['heartbeat'] |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Reconnect |
149
|
|
|
*/ |
150
|
1 |
|
public function reconnect() |
151
|
|
|
{ |
152
|
1 |
|
$this->getConnection()->channel()->close(); |
153
|
1 |
|
$this->channel = null; |
154
|
1 |
|
$this->getConnection()->reconnect(); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return \PhpAmqpLib\Channel\AMQPChannel |
159
|
|
|
*/ |
160
|
1 |
|
public function getChannel() |
161
|
|
|
{ |
162
|
1 |
|
if (is_null($this->channel)) { |
163
|
1 |
|
$this->channel = $this->getConnection()->channel(); |
164
|
|
|
} |
165
|
1 |
|
return $this->channel; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Retrieve the connection alias name |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
1 |
|
public function getAliasName(): string |
174
|
|
|
{ |
175
|
1 |
|
return $this->aliasName; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.