From 75c1ca29d656cafde78519847db8a14791ae4386 Mon Sep 17 00:00:00 2001 From: Nathan Robertson Date: Wed, 15 Sep 2021 15:23:31 +1000 Subject: [PATCH 1/4] Add producer support for default_routing_key --- DependencyInjection/Configuration.php | 1 + DependencyInjection/OldSoundRabbitMqExtension.php | 2 ++ RabbitMq/Producer.php | 13 +++++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index a1c2ce20..7f5bc578 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -124,6 +124,7 @@ protected function addProducers(ArrayNodeDefinition $node) ->scalarNode('class')->defaultValue('%old_sound_rabbit_mq.producer.class%')->end() ->scalarNode('enable_logger')->defaultFalse()->end() ->scalarNode('service_alias')->defaultValue(null)->end() + ->scalarNode('default_routing_key')->defaultValue('')->end() ->end() ->end() ->end() diff --git a/DependencyInjection/OldSoundRabbitMqExtension.php b/DependencyInjection/OldSoundRabbitMqExtension.php index 5a3b7022..b0386bcc 100644 --- a/DependencyInjection/OldSoundRabbitMqExtension.php +++ b/DependencyInjection/OldSoundRabbitMqExtension.php @@ -185,6 +185,8 @@ protected function loadProducers() ->registerAliasForArgument($producerServiceName, $producer['class'], $argName) ->setPublic(false); } + + $definition->addMethodCall('setDefaultRoutingKey', $producer['default_routing_key']); } } else { foreach ($this->config['producers'] as $key => $producer) { diff --git a/RabbitMq/Producer.php b/RabbitMq/Producer.php index 446bb611..d1849a3e 100644 --- a/RabbitMq/Producer.php +++ b/RabbitMq/Producer.php @@ -12,6 +12,7 @@ class Producer extends BaseAmqp implements ProducerInterface { protected $contentType = 'text/plain'; protected $deliveryMode = 2; + protected $defaultRoutingKey = ''; public function setContentType($contentType) { @@ -27,6 +28,13 @@ public function setDeliveryMode($deliveryMode) return $this; } + public function setDefaultRoutingKey($defaultRoutingKey) + { + $this->defaultRoutingKey = $defaultRoutingKey; + + return $this; + } + protected function getBasicProperties() { return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode); @@ -40,7 +48,7 @@ protected function getBasicProperties() * @param array $additionalProperties * @param array $headers */ - public function publish($msgBody, $routingKey = '', $additionalProperties = array(), array $headers = null) + public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null) { if ($this->autoSetupFabric) { $this->setupFabric(); @@ -53,7 +61,8 @@ public function publish($msgBody, $routingKey = '', $additionalProperties = arra $msg->set('application_headers', $headersTable); } - $this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$routingKey); + $real_routingKey = $routingKey != null ? $routingKey : $this->defaultRoutingKey; + $this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey); $this->logger->debug('AMQP message published', array( 'amqp' => array( 'body' => $msgBody, From 001f71537f9cca4cf18dcac9fdeeed87c7ac26ec Mon Sep 17 00:00:00 2001 From: Nathan Robertson Date: Wed, 15 Sep 2021 15:44:32 +1000 Subject: [PATCH 2/4] Pass method call argument wrapped in array() --- DependencyInjection/OldSoundRabbitMqExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DependencyInjection/OldSoundRabbitMqExtension.php b/DependencyInjection/OldSoundRabbitMqExtension.php index b0386bcc..c56e9c84 100644 --- a/DependencyInjection/OldSoundRabbitMqExtension.php +++ b/DependencyInjection/OldSoundRabbitMqExtension.php @@ -186,7 +186,7 @@ protected function loadProducers() ->setPublic(false); } - $definition->addMethodCall('setDefaultRoutingKey', $producer['default_routing_key']); + $definition->addMethodCall('setDefaultRoutingKey', array($producer['default_routing_key'])); } } else { foreach ($this->config['producers'] as $key => $producer) { From 7b36702216d2adffa7e6377d148cc6d6c915defc Mon Sep 17 00:00:00 2001 From: Nathan Robertson Date: Wed, 15 Sep 2021 16:19:20 +1000 Subject: [PATCH 3/4] Use !== rather than != (scrutinizer output) --- RabbitMq/Producer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RabbitMq/Producer.php b/RabbitMq/Producer.php index d1849a3e..49f1c07f 100644 --- a/RabbitMq/Producer.php +++ b/RabbitMq/Producer.php @@ -61,7 +61,7 @@ public function publish($msgBody, $routingKey = null, $additionalProperties = ar $msg->set('application_headers', $headersTable); } - $real_routingKey = $routingKey != null ? $routingKey : $this->defaultRoutingKey; + $real_routingKey = $routingKey !== null ? $routingKey : $this->defaultRoutingKey; $this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey); $this->logger->debug('AMQP message published', array( 'amqp' => array( From c7aeb5ee3a04233b37fc9e75f6679e27967623f4 Mon Sep 17 00:00:00 2001 From: Nathan Robertson Date: Wed, 15 Sep 2021 17:46:54 +1000 Subject: [PATCH 4/4] Fix tests --- .../DependencyInjection/OldSoundRabbitMqExtensionTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php b/Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php index 5406db03..d1e7583b 100644 --- a/Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php +++ b/Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php @@ -313,6 +313,10 @@ public function testFooProducerDefinition() 'declare' => false, ) ) + ), + array( + 'setDefaultRoutingKey', + array('') ) ), $definition->getMethodCalls() @@ -394,6 +398,10 @@ public function testDefaultProducerDefinition() 'declare' => false, ) ) + ), + array( + 'setDefaultRoutingKey', + array('') ) ), $definition->getMethodCalls()