diff --git a/src/AmqpBundle/DependencyInjection/Configuration.php b/src/AmqpBundle/DependencyInjection/Configuration.php index 17e9e00..045a6f4 100644 --- a/src/AmqpBundle/DependencyInjection/Configuration.php +++ b/src/AmqpBundle/DependencyInjection/Configuration.php @@ -26,6 +26,7 @@ public function getConfigTreeBuilder() ->children() ->booleanNode('debug')->defaultValue('%kernel.debug%')->end() ->booleanNode('event_dispatcher')->defaultTrue()->end() + ->booleanNode('prototype')->defaultFalse()->end() ->end(); $this->addConnections($rootNode); diff --git a/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php b/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php index 5b209a6..1258b5a 100644 --- a/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php +++ b/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php @@ -53,6 +53,10 @@ protected function loadConnections(ContainerBuilder $container, array $config) $connectionDefinition->setArguments([['heartbeat' => $connection['heartbeat']]]); + if ($config['prototype']) { + $connectionDefinition->setScope('prototype'); + } + if (!$connection['lazy']) { $connectionDefinition->addMethodCall('connect'); } @@ -102,6 +106,10 @@ protected function loadProducers(ContainerBuilder $container, array $config) $producerDefinition->setFactoryService('m6_web_amqp.producer_factory') ->setFactoryMethod('get'); + if ($config['prototype']) { + $producerDefinition->setScope('prototype'); + } + if ($lazy) { if (!method_exists($producerDefinition, 'setLazy')) { throw new \InvalidArgumentException('It\'s not possible to declare a service as lazy. Are you using Symfony 2.3?'); @@ -156,6 +164,10 @@ protected function loadConsumers(ContainerBuilder $container, array $config) $consumerDefinition->setFactoryService('m6_web_amqp.consumer_factory') ->setFactoryMethod('get'); + if ($config['prototype']) { + $consumerDefinition->setScope('prototype'); + } + if ($lazy) { if (!method_exists($consumerDefinition, 'setLazy')) { throw new \InvalidArgumentException('It\'s not possible to declare a service as lazy. Are you using Symfony 2.3?'); diff --git a/src/AmqpBundle/Tests/Fixtures/queue-arguments-config.yml b/src/AmqpBundle/Tests/Fixtures/queue-arguments-config.yml index a698e7e..ebf19bc 100644 --- a/src/AmqpBundle/Tests/Fixtures/queue-arguments-config.yml +++ b/src/AmqpBundle/Tests/Fixtures/queue-arguments-config.yml @@ -1,4 +1,5 @@ m6_web_amqp: + prototype: true connections: default: host: 'localhost' diff --git a/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php b/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php index 01e5886..86f501b 100644 --- a/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php +++ b/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php @@ -41,6 +41,8 @@ public function testQueueArgumentsConfig() $this ->boolean($container->has('m6_web_amqp.producer.producer_2')) ->isTrue() + ->string($container->getDefinition('m6_web_amqp.producer.producer_2')->getScope()) + ->isEqualTo('prototype') ->array($queueOptions = $container->getDefinition('m6_web_amqp.producer.producer_2')->getArgument(3)) ->hasSize(6) ->string($queueOptions['name']) @@ -63,6 +65,8 @@ public function testQueueArgumentsConfig() $this ->boolean($container->has('m6_web_amqp.consumer.consumer_1')) ->isTrue() + ->string($container->getDefinition('m6_web_amqp.consumer.consumer_1')->getScope()) + ->isEqualTo('prototype') ->array($queueOptions = $container->getDefinition('m6_web_amqp.consumer.consumer_1')->getArgument(3)) ->hasSize(7) ->string($queueOptions['name'])