From 4b0f518771c2a7eb9b9d7f4806d7c1efafe0ef09 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 14 Jun 2012 22:17:58 +0200 Subject: [PATCH 1/2] Added the support of setter injection in the LoggerChannelPass --- .../Compiler/LoggerChannelPass.php | 11 ++++++ .../Compiler/LoggerChannelPassTest.php | 35 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/DependencyInjection/Compiler/LoggerChannelPass.php b/DependencyInjection/Compiler/LoggerChannelPass.php index 7bf16f4c..4789684f 100644 --- a/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/DependencyInjection/Compiler/LoggerChannelPass.php @@ -38,11 +38,22 @@ public function process(ContainerBuilder $container) $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) { + $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; + } } From 510be6a4c8eb3b765008febf7e906ed42f52aeb3 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 14 Jun 2012 22:22:34 +0200 Subject: [PATCH 2/2] Changed the formatting to remove an indentation level --- .../Compiler/LoggerChannelPass.php | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/DependencyInjection/Compiler/LoggerChannelPass.php b/DependencyInjection/Compiler/LoggerChannelPass.php index 4789684f..030a87f2 100644 --- a/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/DependencyInjection/Compiler/LoggerChannelPass.php @@ -34,27 +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); + if (empty($tag['channel']) || 'app' === $tag['channel']) { + continue; + } - foreach ($definition->getArguments() as $index => $argument) { - if ($argument instanceof Reference && 'logger' === (string) $argument) { - $definition->replaceArgument($index, new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict())); - } + $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) { - $calls[$i][1][$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) { + $calls[$i][1][$index] = new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict()); } } - $definition->setMethodCalls($calls); } + $definition->setMethodCalls($calls); } }