@@ -33,9 +33,9 @@ class Connection
33
33
'x-message-ttl ' ,
34
34
];
35
35
36
- private $ connectionConfiguration ;
37
- private $ exchangeConfiguration ;
38
- private $ queuesConfiguration ;
36
+ private $ connectionOptions ;
37
+ private $ exchangeOptions ;
38
+ private $ queuesOptions ;
39
39
private $ amqpFactory ;
40
40
41
41
/**
@@ -86,17 +86,17 @@ class Connection
86
86
* * loop_sleep: Amount of micro-seconds to wait if no message are available (Default: 200000)
87
87
* * prefetch_count: set channel prefetch count
88
88
*/
89
- public function __construct (array $ connectionConfiguration , array $ exchangeConfiguration , array $ queuesConfiguration , AmqpFactory $ amqpFactory = null )
89
+ public function __construct (array $ connectionOptions , array $ exchangeOptions , array $ queuesOptions , AmqpFactory $ amqpFactory = null )
90
90
{
91
- $ this ->connectionConfiguration = array_replace_recursive ([
91
+ $ this ->connectionOptions = array_replace_recursive ([
92
92
'delay ' => [
93
93
'routing_key_pattern ' => 'delay_%delay% ' ,
94
94
'exchange_name ' => 'delay ' ,
95
95
'queue_name_pattern ' => 'delay_queue_%delay% ' ,
96
96
],
97
- ], $ connectionConfiguration );
98
- $ this ->exchangeConfiguration = $ exchangeConfiguration ;
99
- $ this ->queuesConfiguration = $ queuesConfiguration ;
97
+ ], $ connectionOptions );
98
+ $ this ->exchangeOptions = $ exchangeOptions ;
99
+ $ this ->queuesOptions = $ queuesOptions ;
100
100
$ this ->amqpFactory = $ amqpFactory ?: new AmqpFactory ();
101
101
}
102
102
@@ -183,8 +183,6 @@ public function publish(string $body, array $headers = [], int $delay = 0, strin
183
183
$ this ->setup ();
184
184
}
185
185
186
- // TODO - allow flag & attributes to be configured on the message
187
-
188
186
$ this ->exchange ()->publish (
189
187
$ body ,
190
188
$ routingKey ?? $ this ->getDefaultPublishRoutingKey (),
@@ -214,8 +212,6 @@ private function publishWithDelay(string $body, array $headers, int $delay, ?str
214
212
$ this ->setupDelay ($ delay , $ exchangeRoutingKey );
215
213
}
216
214
217
- // TODO - allow flag & attributes to be configured on the message
218
-
219
215
$ this ->getDelayExchange ()->publish (
220
216
$ body ,
221
217
$ this ->getRoutingKeyForDelay ($ delay ),
@@ -244,7 +240,7 @@ private function getDelayExchange(): \AMQPExchange
244
240
{
245
241
if (null === $ this ->amqpDelayExchange ) {
246
242
$ this ->amqpDelayExchange = $ this ->amqpFactory ->createExchange ($ this ->channel ());
247
- $ this ->amqpDelayExchange ->setName ($ this ->connectionConfiguration ['delay ' ]['exchange_name ' ]);
243
+ $ this ->amqpDelayExchange ->setName ($ this ->connectionOptions ['delay ' ]['exchange_name ' ]);
248
244
$ this ->amqpDelayExchange ->setType (AMQP_EX_TYPE_DIRECT );
249
245
}
250
246
@@ -262,10 +258,8 @@ private function getDelayExchange(): \AMQPExchange
262
258
*/
263
259
private function createDelayQueue (int $ delay , ?string $ routingKey )
264
260
{
265
- $ delayConfiguration = $ this ->connectionConfiguration ['delay ' ];
266
-
267
261
$ queue = $ this ->amqpFactory ->createQueue ($ this ->channel ());
268
- $ queue ->setName (str_replace ('%delay% ' , $ delay , $ delayConfiguration ['queue_name_pattern ' ]));
262
+ $ queue ->setName (str_replace ('%delay% ' , $ delay , $ this -> connectionOptions [ ' delay ' ] ['queue_name_pattern ' ]));
269
263
$ queue ->setArguments ([
270
264
'x-message-ttl ' => $ delay ,
271
265
'x-dead-letter-exchange ' => $ this ->exchange ()->getName (),
@@ -282,7 +276,7 @@ private function createDelayQueue(int $delay, ?string $routingKey)
282
276
283
277
private function getRoutingKeyForDelay (int $ delay ): string
284
278
{
285
- return str_replace ('%delay% ' , $ delay , $ this ->connectionConfiguration ['delay ' ]['routing_key_pattern ' ]);
279
+ return str_replace ('%delay% ' , $ delay , $ this ->connectionOptions ['delay ' ]['routing_key_pattern ' ]);
286
280
}
287
281
288
282
/**
@@ -332,7 +326,7 @@ public function setup(): void
332
326
333
327
$ this ->exchange ()->declareExchange ();
334
328
335
- foreach ($ this ->queuesConfiguration as $ queueName => $ queueConfig ) {
329
+ foreach ($ this ->queuesOptions as $ queueName => $ queueConfig ) {
336
330
$ this ->queue ($ queueName )->declareQueue ();
337
331
foreach ($ queueConfig ['binding_keys ' ] ?? [null ] as $ bindingKey ) {
338
332
$ this ->queue ($ queueName )->bind ($ this ->exchange ()->getName (), $ bindingKey );
@@ -345,40 +339,37 @@ public function setup(): void
345
339
*/
346
340
public function getQueueNames (): array
347
341
{
348
- return array_keys ($ this ->queuesConfiguration );
342
+ return array_keys ($ this ->queuesOptions );
349
343
}
350
344
351
- /**
352
- * @internal
353
- */
354
345
public function channel (): \AMQPChannel
355
346
{
356
347
if (null === $ this ->amqpChannel ) {
357
- $ connection = $ this ->amqpFactory ->createConnection ($ this ->connectionConfiguration );
358
- $ connectMethod = 'true ' === ($ this ->connectionConfiguration ['persistent ' ] ?? 'false ' ) ? 'pconnect ' : 'connect ' ;
348
+ $ connection = $ this ->amqpFactory ->createConnection ($ this ->connectionOptions );
349
+ $ connectMethod = 'true ' === ($ this ->connectionOptions ['persistent ' ] ?? 'false ' ) ? 'pconnect ' : 'connect ' ;
359
350
360
351
try {
361
352
$ connection ->{$ connectMethod }();
362
353
} catch (\AMQPConnectionException $ e ) {
363
- $ credentials = $ this ->connectionConfiguration ;
354
+ $ credentials = $ this ->connectionOptions ;
364
355
$ credentials ['password ' ] = '******** ' ;
365
356
366
357
throw new \AMQPException (sprintf ('Could not connect to the AMQP server. Please verify the provided DSN. (%s) ' , json_encode ($ credentials )), 0 , $ e );
367
358
}
368
359
$ this ->amqpChannel = $ this ->amqpFactory ->createChannel ($ connection );
369
360
370
- if (isset ($ this ->connectionConfiguration ['prefetch_count ' ])) {
371
- $ this ->amqpChannel ->setPrefetchCount ($ this ->connectionConfiguration ['prefetch_count ' ]);
361
+ if (isset ($ this ->connectionOptions ['prefetch_count ' ])) {
362
+ $ this ->amqpChannel ->setPrefetchCount ($ this ->connectionOptions ['prefetch_count ' ]);
372
363
}
373
364
}
374
365
375
366
return $ this ->amqpChannel ;
376
367
}
377
368
378
- private function queue (string $ queueName ): \AMQPQueue
369
+ public function queue (string $ queueName ): \AMQPQueue
379
370
{
380
371
if (!isset ($ this ->amqpQueues [$ queueName ])) {
381
- $ queueConfig = $ this ->queuesConfiguration [$ queueName ];
372
+ $ queueConfig = $ this ->queuesOptions [$ queueName ];
382
373
383
374
$ amqpQueue = $ this ->amqpFactory ->createQueue ($ this ->channel ());
384
375
$ amqpQueue ->setName ($ queueName );
@@ -394,27 +385,22 @@ private function queue(string $queueName): \AMQPQueue
394
385
return $ this ->amqpQueues [$ queueName ];
395
386
}
396
387
397
- private function exchange (): \AMQPExchange
388
+ public function exchange (): \AMQPExchange
398
389
{
399
390
if (null === $ this ->amqpExchange ) {
400
391
$ this ->amqpExchange = $ this ->amqpFactory ->createExchange ($ this ->channel ());
401
- $ this ->amqpExchange ->setName ($ this ->exchangeConfiguration ['name ' ]);
402
- $ this ->amqpExchange ->setType ($ this ->exchangeConfiguration ['type ' ] ?? AMQP_EX_TYPE_FANOUT );
403
- $ this ->amqpExchange ->setFlags ($ this ->exchangeConfiguration ['flags ' ] ?? AMQP_DURABLE );
392
+ $ this ->amqpExchange ->setName ($ this ->exchangeOptions ['name ' ]);
393
+ $ this ->amqpExchange ->setType ($ this ->exchangeOptions ['type ' ] ?? AMQP_EX_TYPE_FANOUT );
394
+ $ this ->amqpExchange ->setFlags ($ this ->exchangeOptions ['flags ' ] ?? AMQP_DURABLE );
404
395
405
- if (isset ($ this ->exchangeConfiguration ['arguments ' ])) {
406
- $ this ->amqpExchange ->setArguments ($ this ->exchangeConfiguration ['arguments ' ]);
396
+ if (isset ($ this ->exchangeOptions ['arguments ' ])) {
397
+ $ this ->amqpExchange ->setArguments ($ this ->exchangeOptions ['arguments ' ]);
407
398
}
408
399
}
409
400
410
401
return $ this ->amqpExchange ;
411
402
}
412
403
413
- public function getConnectionConfiguration (): array
414
- {
415
- return $ this ->connectionConfiguration ;
416
- }
417
-
418
404
private function clear (): void
419
405
{
420
406
$ this ->amqpChannel = null ;
@@ -424,11 +410,11 @@ private function clear(): void
424
410
425
411
private function shouldSetup (): bool
426
412
{
427
- if (!\array_key_exists ('auto_setup ' , $ this ->connectionConfiguration )) {
413
+ if (!\array_key_exists ('auto_setup ' , $ this ->connectionOptions )) {
428
414
return true ;
429
415
}
430
416
431
- if (\in_array ($ this ->connectionConfiguration ['auto_setup ' ], [false , 'false ' ], true )) {
417
+ if (\in_array ($ this ->connectionOptions ['auto_setup ' ], [false , 'false ' ], true )) {
432
418
return false ;
433
419
}
434
420
@@ -437,7 +423,7 @@ private function shouldSetup(): bool
437
423
438
424
private function getDefaultPublishRoutingKey (): ?string
439
425
{
440
- return $ this ->exchangeConfiguration ['default_publish_routing_key ' ] ?? null ;
426
+ return $ this ->exchangeOptions ['default_publish_routing_key ' ] ?? null ;
441
427
}
442
428
443
429
public function purgeQueues ()
0 commit comments