|
12 | 12 | namespace Symfony\Component\Messenger\Transport\AmqpExt;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Messenger\Exception\InvalidArgumentException;
|
| 15 | +use Symfony\Component\Messenger\Transport\Dsn; |
15 | 16 |
|
16 | 17 | /**
|
17 | 18 | * An AMQP connection.
|
@@ -99,34 +100,28 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar
|
99 | 100 | */
|
100 | 101 | public static function fromDsn(string $dsn, array $options = [], AmqpFactory $amqpFactory = null): self
|
101 | 102 | {
|
102 |
| - if (false === $parsedUrl = parse_url($dsn)) { |
103 |
| - // this is a valid URI that parse_url cannot handle when you want to pass all parameters as options |
104 |
| - if ('amqp://' !== $dsn) { |
105 |
| - throw new InvalidArgumentException(sprintf('The given AMQP DSN "%s" is invalid.', $dsn)); |
106 |
| - } |
107 |
| - |
108 |
| - $parsedUrl = []; |
109 |
| - } |
| 103 | + return self::fromDsnObject(Dsn::fromString($dsn, $options)); |
| 104 | + } |
110 | 105 |
|
111 |
| - $pathParts = isset($parsedUrl['path']) ? explode('/', trim($parsedUrl['path'], '/')) : []; |
112 |
| - $exchangeName = $pathParts[1] ?? 'messages'; |
113 |
| - parse_str($parsedUrl['query'] ?? '', $parsedQuery); |
| 106 | + public static function fromDsnObject(Dsn $dsn, AmqpFactory $amqpFactory = null): self |
| 107 | + { |
| 108 | + list($vhost, $exchangeName) = ['/', 'messages'] + explode('/', trim($dsn->getPath(''), '/')); |
114 | 109 |
|
115 | 110 | $amqpOptions = array_replace_recursive([
|
116 |
| - 'host' => $parsedUrl['host'] ?? 'localhost', |
117 |
| - 'port' => $parsedUrl['port'] ?? 5672, |
118 |
| - 'vhost' => isset($pathParts[0]) ? urldecode($pathParts[0]) : '/', |
| 111 | + 'host' => $dsn->getHost('localhost'), |
| 112 | + 'port' => $dsn->getPort(5672), |
| 113 | + 'vhost' => $vhost, |
119 | 114 | 'exchange' => [
|
120 | 115 | 'name' => $exchangeName,
|
121 | 116 | ],
|
122 |
| - ], $options, $parsedQuery); |
| 117 | + ], $dsn->getOptions()); |
123 | 118 |
|
124 |
| - if (isset($parsedUrl['user'])) { |
125 |
| - $amqpOptions['login'] = $parsedUrl['user']; |
| 119 | + if ($user = $dsn->getUser()) { |
| 120 | + $amqpOptions['login'] = $user; |
126 | 121 | }
|
127 | 122 |
|
128 |
| - if (isset($parsedUrl['pass'])) { |
129 |
| - $amqpOptions['password'] = $parsedUrl['pass']; |
| 123 | + if ($password = $dsn->getPassword()) { |
| 124 | + $amqpOptions['password'] = $password; |
130 | 125 | }
|
131 | 126 |
|
132 | 127 | if (!isset($amqpOptions['queues'])) {
|
|
0 commit comments