Skip to content

Commit 0d57187

Browse files
committed
[dsn] Cluster DSN
1 parent c8936f8 commit 0d57187

17 files changed

+532
-200
lines changed

docs/dsn.md

+47-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Basic usage:
2727

2828
use Enqueue\Dsn\Dsn;
2929

30-
$dsn = new Dsn('mysql+pdo://user:password@localhost:3306/database?connection_timeout=123');
30+
$dsn = Dsn::parseFirst('mysql+pdo://user:password@localhost:3306/database?connection_timeout=123');
3131

3232
$dsn->getSchemeProtocol(); // 'mysql'
3333
$dsn->getScheme(); // 'mysql+pdo'
@@ -39,8 +39,30 @@ $dsn->getPort(); // 3306
3939

4040
$dsn->getQueryString(); // 'connection_timeout=123'
4141
$dsn->getQuery(); // ['connection_timeout' => '123']
42-
$dsn->getQueryParameter('connection_timeout'); // '123'
43-
$dsn->getInt('connection_timeout'); // 123
42+
$dsn->getString('connection_timeout'); // '123'
43+
$dsn->getDecimal('connection_timeout'); // 123
44+
```
45+
46+
Parse Cluster DSN:
47+
48+
```php
49+
<?php
50+
51+
use Enqueue\Dsn\Dsn;
52+
53+
$dsns = Dsn::parse('mysql+pdo://user:password@foo:3306,bar:5678/database?connection_timeout=123');
54+
55+
count($dsns); // 2
56+
57+
$dsns[0]->getUser(); // 'user'
58+
$dsns[0]->getPassword(); // 'password'
59+
$dsns[0]->getHost(); // 'foo'
60+
$dsns[0]->getPort(); // 3306
61+
62+
$dsns[1]->getUser(); // 'user'
63+
$dsns[1]->getPassword(); // 'password'
64+
$dsns[1]->getHost(); // 'bar'
65+
$dsns[1]->getPort(); // 5678
4466
```
4567

4668
Some parts could be omitted:
@@ -49,7 +71,7 @@ Some parts could be omitted:
4971
<?php
5072
use Enqueue\Dsn\Dsn;
5173

52-
$dsn = new Dsn('sqs:?key=aKey&secret=aSecret&token=aToken');
74+
$dsn = Dsn::parseFirst('sqs:?key=aKey&secret=aSecret&token=aToken');
5375

5476
$dsn->getSchemeProtocol(); // 'sqs'
5577
$dsn->getScheme(); // 'sqs'
@@ -59,8 +81,24 @@ $dsn->getPassword(); // null
5981
$dsn->getHost(); // null
6082
$dsn->getPort(); // null
6183

62-
$dsn->getQueryParameter('key'); // 'aKey'
63-
$dsn->getQueryParameter('secret'); // 'aSecret'
84+
$dsn->getString('key'); // 'aKey'
85+
$dsn->getString('secret'); // 'aSecret'
86+
```
87+
88+
Get typed query params:
89+
90+
```php
91+
<?php
92+
use Enqueue\Dsn\Dsn;
93+
94+
$dsn = Dsn::parseFirst('sqs:?decimal=12&octal=0666&float=1.2&bool=1&array[0]=val');
95+
96+
$dsn->getDecimal('decimal'); // 12
97+
$dsn->getOctal('decimal'); // 0666
98+
$dsn->getFloat('float'); // 1.2
99+
$dsn->getBool('bool'); // true
100+
$dsn->getArray('array')->getString(0); // val
101+
$dsn->getArray('array')->toArray(); // [val]
64102
```
65103

66104
Throws exception if DSN not valid:
@@ -69,7 +107,7 @@ Throws exception if DSN not valid:
69107
<?php
70108
use Enqueue\Dsn\Dsn;
71109

72-
$dsn = new Dsn('foo'); // throws exception here
110+
$dsn = Dsn::parseFirst('foo'); // throws exception here
73111
```
74112

75113
Throws exception if cannot cast query parameter:
@@ -78,9 +116,9 @@ Throws exception if cannot cast query parameter:
78116
<?php
79117
use Enqueue\Dsn\Dsn;
80118

81-
$dsn = new Dsn('mysql:?connection_timeout=notInt');
119+
$dsn = Dsn::parseFirst('mysql:?connection_timeout=notInt');
82120

83-
$dsn->getInt('connection_timeout'); // throws exception here
121+
$dsn->getDecimal('connection_timeout'); // throws exception here
84122
```
85123

86124
[back to index](index.md)

pkg/amqp-tools/ConnectionConfig.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public function getConfig()
379379
*/
380380
private function parseDsn($dsn)
381381
{
382-
$dsn = new Dsn($dsn);
382+
$dsn = Dsn::parseFirst($dsn);
383383

384384
$supportedSchemes = $this->supportedSchemes;
385385
if (false == in_array($dsn->getSchemeProtocol(), $supportedSchemes, true)) {
@@ -410,14 +410,14 @@ private function parseDsn($dsn)
410410
'persisted' => $dsn->getBool('persisted'),
411411
'lazy' => $dsn->getBool('lazy'),
412412
'qos_global' => $dsn->getBool('qos_global'),
413-
'qos_prefetch_size' => $dsn->getInt('qos_prefetch_size'),
414-
'qos_prefetch_count' => $dsn->getInt('qos_prefetch_count'),
413+
'qos_prefetch_size' => $dsn->getDecimal('qos_prefetch_size'),
414+
'qos_prefetch_count' => $dsn->getDecimal('qos_prefetch_count'),
415415
'ssl_on' => $sslOn,
416416
'ssl_verify' => $dsn->getBool('ssl_verify'),
417-
'ssl_cacert' => $dsn->getQueryParameter('ssl_cacert'),
418-
'ssl_cert' => $dsn->getQueryParameter('ssl_cert'),
419-
'ssl_key' => $dsn->getQueryParameter('ssl_key'),
420-
'ssl_passphrase' => $dsn->getQueryParameter('ssl_passphrase'),
417+
'ssl_cacert' => $dsn->getString('ssl_cacert'),
418+
'ssl_cert' => $dsn->getString('ssl_cert'),
419+
'ssl_key' => $dsn->getString('ssl_key'),
420+
'ssl_passphrase' => $dsn->getString('ssl_passphrase'),
421421
]), function ($value) { return null !== $value; });
422422

423423
return array_map(function ($value) {

0 commit comments

Comments
 (0)