Skip to content

Commit 906b9de

Browse files
author
Oleg Namaka
committed
Add multiple bindings support
1 parent 10a813f commit 906b9de

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/Symfony/Component/Messenger/Bridge/Amqp/Transport/Connection.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,21 @@ class Connection
6666
];
6767

6868
private const AVAILABLE_QUEUE_OPTIONS = [
69+
'flags',
70+
'arguments',
71+
];
72+
73+
private const NEW_QUEUE_OPTIONS = [
74+
'bindings',
75+
];
76+
77+
private const DEPRECATED_BINDING_KEYS = [
6978
'binding_keys',
7079
'binding_arguments',
71-
'flags',
80+
];
81+
82+
private const AVAILABLE_BINDINGS_OPTIONS = [
83+
'key',
7284
'arguments',
7385
];
7486

@@ -145,8 +157,11 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar
145157
* * connect_timeout: Connection timeout. Note: 0 or greater seconds. May be fractional.
146158
* * confirm_timeout: Timeout in seconds for confirmation, if none specified transport will not wait for message confirmation. Note: 0 or greater seconds. May be fractional.
147159
* * queues[name]: An array of queues, keyed by the name
148-
* * binding_keys: The binding keys (if any) to bind to this queue
149-
* * binding_arguments: Arguments to be used while binding the queue.
160+
* * binding_keys: The binding keys (if any) to bind to this queue (Usage is deprecated. See 'bindings')
161+
* * binding_arguments: Arguments to be used while binding the queue. (Usage is deprecated. See 'bindings')
162+
* * bindings[name]: An array of bindings for this queue, keyed by the name
163+
* * key: The binding key (if any) to bind to this queue
164+
* * arguments: An array of arguments to be used while binding the queue.
150165
* * flags: Queue flags (Default: AMQP_DURABLE)
151166
* * arguments: Extra arguments
152167
* * exchange:
@@ -261,9 +276,24 @@ private static function validateOptions(array $options): void
261276
continue;
262277
}
263278

264-
if (0 < \count($invalidQueueOptions = array_diff(array_keys($queue), self::AVAILABLE_QUEUE_OPTIONS))) {
279+
if (0 < \count($deprecatedQueueOptions = array_intersect(array_keys($queue), self::DEPRECATED_BINDING_KEYS))) {
280+
trigger_deprecation('symfony/messenger', '6.3', 'Deprecated queue option(s) "%s" passed to the AMQP Messenger transport. The "%s" option(s) should be used rather than "%s".', implode('", "', $deprecatedQueueOptions), implode('", ', self::NEW_QUEUE_OPTIONS), implode('", ', self::DEPRECATED_BINDING_KEYS));
281+
if (0 < \count($newQueueOptions = array_intersect(array_keys($queue), self::NEW_QUEUE_OPTIONS))) {
282+
throw new LogicException(sprintf('New "%s" and deprecated "%s" option(s) passed to the AMQP Messenger transport', implode('", "', $newQueueOptions), implode('", "', $deprecatedQueueOptions)));
283+
}
284+
}
285+
286+
if (0 < \count($invalidQueueOptions = array_diff(array_keys($queue), self::AVAILABLE_QUEUE_OPTIONS, self::NEW_QUEUE_OPTIONS, self::DEPRECATED_BINDING_KEYS))) {
265287
trigger_deprecation('symfony/messenger', '5.1', 'Invalid queue option(s) "%s" passed to the AMQP Messenger transport. Passing invalid queue options is deprecated.', implode('", "', $invalidQueueOptions));
266288
}
289+
290+
if (\is_array($queue['bindings'] ?? false)) {
291+
foreach ($queue['bindings'] as $individualBinding) {
292+
if (0 < \count(array_diff(array_keys($individualBinding), self::AVAILABLE_BINDINGS_OPTIONS))) {
293+
throw new LogicException(sprintf("Valid options for each 'bindings' are: %s", implode(', ', self::AVAILABLE_BINDINGS_OPTIONS)));
294+
}
295+
}
296+
}
267297
}
268298
}
269299

@@ -478,9 +508,9 @@ public function pull(string $queueName, ?callable $callback): void
478508
$this->queue($queueName)->consume($callback);
479509
}
480510

481-
public function ack(\AMQPEnvelope $message, string $queueName): bool
511+
public function ack(\AMQPEnvelope $message, string $queueName, int $flags = \AMQP_NOPARAM): bool
482512
{
483-
return $this->queue($queueName)->ack($message->getDeliveryTag()) ?? true;
513+
return $this->queue($queueName)->ack($message->getDeliveryTag(), $flags);
484514
}
485515

486516
public function nack(\AMQPEnvelope $message, string $queueName, int $flags = \AMQP_NOPARAM): bool
@@ -500,6 +530,12 @@ private function setupExchangeAndQueues(): void
500530

501531
foreach ($this->queuesOptions as $queueName => $queueConfig) {
502532
$this->queue($queueName)->declareQueue();
533+
foreach ($queueConfig['bindings'] ?? [] as $binding) {
534+
$this->queue($queueName)->bind($this->exchangeOptions['name'], $binding['key'] ?? null, $binding['arguments'] ?? []);
535+
}
536+
if (isset($queueConfig['bindings']) && empty($queueConfig['binding_keys'])) {
537+
continue;
538+
}
503539
foreach ($queueConfig['binding_keys'] ?? [null] as $bindingKey) {
504540
$this->queue($queueName)->bind($this->exchangeOptions['name'], $bindingKey, $queueConfig['binding_arguments'] ?? []);
505541
}

0 commit comments

Comments
 (0)