Skip to content

Commit 4b0f518

Browse files
committed
Added the support of setter injection in the LoggerChannelPass
1 parent 00769da commit 4b0f518

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

DependencyInjection/Compiler/LoggerChannelPass.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,22 @@ public function process(ContainerBuilder $container)
3838
$definition = $container->getDefinition($id);
3939
$loggerId = sprintf('monolog.logger.%s', $tag['channel']);
4040
$this->createLogger($tag['channel'], $loggerId, $container);
41+
4142
foreach ($definition->getArguments() as $index => $argument) {
4243
if ($argument instanceof Reference && 'logger' === (string) $argument) {
4344
$definition->replaceArgument($index, new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict()));
4445
}
4546
}
47+
48+
$calls = $definition->getMethodCalls();
49+
foreach ($calls as $i => $call) {
50+
foreach ($call[1] as $index => $argument) {
51+
if ($argument instanceof Reference && 'logger' === (string) $argument) {
52+
$calls[$i][1][$index] = new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict());
53+
}
54+
}
55+
}
56+
$definition->setMethodCalls($calls);
4657
}
4758
}
4859
}

Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ public function testProcess()
5252
}
5353
}
5454

55+
public function testProcessSetters()
56+
{
57+
$container = $this->getContainerWithSetter();
58+
$this->assertTrue($container->hasDefinition('monolog.logger.test'), '->process adds a logger service for tagged service');
59+
60+
$service = $container->getDefinition('foo');
61+
$calls = $service->getMethodCalls();
62+
$this->assertEquals('monolog.logger.test', (string) $calls[0][1][0], '->process replaces the logger by the new one in setters');
63+
}
64+
5565
protected function getContainer()
5666
{
5767
$container = new ContainerBuilder();
@@ -92,4 +102,29 @@ protected function getContainer()
92102

93103
return $container;
94104
}
105+
106+
protected function getContainerWithSetter()
107+
{
108+
$container = new ContainerBuilder();
109+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
110+
$loader->load('monolog.xml');
111+
$definition = $container->getDefinition('monolog.logger_prototype');
112+
$container->set('monolog.handler.test', new Definition('%monolog.handler.null.class%', array (100, false)));
113+
$definition->addMethodCall('pushHandler', array(new Reference('monolog.handler.test')));
114+
115+
// Channels
116+
$service = new Definition('TestClass');
117+
$service->addTag('monolog.logger', array ('channel' => 'test'));
118+
$service->addMethodCall('setLogger', array(new Reference('logger')));
119+
$container->setDefinition('foo', $service);
120+
121+
$container->setParameter('monolog.handlers_to_channels', array());
122+
123+
$container->getCompilerPassConfig()->setOptimizationPasses(array());
124+
$container->getCompilerPassConfig()->setRemovingPasses(array());
125+
$container->addCompilerPass(new LoggerChannelPass());
126+
$container->compile();
127+
128+
return $container;
129+
}
95130
}

0 commit comments

Comments
 (0)