diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index d25423c..8115858 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -47,6 +47,11 @@ private function addAmqpNode(NodeDefinition $root) ->isRequired() ->end() + ->scalarNode('logger_channel') + ->info('Logger channel to use when a logger is required') + ->defaultValue('amqp') + ->end() + ->arrayNode('connections') ->info('Connections to AMQP to use') ->useAttributeAsKey('name') diff --git a/src/DependencyInjection/WisemblyAmqpExtension.php b/src/DependencyInjection/WisemblyAmqpExtension.php index aa637a7..693f352 100644 --- a/src/DependencyInjection/WisemblyAmqpExtension.php +++ b/src/DependencyInjection/WisemblyAmqpExtension.php @@ -20,6 +20,8 @@ use Wisembly\AmqpBundle\Broker\PeclBroker; use Wisembly\AmqpBundle\Processor\ProcessFactory; +use Wisembly\AmqpBundle\Processor\ConsumerFactory; +use Wisembly\AmqpBundle\Processor\CommandProcessor; /** * This is the class that loads and manages your bundle configuration @@ -59,6 +61,18 @@ public function load(array $configs, ContainerBuilder $container) $container->registerForAutoconfiguration(BrokerInterface::class) ->addTag('wisembly.amqp.broker') ; + + // dynamically change the monolog.logger tag + foreach ([ConsumerFactory::class, CommandProcessor::class] as $service) { + $def = $container->getDefinition($service); + $tags = $def->getTag('monolog.logger'); + $def->clearTag('monolog.logger'); + + foreach ($tags as $attributes) { + $attributes['channel'] = $config['logger_channel']; + $def->addTag('monolog.logger', $attributes); + } + } } public function getConfig(array $configs): array diff --git a/tests/DependencyInjection/WisemblyAmqpExtensionTest.php b/tests/DependencyInjection/WisemblyAmqpExtensionTest.php index 847c32d..c78d00f 100644 --- a/tests/DependencyInjection/WisemblyAmqpExtensionTest.php +++ b/tests/DependencyInjection/WisemblyAmqpExtensionTest.php @@ -8,9 +8,13 @@ use Wisembly\AmqpBundle\GatesBag; use Wisembly\AmqpBundle\Connection; use Wisembly\AmqpBundle\UriConnection; + use Wisembly\AmqpBundle\BrokerInterface; use Wisembly\AmqpBundle\Broker\PeclBroker; +use Wisembly\AmqpBundle\Processor\ConsumerFactory; +use Wisembly\AmqpBundle\Processor\CommandProcessor; + class WisemblyAmqpExtensionTest extends TestCase { public function test_it_can_be_instantiated() @@ -116,7 +120,23 @@ public function test_it_picks_the_default_connection_if_none_specified() } } - private function getContainer(array $config = []) + public function test_it_changes_the_monolog_tag() + { + $container = $this->getContainer([ + 'logger_channel' => 'foo' + ]); + + foreach ([CommandProcessor::class, ConsumerFactory::class] as $service) { + $def = $container->getDefinition($service); + $tags = $def->getTag('monolog.logger'); + + foreach ($tags as $attributes) { + $this->assertSame('foo', $attributes['channel']); + } + } + } + + private function getContainer(array $config = []): ContainerBuilder { $broker = $this->prophesize(BrokerInterface::class)->reveal();