|
| 1 | +## DSN Parser. |
| 2 | + |
| 3 | +The [enqueue/dsn](https://github.com/php-enqueue/dsn) tool helps to parse DSN\URI string. |
| 4 | +The tool is used by Enqueue transports to parse DSNs. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +```bash |
| 9 | +composer req enqueue/dsn 0.9.x |
| 10 | +``` |
| 11 | + |
| 12 | +### Examples |
| 13 | + |
| 14 | +Basic usage: |
| 15 | + |
| 16 | +```php |
| 17 | +<?php |
| 18 | + |
| 19 | +use Enqueue\Dsn\Dsn; |
| 20 | + |
| 21 | +$dsn = new Dsn('mysql+pdo://user:password@localhost:3306/database?connection_timeout=123'); |
| 22 | + |
| 23 | +$dsn->getSchemeProtocol(); // 'mysql' |
| 24 | +$dsn->getScheme(); // 'mysql+pdo' |
| 25 | +$dsn->getSchemeExtensions(); // ['pdo'] |
| 26 | +$dsn->getUser(); // 'user' |
| 27 | +$dsn->getPassword(); // 'password' |
| 28 | +$dsn->getHost(); // 'localhost' |
| 29 | +$dsn->getPort(); // 3306 |
| 30 | + |
| 31 | +$dsn->getQueryString(); // 'connection_timeout=123' |
| 32 | +$dsn->getQuery(); // ['connection_timeout' => '123'] |
| 33 | +$dsn->getQueryParameter('connection_timeout'); // '123' |
| 34 | +$dsn->getInt('connection_timeout'); // 123 |
| 35 | +``` |
| 36 | + |
| 37 | +Some parts could be omitted: |
| 38 | + |
| 39 | +```php |
| 40 | +<?php |
| 41 | +use Enqueue\Dsn\Dsn; |
| 42 | + |
| 43 | +$dsn = new Dsn('sqs:?key=aKey&secret=aSecret&token=aToken'); |
| 44 | + |
| 45 | +$dsn->getSchemeProtocol(); // 'sqs' |
| 46 | +$dsn->getScheme(); // 'sqs' |
| 47 | +$dsn->getSchemeExtensions(); // [] |
| 48 | +$dsn->getUser(); // null |
| 49 | +$dsn->getPassword(); // null |
| 50 | +$dsn->getHost(); // null |
| 51 | +$dsn->getPort(); // null |
| 52 | + |
| 53 | +$dsn->getQueryParameter('key'); // 'aKey' |
| 54 | +$dsn->getQueryParameter('secret'); // 'aSecret' |
| 55 | +``` |
| 56 | + |
| 57 | +Throws exception if DSN not valid: |
| 58 | + |
| 59 | +```php |
| 60 | +<?php |
| 61 | +use Enqueue\Dsn\Dsn; |
| 62 | + |
| 63 | +$dsn = new Dsn('foo'); // throws exception here |
| 64 | +``` |
| 65 | + |
| 66 | +Throws exception if cannot cast query parameter: |
| 67 | + |
| 68 | +```php |
| 69 | +<?php |
| 70 | +use Enqueue\Dsn\Dsn; |
| 71 | + |
| 72 | +$dsn = new Dsn('mysql:?connection_timeout=notInt'); |
| 73 | + |
| 74 | +$dsn->getInt('connection_timeout'); // throws exception here |
| 75 | +``` |
| 76 | + |
| 77 | +[back to index](index.md) |
0 commit comments