Skip to content

Commit a0828d9

Browse files
committed
ISSUE-278:
- Added possibility for auto-wire monolog channels according to variable type-hint and name. Variable will have appropriated name to camel cased monolog channel service name: `monolog.logger.acme -> $monologLoggerAcme`. - Removed useless import `DefinitionDecorator`.
1 parent 7fbecb3 commit a0828d9

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

DependencyInjection/Compiler/LoggerChannelPass.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
15-
use Symfony\Component\DependencyInjection\Reference;
1616
use Symfony\Component\DependencyInjection\ChildDefinition;
17-
use Symfony\Component\DependencyInjection\ContainerBuilder;
1817
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19-
use Symfony\Component\DependencyInjection\DefinitionDecorator;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
2019
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
20+
use Symfony\Component\DependencyInjection\Reference;
2121

2222
/**
2323
* Replaces the default logger by another one with its own channel for tagged services.
@@ -28,6 +28,9 @@ class LoggerChannelPass implements CompilerPassInterface
2828
{
2929
protected $channels = array('app');
3030

31+
/**
32+
* {@inheritDoc}
33+
*/
3134
public function process(ContainerBuilder $container)
3235
{
3336
if (!$container->hasDefinition('monolog.logger')) {
@@ -105,11 +108,19 @@ public function process(ContainerBuilder $container)
105108
}
106109
}
107110

111+
/**
112+
* @return array
113+
*/
108114
public function getChannels()
109115
{
110116
return $this->channels;
111117
}
112118

119+
/**
120+
* @param array $configuration
121+
*
122+
* @return array
123+
*/
113124
protected function processChannels($configuration)
114125
{
115126
if (null === $configuration) {
@@ -123,6 +134,13 @@ protected function processChannels($configuration)
123134
return array_diff($this->channels, $configuration['elements']);
124135
}
125136

137+
/**
138+
* Create new logger from th monolog.logger_prototype
139+
*
140+
* @param string $channel
141+
* @param string $loggerId
142+
* @param ContainerBuilder $container
143+
*/
126144
protected function createLogger($channel, $loggerId, ContainerBuilder $container)
127145
{
128146
if (!in_array($channel, $this->channels)) {
@@ -131,6 +149,11 @@ protected function createLogger($channel, $loggerId, ContainerBuilder $container
131149
$container->setDefinition($loggerId, $logger);
132150
$this->channels[] = $channel;
133151
}
152+
153+
// Allows only for Symfony 4.2+
154+
if (\method_exists($container, 'registerAliasForArgument')) {
155+
$container->registerAliasForArgument($loggerId, LoggerInterface::class);
156+
}
134157
}
135158

136159
/**

Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Log\LoggerInterface;
16-
use Symfony\Component\DependencyInjection\Reference;
17-
use Symfony\Component\DependencyInjection\Definition;
18-
use Symfony\Component\DependencyInjection\ContainerBuilder;
1916
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
2017
use Symfony\Component\Config\FileLocator;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Definition;
2120
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21+
use Symfony\Component\DependencyInjection\Reference;
2222

2323
class LoggerChannelPassTest extends TestCase
2424
{
@@ -51,7 +51,19 @@ public function testProcess()
5151
}
5252
}
5353

54-
$this->assertNotNull($container->getDefinition('monolog.logger.manualchan'));
54+
$this->assertNotNull($container->getDefinition('monolog.logger.additional'));
55+
56+
if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) {
57+
return;
58+
}
59+
60+
$expectedChannels = \array_keys($expected);
61+
$expectedChannels[] = 'additional';
62+
63+
foreach ($expectedChannels as $channelName) {
64+
$aliasName = LoggerInterface::class.' $monologLogger'.\ucfirst($channelName);
65+
$this->assertTrue($container->hasAlias($aliasName), 'type-hinted alias should be exists for each logger channel');
66+
}
5567
}
5668

5769
public function testProcessSetters()
@@ -166,7 +178,7 @@ private function getContainer()
166178
$container->setDefinition($name, $service);
167179
}
168180

169-
$container->setParameter('monolog.additional_channels', array('manualchan'));
181+
$container->setParameter('monolog.additional_channels', array('additional'));
170182
$container->setParameter('monolog.handlers_to_channels', array(
171183
'monolog.handler.a' => array(
172184
'type' => 'inclusive',
@@ -202,7 +214,7 @@ private function getContainerWithSetter()
202214
$service->addMethodCall('setLogger', array(new Reference('logger')));
203215
$container->setDefinition('foo', $service);
204216

205-
$container->setParameter('monolog.additional_channels', array('manualchan'));
217+
$container->setParameter('monolog.additional_channels', array('additional'));
206218
$container->setParameter('monolog.handlers_to_channels', array());
207219

208220
$container->getCompilerPassConfig()->setOptimizationPasses(array());

0 commit comments

Comments
 (0)