Skip to content

Commit 0b5046d

Browse files
committed
Merge pull request #14 from symfony/channel_setters
Channel setters
2 parents 00769da + 510be6a commit 0b5046d

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

DependencyInjection/Compiler/LoggerChannelPass.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,29 @@ public function process(ContainerBuilder $container)
3434

3535
foreach ($container->findTaggedServiceIds('monolog.logger') as $id => $tags) {
3636
foreach ($tags as $tag) {
37-
if (!empty($tag['channel']) && 'app' !== $tag['channel']) {
38-
$definition = $container->getDefinition($id);
39-
$loggerId = sprintf('monolog.logger.%s', $tag['channel']);
40-
$this->createLogger($tag['channel'], $loggerId, $container);
41-
foreach ($definition->getArguments() as $index => $argument) {
37+
if (empty($tag['channel']) || 'app' === $tag['channel']) {
38+
continue;
39+
}
40+
41+
$definition = $container->getDefinition($id);
42+
$loggerId = sprintf('monolog.logger.%s', $tag['channel']);
43+
$this->createLogger($tag['channel'], $loggerId, $container);
44+
45+
foreach ($definition->getArguments() as $index => $argument) {
46+
if ($argument instanceof Reference && 'logger' === (string) $argument) {
47+
$definition->replaceArgument($index, new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict()));
48+
}
49+
}
50+
51+
$calls = $definition->getMethodCalls();
52+
foreach ($calls as $i => $call) {
53+
foreach ($call[1] as $index => $argument) {
4254
if ($argument instanceof Reference && 'logger' === (string) $argument) {
43-
$definition->replaceArgument($index, new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict()));
55+
$calls[$i][1][$index] = new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict());
4456
}
4557
}
4658
}
59+
$definition->setMethodCalls($calls);
4760
}
4861
}
4962

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)