-
Notifications
You must be signed in to change notification settings - Fork 6
/
ConnectionConfig.php
432 lines (381 loc) · 12.3 KB
/
ConnectionConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
declare(strict_types=1);
namespace Enqueue\AmqpTools;
use Enqueue\Dsn\Dsn;
/**
* The config could be an array, string DSN or null. In case of null it will attempt to connect to localhost with default credentials.
*
* 1. The config could be an array with next options:
* host - The host to connect too. Note: Max 1024 characters
* port - Port on the host
* vhost - The virtual host on the host. Note: Max 128 characters
* user - The user name to use. Note: Max 128 characters
* pass - Password. Note: Max 128 characters
* read_timeout - Timeout in for income activity. Note: 0 or greater seconds. May be fractional
* write_timeout - Timeout in for outcome activity. Note: 0 or greater seconds. May be fractional
* connection_timeout - Connection timeout. Note: 0 or greater seconds. May be fractional
* heartbeat - how often to send heartbeat. 0 means off
* persisted - bool, Whether it use single persisted connection or open a new one for every context
* lazy - the connection will be performed as later as possible, if the option set to true
* qos_prefetch_size - The server will send a message in advance if it is equal to or smaller in size than the available prefetch size. May be set to zero, meaning "no specific limit"
* qos_prefetch_count - Specifies a prefetch window in terms of whole messages
* qos_global - If "false" the QoS settings apply to the current channel only. If this field is "true", they are applied to the entire connection.
* ssl_on - Should be true if you want to use secure connections. False by default
* ssl_verify - This option determines whether ssl client verifies that the server cert is for the server it is known as. True by default.
* ssl_cacert - Location of Certificate Authority file on local filesystem which should be used with the verify_peer context option to authenticate the identity of the remote peer. A string.
* ssl_cert - Path to local certificate file on filesystem. It must be a PEM encoded file which contains your certificate and private key. A string
* ssl_key - Path to local private key file on filesystem in case of separate files for certificate (local_cert) and private key. A string.
* ssl_passphrase - Passphrase with which your local_cert file was encoded. A string
*
* 2. null - in this case it tries to connect to localhost with default settings
* 3. amqp: same as 2.
* 4. amqp://user:pass@host:10000/vhost?lazy=true&persisted=false&read_timeout=2
* 5. amqp+foo: - the scheme driver could be used. (make sure you added it to the list of supported schemes)
* 6. amqps: - secure connection to localhost
* 7. amqp+tls: - secure connection
* 8. amqp+rabbitmq: - if you connect to RabbitMQ server
*
* @see https://www.rabbitmq.com/uri-spec.html
*/
class ConnectionConfig
{
/**
* @var array
*/
private $config;
/**
* @var array|string|null
*/
private $inputConfig;
/**
* @var array
*/
private $defaultConfig;
/**
* @var string[]
*/
private $supportedSchemes;
/**
* @var array
*/
private $schemeExtensions = [];
/**
* @param array|string|null $config
*/
public function __construct($config = null)
{
$this->inputConfig = $config;
$this->supportedSchemes = [];
$this->defaultConfig = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
'vhost' => '/',
'read_timeout' => 3.,
'write_timeout' => 3.,
'connection_timeout' => 3.,
'heartbeat' => 0,
'persisted' => false,
'lazy' => true,
'qos_global' => false,
'qos_prefetch_size' => 0,
'qos_prefetch_count' => 1,
'ssl_on' => false,
'ssl_verify' => true,
'ssl_cacert' => '',
'ssl_cert' => '',
'ssl_key' => '',
'ssl_passphrase' => '',
];
$this->schemeExtensions = [];
$this->addSupportedScheme('amqp');
$this->addSupportedScheme('amqps');
}
/**
* @param string[] $extensions
*/
public function addSupportedScheme(string $schema): self
{
$this->supportedSchemes[] = $schema;
$this->supportedSchemes = array_unique($this->supportedSchemes);
return $this;
}
/**
* @param string $name
* @param mixed $value
*
* @return self
*/
public function addDefaultOption($name, $value)
{
$this->defaultConfig[$name] = $value;
return $this;
}
/**
* @return self
*/
public function parse()
{
if (empty($this->inputConfig) || in_array($this->inputConfig, $this->supportedSchemes, true)) {
$config = [];
} elseif (is_string($this->inputConfig)) {
$config = $this->parseDsn($this->inputConfig);
} elseif (is_array($this->inputConfig)) {
$config = $this->inputConfig;
if (array_key_exists('dsn', $config)) {
$dsn = $config['dsn'];
unset($config['dsn']);
if ($dsn) {
$config = array_replace($config, $this->parseDsn($dsn));
}
}
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$config = array_replace($this->defaultConfig, $config);
$config['host'] = (string) $config['host'];
$config['port'] = (int) ($config['port']);
$config['user'] = (string) $config['user'];
$config['pass'] = (string) $config['pass'];
$config['read_timeout'] = max((float) ($config['read_timeout']), 0);
$config['write_timeout'] = max((float) ($config['write_timeout']), 0);
$config['connection_timeout'] = max((float) ($config['connection_timeout']), 0);
$config['heartbeat'] = max((float) ($config['heartbeat']), 0);
$config['persisted'] = !empty($config['persisted']);
$config['lazy'] = !empty($config['lazy']);
$config['qos_global'] = !empty($config['qos_global']);
$config['qos_prefetch_count'] = max((int) ($config['qos_prefetch_count']), 0);
$config['qos_prefetch_size'] = max((int) ($config['qos_prefetch_size']), 0);
$config['ssl_on'] = !empty($config['ssl_on']);
$config['ssl_verify'] = !empty($config['ssl_verify']);
$config['ssl_cacert'] = (string) $config['ssl_cacert'];
$config['ssl_cert'] = (string) $config['ssl_cert'];
$config['ssl_key'] = (string) $config['ssl_key'];
$config['ssl_passphrase'] = (string) $config['ssl_passphrase'];
$this->config = $config;
return $this;
}
/**
* @return string[]
*/
public function getSchemeExtensions(): array
{
return $this->schemeExtensions;
}
/**
* @return string
*/
public function getHost()
{
return $this->getOption('host');
}
/**
* @return int
*/
public function getPort()
{
return $this->getOption('port');
}
/**
* @return string
*/
public function getUser()
{
return $this->getOption('user');
}
/**
* @return string
*/
public function getPass()
{
return $this->getOption('pass');
}
/**
* @return string
*/
public function getVHost()
{
return $this->getOption('vhost');
}
/**
* @return int
*/
public function getReadTimeout()
{
return $this->getOption('read_timeout');
}
/**
* @return int
*/
public function getWriteTimeout()
{
return $this->getOption('write_timeout');
}
/**
* @return int
*/
public function getConnectionTimeout()
{
return $this->getOption('connection_timeout');
}
/**
* @return int
*/
public function getHeartbeat()
{
return $this->getOption('heartbeat');
}
/**
* @return bool
*/
public function isPersisted()
{
return $this->getOption('persisted');
}
/**
* @return bool
*/
public function isLazy()
{
return $this->getOption('lazy');
}
/**
* @return bool
*/
public function isQosGlobal()
{
return $this->getOption('qos_global');
}
/**
* @return int
*/
public function getQosPrefetchSize()
{
return $this->getOption('qos_prefetch_size');
}
/**
* @return int
*/
public function getQosPrefetchCount()
{
return $this->getOption('qos_prefetch_count');
}
/**
* @return bool
*/
public function isSslOn()
{
return $this->getOption('ssl_on');
}
/**
* @return bool
*/
public function isSslVerify()
{
return $this->getOption('ssl_verify');
}
/**
* @return string
*/
public function getSslCaCert()
{
return $this->getOption('ssl_cacert');
}
/**
* @return string
*/
public function getSslCert()
{
return $this->getOption('ssl_cert');
}
/**
* @return string
*/
public function getSslKey()
{
return $this->getOption('ssl_key');
}
/**
* @return string
*/
public function getSslPassPhrase()
{
return $this->getOption('ssl_passphrase');
}
/**
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getOption($name, $default = null)
{
$config = $this->getConfig();
return array_key_exists($name, $config) ? $config[$name] : $default;
}
/**
* @throws \LogicException if the input config has not been parsed
*
* @return array
*/
public function getConfig()
{
if (null === $this->config) {
throw new \LogicException('The config has not been parsed.');
}
return $this->config;
}
/**
* @param string $dsn
*
* @return array
*/
private function parseDsn($dsn)
{
$dsn = Dsn::parseFirst($dsn);
$supportedSchemes = $this->supportedSchemes;
if (false == in_array($dsn->getSchemeProtocol(), $supportedSchemes, true)) {
throw new \LogicException(sprintf(
'The given scheme protocol "%s" is not supported. It must be one of "%s".',
$dsn->getSchemeProtocol(),
implode('", "', $supportedSchemes)
));
}
$sslOn = false;
$isAmqps = 'amqps' === $dsn->getSchemeProtocol();
$isTls = in_array('tls', $dsn->getSchemeExtensions(), true);
$isSsl = in_array('ssl', $dsn->getSchemeExtensions(), true);
if ($isAmqps || $isTls || $isSsl) {
$sslOn = true;
}
$this->schemeExtensions = $dsn->getSchemeExtensions();
$config = array_filter(array_replace($dsn->getQuery(), [
'host' => $dsn->getHost(),
'port' => $dsn->getPort(),
'user' => $dsn->getUser(),
'pass' => $dsn->getPassword(),
'vhost' => null !== ($path = $dsn->getPath()) ?
(0 === strpos($path, '/') ? substr($path, 1) : $path)
: null,
'read_timeout' => $dsn->getFloat('read_timeout'),
'write_timeout' => $dsn->getFloat('write_timeout'),
'connection_timeout' => $dsn->getFloat('connection_timeout'),
'heartbeat' => $dsn->getFloat('heartbeat'),
'persisted' => $dsn->getBool('persisted'),
'lazy' => $dsn->getBool('lazy'),
'qos_global' => $dsn->getBool('qos_global'),
'qos_prefetch_size' => $dsn->getDecimal('qos_prefetch_size'),
'qos_prefetch_count' => $dsn->getDecimal('qos_prefetch_count'),
'ssl_on' => $sslOn,
'ssl_verify' => $dsn->getBool('ssl_verify'),
'ssl_cacert' => $dsn->getString('ssl_cacert'),
'ssl_cert' => $dsn->getString('ssl_cert'),
'ssl_key' => $dsn->getString('ssl_key'),
'ssl_passphrase' => $dsn->getString('ssl_passphrase'),
]), function ($value) { return null !== $value; });
return array_map(function ($value) {
return is_string($value) ? rawurldecode($value) : $value;
}, $config);
}
}