Permalink
Browse files

Merge pull request #14 from symfony/channel_setters

Channel setters
  • Loading branch information...
Seldaek committed Jun 14, 2012
2 parents 00769da + 510be6a commit 0b5046d3d1ef93cd59ceabfa37de2665f5118c7f
@@ -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);
}
}
@@ -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;
+ }
}

0 comments on commit 0b5046d

Please sign in to comment.