Skip to content

Commit fabf119

Browse files
authored
Merge pull request #315 from adrenalinkin/autowiring_monolog_channels
Symfony autowiring monolog channels #278
2 parents 3239d22 + 9fc40dc commit fabf119

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
## 3.5.0 (xxxx-xx-xx)
22

3+
* Added support for Monolog 2.0
34
* Added `sentry` type to use sentry 2.0 client
45
* Added `insightops` handler
5-
* Added support for Monolog 2.0
6+
* Added possibility for auto-wire monolog channel according to the type-hinted aliases, introduced in the Symfony 4.2
67

78
## 3.4.0 (2019-06-20)
89

DependencyInjection/Compiler/LoggerChannelPass.php

Lines changed: 28 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 = ['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 the 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,13 @@ 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+
$parameterName = $channel . 'Logger';
156+
157+
$container->registerAliasForArgument($loggerId, LoggerInterface::class, $parameterName);
158+
}
134159
}
135160

136161
/**

Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php

Lines changed: 21 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,22 @@ public function testProcess()
5151
}
5252
}
5353

54-
$this->assertNotNull($container->getDefinition('monolog.logger.manualchan'));
54+
$this->assertNotNull($container->getDefinition('monolog.logger.additional'));
55+
}
56+
57+
public function testTypeHintedAliasesExistForEachChannel()
58+
{
59+
if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) {
60+
$this->markTestSkipped('Need DependencyInjection 4.2+ to register type-hinted aliases for channels.');
61+
}
62+
63+
$container = $this->getContainer();
64+
$expectedChannels = ['test', 'foo', 'bar', 'additional'];
65+
66+
foreach ($expectedChannels as $channelName) {
67+
$aliasName = LoggerInterface::class.' $' .$channelName.'Logger';
68+
$this->assertTrue($container->hasAlias($aliasName), 'type-hinted alias should be exists for each logger channel');
69+
}
5570
}
5671

5772
public function testProcessSetters()
@@ -166,7 +181,7 @@ private function getContainer()
166181
$container->setDefinition($name, $service);
167182
}
168183

169-
$container->setParameter('monolog.additional_channels', ['manualchan']);
184+
$container->setParameter('monolog.additional_channels', ['additional']);
170185
$container->setParameter('monolog.handlers_to_channels', [
171186
'monolog.handler.a' => [
172187
'type' => 'inclusive',
@@ -202,7 +217,7 @@ private function getContainerWithSetter()
202217
$service->addMethodCall('setLogger', [new Reference('logger')]);
203218
$container->setDefinition('foo', $service);
204219

205-
$container->setParameter('monolog.additional_channels', ['manualchan']);
220+
$container->setParameter('monolog.additional_channels', ['additional']);
206221
$container->setParameter('monolog.handlers_to_channels', []);
207222

208223
$container->getCompilerPassConfig()->setOptimizationPasses([]);

0 commit comments

Comments
 (0)