|
4 | 4 |
|
5 | 5 | namespace Enqueue\Stomp; |
6 | 6 |
|
| 7 | +use Enqueue\Dsn\Dsn; |
7 | 8 | use Interop\Queue\PsrConnectionFactory; |
8 | 9 | use Interop\Queue\PsrContext; |
9 | 10 | use Stomp\Network\Connection; |
@@ -48,6 +49,11 @@ public function __construct($config = 'stomp:') |
48 | 49 | } elseif (is_string($config)) { |
49 | 50 | $config = $this->parseDsn($config); |
50 | 51 | } elseif (is_array($config)) { |
| 52 | + if (array_key_exists('dsn', $config)) { |
| 53 | + $config = array_replace($config, $this->parseDsn($config['dsn'])); |
| 54 | + |
| 55 | + unset($config['dsn']); |
| 56 | + } |
51 | 57 | } else { |
52 | 58 | throw new \LogicException('The config must be either an array of options, a DSN string or null'); |
53 | 59 | } |
@@ -91,27 +97,24 @@ private function establishConnection(): BufferedStompClient |
91 | 97 |
|
92 | 98 | private function parseDsn(string $dsn): array |
93 | 99 | { |
94 | | - if (false === strpos($dsn, 'stomp:')) { |
95 | | - throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "stomp:".', $dsn)); |
96 | | - } |
97 | | - |
98 | | - if (false === $config = parse_url($dsn)) { |
99 | | - throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn)); |
100 | | - } |
| 100 | + $dsn = new Dsn($dsn); |
101 | 101 |
|
102 | | - if ($query = parse_url($dsn, PHP_URL_QUERY)) { |
103 | | - $queryConfig = []; |
104 | | - parse_str($query, $queryConfig); |
105 | | - |
106 | | - $config = array_replace($queryConfig, $config); |
| 102 | + if ('stomp' !== $dsn->getSchemeProtocol()) { |
| 103 | + throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "stomp:".', $dsn)); |
107 | 104 | } |
108 | 105 |
|
109 | | - unset($config['query'], $config['scheme']); |
110 | | - |
111 | | - $config['sync'] = empty($config['sync']) ? false : true; |
112 | | - $config['lazy'] = empty($config['lazy']) ? false : true; |
113 | | - |
114 | | - return $config; |
| 106 | + return array_filter(array_replace($dsn->getQuery(), [ |
| 107 | + 'host' => $dsn->getHost(), |
| 108 | + 'port' => $dsn->getPort(), |
| 109 | + 'login' => $dsn->getUser(), |
| 110 | + 'password' => $dsn->getPassword(), |
| 111 | + 'vhost' => null !== $dsn->getPath() ? ltrim($dsn->getPath(), '/') : null, |
| 112 | + 'buffer_size' => $dsn->getInt('buffer_size'), |
| 113 | + 'connection_timeout' => $dsn->getInt('connection_timeout'), |
| 114 | + 'sync' => $dsn->getBool('sync'), |
| 115 | + 'lazy' => $dsn->getBool('lazy'), |
| 116 | + 'ssl_on' => $dsn->getBool('ssl_on'), |
| 117 | + ]), function ($value) { return null !== $value; }); |
115 | 118 | } |
116 | 119 |
|
117 | 120 | private function defaultConfig(): array |
|
0 commit comments