|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunnu\RabbitMQ; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
|
8
|
|
|
use PhpAmqpLib\Connection\AMQPSSLConnection; |
|
9
|
|
|
use Illuminate\Contracts\Container\Container; |
|
10
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
|
11
|
|
|
|
|
12
|
|
|
class RabbitMQManager |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Configuration key. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
const CONFIG_KEY = 'rabbitmq'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* IoC Container/Application. |
|
23
|
|
|
* |
|
24
|
|
|
* @var Container $app |
|
25
|
|
|
*/ |
|
26
|
|
|
protected Container $app; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Configuration repository. |
|
30
|
|
|
* |
|
31
|
|
|
* @var Repository $config |
|
32
|
|
|
*/ |
|
33
|
|
|
protected Repository $config; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Connection pool. |
|
37
|
|
|
* |
|
38
|
|
|
* @var Collection $connections |
|
39
|
|
|
*/ |
|
40
|
|
|
protected Collection $connections; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Channel pool. |
|
44
|
|
|
* |
|
45
|
|
|
* @var Collection $channels |
|
46
|
|
|
*/ |
|
47
|
|
|
protected Collection $channels; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create a new RabbitMQManager instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Container $app |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(Container $app) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->app = $app; |
|
57
|
|
|
$this->config = $this->app->get('config'); |
|
58
|
|
|
$this->connections = new Collection([]); |
|
59
|
|
|
$this->channels = new Collection([]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get Connections. |
|
64
|
|
|
* |
|
65
|
|
|
* @return Collection |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getConnections(): Collection |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->connections; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get Config. |
|
74
|
|
|
* |
|
75
|
|
|
* @return Repository |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getConfig(): Repository |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->config; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get Application Container. |
|
84
|
|
|
* |
|
85
|
|
|
* @return Container |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getApp(): Container |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->app; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get Channels. |
|
94
|
|
|
* |
|
95
|
|
|
* @return Collection |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getChannels(): Collection |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->channels; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Resolve default connection name. |
|
104
|
|
|
* |
|
105
|
|
|
* @return string|null |
|
106
|
|
|
*/ |
|
107
|
|
|
public function resolveDefaultConfigName(): ?string |
|
108
|
|
|
{ |
|
109
|
|
|
$configKey = self::CONFIG_KEY; |
|
110
|
|
|
return $this->config->get("{$configKey}.defaultConnection"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Resolve connection instance by name. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string|null $name |
|
117
|
|
|
* @param ConnectionConfig|null $config |
|
118
|
|
|
* @return AbstractConnection |
|
119
|
|
|
*/ |
|
120
|
|
|
public function resolveConnection(?string $name = null, ?ConnectionConfig $config = null): AbstractConnection |
|
121
|
|
|
{ |
|
122
|
|
|
$name = $name ?? $this->resolveDefaultConfigName(); |
|
123
|
|
|
|
|
124
|
|
|
if (!$this->connections->has($name)) { |
|
125
|
|
|
$this->connections->put( |
|
126
|
|
|
$name, |
|
127
|
|
|
$this->makeConnection($config ?? $this->resolveConfig($name)) |
|
|
|
|
|
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->connections->get($name); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Resolve connection configuration. |
|
136
|
|
|
* |
|
137
|
|
|
* @return ConnectionConfig |
|
138
|
|
|
*/ |
|
139
|
|
|
public function resolveConfig(string $connectionName): ConnectionConfig |
|
140
|
|
|
{ |
|
141
|
|
|
$configKey = self::CONFIG_KEY; |
|
142
|
|
|
$connectionKey = "{$configKey}.connections.{$connectionName}"; |
|
143
|
|
|
return new ConnectionConfig($this->config->get($connectionKey, [])); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the publisher. |
|
148
|
|
|
* |
|
149
|
|
|
* @return RabbitMQPublisher |
|
150
|
|
|
*/ |
|
151
|
|
|
public function publisher(): RabbitMQPublisher |
|
152
|
|
|
{ |
|
153
|
|
|
return new RabbitMQPublisher($this); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get the consumer. |
|
158
|
|
|
* |
|
159
|
|
|
* @return RabbitMQConsumer |
|
160
|
|
|
*/ |
|
161
|
|
|
public function consumer(): RabbitMQConsumer |
|
162
|
|
|
{ |
|
163
|
|
|
return new RabbitMQConsumer($this); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Resolve the channel ID. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string|null $channelId |
|
170
|
|
|
* @param string|null $connectionName |
|
171
|
|
|
* @return string|null |
|
172
|
|
|
*/ |
|
173
|
|
|
public function resolveChannelId(?string $channelId, ?string $connectionName): ?string |
|
|
|
|
|
|
174
|
|
|
{ |
|
175
|
|
|
$configKey = self::CONFIG_KEY; |
|
176
|
|
|
return $channelId ?? $this->config->get("{$configKey}.defaults.channel_id", $channelId); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Resolve channel for the given connection. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string|null $connectionName |
|
183
|
|
|
* @param string|null $channelId |
|
184
|
|
|
* @param AbstractConnection|null $connection |
|
185
|
|
|
* |
|
186
|
|
|
* @return AMQPChannel|null |
|
187
|
|
|
*/ |
|
188
|
|
|
public function resolveChannel( |
|
189
|
|
|
?string $connectionName = null, |
|
190
|
|
|
?string $channelId = null, |
|
191
|
|
|
?AbstractConnection $connection = null |
|
192
|
|
|
): AMQPChannel { |
|
193
|
|
|
if (!$connection) { |
|
194
|
|
|
$connection = $this->resolveConnection($connectionName); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if (!$this->channels->has("{$connectionName}.{$channelId}")) { |
|
198
|
|
|
$this->channels->put("{$connectionName}.{$channelId}", $connection->channel($channelId)); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $this->channels->get("{$connectionName}.{$channelId}"); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Create a new connection. |
|
206
|
|
|
* |
|
207
|
|
|
* @param ConnectionConfig $config |
|
208
|
|
|
* |
|
209
|
|
|
* @return AbstractConnection |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function makeConnection(ConnectionConfig $config): AbstractConnection |
|
212
|
|
|
{ |
|
213
|
|
|
return new AMQPSSLConnection( |
|
214
|
|
|
$config->getHost(), |
|
215
|
|
|
$config->getPort(), |
|
216
|
|
|
$config->getUser(), |
|
217
|
|
|
$config->getPassword(), |
|
218
|
|
|
$config->getVhost(), |
|
219
|
|
|
$config->getSSLOptions(), |
|
220
|
|
|
$config->getOptions(), |
|
221
|
|
|
$config->getSSLProtocol(), |
|
222
|
|
|
); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|