@@ -79,6 +79,8 @@ class Connection
79
79
private $ exchangeOptions ;
80
80
private $ queuesOptions ;
81
81
private $ amqpFactory ;
82
+ private $ autoSetupExchange ;
83
+ private $ autoSetup ;
82
84
83
85
/**
84
86
* @var \AMQPChannel|null
@@ -112,6 +114,7 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar
112
114
'queue_name_pattern ' => 'delay_%exchange_name%_%routing_key%_%delay% ' ,
113
115
],
114
116
], $ connectionOptions );
117
+ $ this ->autoSetupExchange = $ this ->autoSetup = $ connectionOptions ['auto_setup ' ] ?? true ;
115
118
$ this ->exchangeOptions = $ exchangeOptions ;
116
119
$ this ->queuesOptions = $ queuesOptions ;
117
120
$ this ->amqpFactory = $ amqpFactory ?: new AmqpFactory ();
@@ -207,6 +210,9 @@ public static function fromDsn(string $dsn, array $options = [], AmqpFactory $am
207
210
$ exchangeOptions = $ amqpOptions ['exchange ' ];
208
211
$ queuesOptions = $ amqpOptions ['queues ' ];
209
212
unset($ amqpOptions ['queues ' ], $ amqpOptions ['exchange ' ]);
213
+ if (isset ($ amqpOptions ['auto_setup ' ])) {
214
+ $ amqpOptions ['auto_setup ' ] = filter_var ($ amqpOptions ['auto_setup ' ], \FILTER_VALIDATE_BOOLEAN );
215
+ }
210
216
211
217
$ queuesOptions = array_map (function ($ queueOptions ) {
212
218
if (!\is_array ($ queueOptions )) {
@@ -285,7 +291,7 @@ public function publish(string $body, array $headers = [], int $delayInMs = 0, A
285
291
return ;
286
292
}
287
293
288
- if ($ this ->shouldSetup () ) {
294
+ if ($ this ->autoSetupExchange ) {
289
295
$ this ->setupExchangeAndQueues ();
290
296
}
291
297
@@ -347,7 +353,7 @@ private function publishOnExchange(\AMQPExchange $exchange, string $body, string
347
353
348
354
private function setupDelay (int $ delay , ?string $ routingKey )
349
355
{
350
- if ($ this ->shouldSetup () ) {
356
+ if ($ this ->autoSetup ) {
351
357
$ this ->setup (); // setup delay exchange and normal exchange for delay queue to DLX messages to
352
358
}
353
359
@@ -418,23 +424,12 @@ public function get(string $queueName): ?\AMQPEnvelope
418
424
{
419
425
$ this ->clearWhenDisconnected ();
420
426
421
- if ($ this ->shouldSetup () ) {
427
+ if ($ this ->autoSetupExchange ) {
422
428
$ this ->setupExchangeAndQueues ();
423
429
}
424
430
425
- try {
426
- if (false !== $ message = $ this ->queue ($ queueName )->get ()) {
427
- return $ message ;
428
- }
429
- } catch (\AMQPQueueException $ e ) {
430
- if (404 === $ e ->getCode () && $ this ->shouldSetup ()) {
431
- // If we get a 404 for the queue, it means we need to set up the exchange & queue.
432
- $ this ->setupExchangeAndQueues ();
433
-
434
- return $ this ->get ($ queueName );
435
- }
436
-
437
- throw $ e ;
431
+ if (false !== $ message = $ this ->queue ($ queueName )->get ()) {
432
+ return $ message ;
438
433
}
439
434
440
435
return null ;
@@ -452,8 +447,11 @@ public function nack(\AMQPEnvelope $message, string $queueName, int $flags = \AM
452
447
453
448
public function setup (): void
454
449
{
455
- $ this ->setupExchangeAndQueues ();
450
+ if ($ this ->autoSetupExchange ) {
451
+ $ this ->setupExchangeAndQueues ();
452
+ }
456
453
$ this ->getDelayExchange ()->declareExchange ();
454
+ $ this ->autoSetup = false ;
457
455
}
458
456
459
457
private function setupExchangeAndQueues (): void
@@ -466,6 +464,7 @@ private function setupExchangeAndQueues(): void
466
464
$ this ->queue ($ queueName )->bind ($ this ->exchangeOptions ['name ' ], $ bindingKey , $ queueConfig ['binding_arguments ' ] ?? []);
467
465
}
468
466
}
467
+ $ this ->autoSetupExchange = false ;
469
468
}
470
469
471
470
/**
@@ -558,19 +557,6 @@ private function clearWhenDisconnected(): void
558
557
}
559
558
}
560
559
561
- private function shouldSetup (): bool
562
- {
563
- if (!\array_key_exists ('auto_setup ' , $ this ->connectionOptions )) {
564
- return true ;
565
- }
566
-
567
- if (\in_array ($ this ->connectionOptions ['auto_setup ' ], [false , 'false ' ], true )) {
568
- return false ;
569
- }
570
-
571
- return true ;
572
- }
573
-
574
560
private function getDefaultPublishRoutingKey (): ?string
575
561
{
576
562
return $ this ->exchangeOptions ['default_publish_routing_key ' ] ?? null ;
0 commit comments