Skip to content

Commit 4cb2abb

Browse files
committed
Merge pull request #157 from wpottier/add_nested_option_for_handler
Add a nested option on handler config definition
2 parents d4d933a + 0502453 commit 4cb2abb

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ public function getConfigTreeBuilder()
601601
->end()
602602
->end()
603603
->scalarNode('formatter')->end()
604+
->booleanNode('nested')->defaultFalse()->end()
604605
->end()
605606
->validate()
606607
->ifTrue(function ($v) { return 'service' === $v['type'] && !empty($v['formatter']); })

DependencyInjection/MonologExtension.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
312312
$handler['passthru_level'] = $this->levelToMonologConst($handler['passthru_level']);
313313
}
314314
$nestedHandlerId = $this->getHandlerId($handler['handler']);
315-
$this->nestedHandlers[] = $nestedHandlerId;
315+
$this->markNestedHandler($nestedHandlerId);
316316

317317
if (isset($handler['activation_strategy'])) {
318318
$activation = new Reference($handler['activation_strategy']);
@@ -343,7 +343,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
343343
}
344344

345345
$nestedHandlerId = $this->getHandlerId($handler['handler']);
346-
$this->nestedHandlers[] = $nestedHandlerId;
346+
$this->markNestedHandler($nestedHandlerId);
347347
$minLevelOrList = !empty($handler['accepted_levels']) ? $handler['accepted_levels'] : $handler['min_level'];
348348

349349
$definition->setArguments(array(
@@ -356,7 +356,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
356356

357357
case 'buffer':
358358
$nestedHandlerId = $this->getHandlerId($handler['handler']);
359-
$this->nestedHandlers[] = $nestedHandlerId;
359+
$this->markNestedHandler($nestedHandlerId);
360360

361361
$definition->setArguments(array(
362362
new Reference($nestedHandlerId),
@@ -372,7 +372,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
372372
$references = array();
373373
foreach ($handler['members'] as $nestedHandler) {
374374
$nestedHandlerId = $this->getHandlerId($nestedHandler);
375-
$this->nestedHandlers[] = $nestedHandlerId;
375+
$this->markNestedHandler($nestedHandlerId);
376376
$references[] = new Reference($nestedHandlerId);
377377
}
378378

@@ -642,6 +642,10 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
642642
throw new \InvalidArgumentException(sprintf('Invalid handler type "%s" given for handler "%s"', $handler['type'], $name));
643643
}
644644

645+
if (!empty($handler['nested']) && true === $handler['nested']) {
646+
$this->markNestedHandler($handlerId);
647+
}
648+
645649
if (!empty($handler['formatter'])) {
646650
$definition->addMethodCall('setFormatter', array(new Reference($handler['formatter'])));
647651
}
@@ -650,6 +654,15 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
650654
return $handlerId;
651655
}
652656

657+
private function markNestedHandler($nestedHandlerId)
658+
{
659+
if (in_array($nestedHandlerId, $this->nestedHandlers)) {
660+
return;
661+
}
662+
663+
$this->nestedHandlers[] = $nestedHandlerId;
664+
}
665+
653666
private function getHandlerId($name)
654667
{
655668
return sprintf('monolog.handler.%s', $name);

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testProcessSimpleCase()
3636
$this->assertArrayHasKey('foobar', $config['handlers']);
3737
$this->assertEquals('stream', $config['handlers']['foobar']['type']);
3838
$this->assertEquals('/foo/bar', $config['handlers']['foobar']['path']);
39+
$this->assertFalse($config['handlers']['foobar']['nested']);
3940
}
4041

4142
public function provideProcessStringChannels()
@@ -321,6 +322,20 @@ public function testWithFilePermission()
321322
$this->assertSame(0777, $config['handlers']['bar']['file_permission']);
322323
}
323324

325+
public function testWithNestedHandler()
326+
{
327+
$configs = array(
328+
array(
329+
'handlers' => array('foobar' => array('type' => 'stream', 'path' => '/foo/bar', 'nested' => true))
330+
)
331+
);
332+
333+
$config = $this->process($configs);
334+
335+
336+
$this->assertTrue($config['handlers']['foobar']['nested']);
337+
}
338+
324339
/**
325340
* Processes an array of configurations and returns a compiled version.
326341
*

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ public function testLoadWithCustomValues()
5050
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666));
5151
}
5252

53+
public function testLoadWithNestedHandler()
54+
{
55+
$container = $this->getContainer(array(array('handlers' => array(
56+
'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666'),
57+
'nested' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666', 'nested' => true)
58+
))));
59+
$this->assertTrue($container->hasDefinition('monolog.logger'));
60+
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
61+
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
62+
63+
$logger = $container->getDefinition('monolog.logger');
64+
// Nested handler must not be pushed to logger
65+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
66+
67+
$handler = $container->getDefinition('monolog.handler.custom');
68+
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
69+
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666));
70+
}
71+
5372
/**
5473
* @expectedException InvalidArgumentException
5574
*/

0 commit comments

Comments
 (0)