diff --git a/DependencyInjection/Compiler/LoggerChannelPass.php b/DependencyInjection/Compiler/LoggerChannelPass.php index 7bf16f4c..030a87f2 100644 --- a/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/DependencyInjection/Compiler/LoggerChannelPass.php @@ -34,16 +34,29 @@ public function process(ContainerBuilder $container) foreach ($container->findTaggedServiceIds('monolog.logger') as $id => $tags) { foreach ($tags as $tag) { - if (!empty($tag['channel']) && 'app' !== $tag['channel']) { - $definition = $container->getDefinition($id); - $loggerId = sprintf('monolog.logger.%s', $tag['channel']); - $this->createLogger($tag['channel'], $loggerId, $container); - foreach ($definition->getArguments() as $index => $argument) { + if (empty($tag['channel']) || 'app' === $tag['channel']) { + continue; + } + + $definition = $container->getDefinition($id); + $loggerId = sprintf('monolog.logger.%s', $tag['channel']); + $this->createLogger($tag['channel'], $loggerId, $container); + + foreach ($definition->getArguments() as $index => $argument) { + if ($argument instanceof Reference && 'logger' === (string) $argument) { + $definition->replaceArgument($index, new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict())); + } + } + + $calls = $definition->getMethodCalls(); + foreach ($calls as $i => $call) { + foreach ($call[1] as $index => $argument) { if ($argument instanceof Reference && 'logger' === (string) $argument) { - $definition->replaceArgument($index, new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict())); + $calls[$i][1][$index] = new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict()); } } } + $definition->setMethodCalls($calls); } } diff --git a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php index 3217e7ce..e6b670eb 100644 --- a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php @@ -52,6 +52,16 @@ public function testProcess() } } + public function testProcessSetters() + { + $container = $this->getContainerWithSetter(); + $this->assertTrue($container->hasDefinition('monolog.logger.test'), '->process adds a logger service for tagged service'); + + $service = $container->getDefinition('foo'); + $calls = $service->getMethodCalls(); + $this->assertEquals('monolog.logger.test', (string) $calls[0][1][0], '->process replaces the logger by the new one in setters'); + } + protected function getContainer() { $container = new ContainerBuilder(); @@ -92,4 +102,29 @@ protected function getContainer() return $container; } + + protected function getContainerWithSetter() + { + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config')); + $loader->load('monolog.xml'); + $definition = $container->getDefinition('monolog.logger_prototype'); + $container->set('monolog.handler.test', new Definition('%monolog.handler.null.class%', array (100, false))); + $definition->addMethodCall('pushHandler', array(new Reference('monolog.handler.test'))); + + // Channels + $service = new Definition('TestClass'); + $service->addTag('monolog.logger', array ('channel' => 'test')); + $service->addMethodCall('setLogger', array(new Reference('logger'))); + $container->setDefinition('foo', $service); + + $container->setParameter('monolog.handlers_to_channels', array()); + + $container->getCompilerPassConfig()->setOptimizationPasses(array()); + $container->getCompilerPassConfig()->setRemovingPasses(array()); + $container->addCompilerPass(new LoggerChannelPass()); + $container->compile(); + + return $container; + } }