Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[dsn] Parse DSN Cluster #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions docs/dsn.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Basic usage:

use Enqueue\Dsn\Dsn;

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

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

$dsn->getQueryString(); // 'connection_timeout=123'
$dsn->getQuery(); // ['connection_timeout' => '123']
$dsn->getQueryParameter('connection_timeout'); // '123'
$dsn->getInt('connection_timeout'); // 123
$dsn->getString('connection_timeout'); // '123'
$dsn->getDecimal('connection_timeout'); // 123
```

Parse Cluster DSN:

```php
<?php

use Enqueue\Dsn\Dsn;

$dsns = Dsn::parse('mysql+pdo://user:password@foo:3306,bar:5678/database?connection_timeout=123');

count($dsns); // 2

$dsns[0]->getUser(); // 'user'
$dsns[0]->getPassword(); // 'password'
$dsns[0]->getHost(); // 'foo'
$dsns[0]->getPort(); // 3306

$dsns[1]->getUser(); // 'user'
$dsns[1]->getPassword(); // 'password'
$dsns[1]->getHost(); // 'bar'
$dsns[1]->getPort(); // 5678
```

Some parts could be omitted:
Expand All @@ -49,7 +71,7 @@ Some parts could be omitted:
<?php
use Enqueue\Dsn\Dsn;

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

$dsn->getSchemeProtocol(); // 'sqs'
$dsn->getScheme(); // 'sqs'
Expand All @@ -59,8 +81,25 @@ $dsn->getPassword(); // null
$dsn->getHost(); // null
$dsn->getPort(); // null

$dsn->getQueryParameter('key'); // 'aKey'
$dsn->getQueryParameter('secret'); // 'aSecret'
$dsn->getString('key'); // 'aKey'
$dsn->getString('secret'); // 'aSecret'
```

Get typed query params:

```php
<?php
use Enqueue\Dsn\Dsn;

$dsn = Dsn::parseFirst('sqs:?decimal=12&octal=0666&float=1.2&bool=1&array[0]=val&array[1]=123');

$dsn->getDecimal('decimal'); // 12
$dsn->getOctal('decimal'); // 0666
$dsn->getFloat('float'); // 1.2
$dsn->getBool('bool'); // true
$dsn->getArray('array')->getString(0); // val
$dsn->getArray('array')->getDecimal(1); // 123
$dsn->getArray('array')->toArray(); // [val]
```

Throws exception if DSN not valid:
Expand All @@ -69,7 +108,7 @@ Throws exception if DSN not valid:
<?php
use Enqueue\Dsn\Dsn;

$dsn = new Dsn('foo'); // throws exception here
$dsn = Dsn::parseFirst('foo'); // throws exception here
```

Throws exception if cannot cast query parameter:
Expand All @@ -78,9 +117,9 @@ Throws exception if cannot cast query parameter:
<?php
use Enqueue\Dsn\Dsn;

$dsn = new Dsn('mysql:?connection_timeout=notInt');
$dsn = Dsn::parseFirst('mysql:?connection_timeout=notInt');

$dsn->getInt('connection_timeout'); // throws exception here
$dsn->getDecimal('connection_timeout'); // throws exception here
```

[back to index](index.md)
14 changes: 7 additions & 7 deletions pkg/amqp-tools/ConnectionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public function getConfig()
*/
private function parseDsn($dsn)
{
$dsn = new Dsn($dsn);
$dsn = Dsn::parseFirst($dsn);

$supportedSchemes = $this->supportedSchemes;
if (false == in_array($dsn->getSchemeProtocol(), $supportedSchemes, true)) {
Expand Down Expand Up @@ -410,14 +410,14 @@ private function parseDsn($dsn)
'persisted' => $dsn->getBool('persisted'),
'lazy' => $dsn->getBool('lazy'),
'qos_global' => $dsn->getBool('qos_global'),
'qos_prefetch_size' => $dsn->getInt('qos_prefetch_size'),
'qos_prefetch_count' => $dsn->getInt('qos_prefetch_count'),
'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->getQueryParameter('ssl_cacert'),
'ssl_cert' => $dsn->getQueryParameter('ssl_cert'),
'ssl_key' => $dsn->getQueryParameter('ssl_key'),
'ssl_passphrase' => $dsn->getQueryParameter('ssl_passphrase'),
'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) {
Expand Down
Loading