From a0828d946563d269e1d19d5cb57b82a647b2bc4e Mon Sep 17 00:00:00 2001 From: Viktor Linkin Date: Sat, 10 Aug 2019 15:25:13 +0300 Subject: [PATCH 1/5] 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`. --- .../Compiler/LoggerChannelPass.php | 29 +++++++++++++++++-- .../Compiler/LoggerChannelPassTest.php | 24 +++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/DependencyInjection/Compiler/LoggerChannelPass.php b/DependencyInjection/Compiler/LoggerChannelPass.php index 51a21d5e..208d589f 100644 --- a/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/DependencyInjection/Compiler/LoggerChannelPass.php @@ -11,13 +11,13 @@ namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Argument\BoundArgument; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ChildDefinition; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\DefinitionDecorator; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; +use Symfony\Component\DependencyInjection\Reference; /** * Replaces the default logger by another one with its own channel for tagged services. @@ -28,6 +28,9 @@ class LoggerChannelPass implements CompilerPassInterface { protected $channels = array('app'); + /** + * {@inheritDoc} + */ public function process(ContainerBuilder $container) { if (!$container->hasDefinition('monolog.logger')) { @@ -105,11 +108,19 @@ public function process(ContainerBuilder $container) } } + /** + * @return array + */ public function getChannels() { return $this->channels; } + /** + * @param array $configuration + * + * @return array + */ protected function processChannels($configuration) { if (null === $configuration) { @@ -123,6 +134,13 @@ protected function processChannels($configuration) return array_diff($this->channels, $configuration['elements']); } + /** + * Create new logger from th monolog.logger_prototype + * + * @param string $channel + * @param string $loggerId + * @param ContainerBuilder $container + */ protected function createLogger($channel, $loggerId, ContainerBuilder $container) { if (!in_array($channel, $this->channels)) { @@ -131,6 +149,11 @@ protected function createLogger($channel, $loggerId, ContainerBuilder $container $container->setDefinition($loggerId, $logger); $this->channels[] = $channel; } + + // Allows only for Symfony 4.2+ + if (\method_exists($container, 'registerAliasForArgument')) { + $container->registerAliasForArgument($loggerId, LoggerInterface::class); + } } /** diff --git a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php index 7743cbf7..2129abab 100644 --- a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php @@ -13,12 +13,12 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; -use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Reference; class LoggerChannelPassTest extends TestCase { @@ -51,7 +51,19 @@ public function testProcess() } } - $this->assertNotNull($container->getDefinition('monolog.logger.manualchan')); + $this->assertNotNull($container->getDefinition('monolog.logger.additional')); + + if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) { + return; + } + + $expectedChannels = \array_keys($expected); + $expectedChannels[] = 'additional'; + + foreach ($expectedChannels as $channelName) { + $aliasName = LoggerInterface::class.' $monologLogger'.\ucfirst($channelName); + $this->assertTrue($container->hasAlias($aliasName), 'type-hinted alias should be exists for each logger channel'); + } } public function testProcessSetters() @@ -166,7 +178,7 @@ private function getContainer() $container->setDefinition($name, $service); } - $container->setParameter('monolog.additional_channels', array('manualchan')); + $container->setParameter('monolog.additional_channels', array('additional')); $container->setParameter('monolog.handlers_to_channels', array( 'monolog.handler.a' => array( 'type' => 'inclusive', @@ -202,7 +214,7 @@ private function getContainerWithSetter() $service->addMethodCall('setLogger', array(new Reference('logger'))); $container->setDefinition('foo', $service); - $container->setParameter('monolog.additional_channels', array('manualchan')); + $container->setParameter('monolog.additional_channels', array('additional')); $container->setParameter('monolog.handlers_to_channels', array()); $container->getCompilerPassConfig()->setOptimizationPasses(array()); From 2139a5ba2d80a518f47df51cea3f78a97bd25d0d Mon Sep 17 00:00:00 2001 From: Viktor Linkin Date: Tue, 13 Aug 2019 15:55:11 +0300 Subject: [PATCH 2/5] ISSUE-278: - Updated changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4d8c2c2..db07f6b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 3.5.0 (xxxx-xx-xx) * Add `sentry` type to use sentry 2.0 client +* Added possibility for auto-wire monolog channel according to the type-hinted aliases, introduced in the Symfony 4.2 ## 3.4.0 (2019-06-20) From c0b0a9b1a726c0a7b035e91790ec8ca4ad0bd3d7 Mon Sep 17 00:00:00 2001 From: Viktor Linkin Date: Sat, 17 Aug 2019 16:48:33 +0300 Subject: [PATCH 3/5] ISSUE-278: - Updated naming strategy. Channel 'test' become '$testLogger' and etc. --- DependencyInjection/Compiler/LoggerChannelPass.php | 4 +++- Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Compiler/LoggerChannelPass.php b/DependencyInjection/Compiler/LoggerChannelPass.php index 208d589f..1245630b 100644 --- a/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/DependencyInjection/Compiler/LoggerChannelPass.php @@ -152,7 +152,9 @@ protected function createLogger($channel, $loggerId, ContainerBuilder $container // Allows only for Symfony 4.2+ if (\method_exists($container, 'registerAliasForArgument')) { - $container->registerAliasForArgument($loggerId, LoggerInterface::class); + $parameterName = $channel . 'Logger'; + + $container->registerAliasForArgument($loggerId, LoggerInterface::class, $parameterName); } } diff --git a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php index 2129abab..aff3c2e5 100644 --- a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php @@ -53,6 +53,7 @@ public function testProcess() $this->assertNotNull($container->getDefinition('monolog.logger.additional')); + // test below acceptable only for Symfony 4.2+ if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) { return; } @@ -61,7 +62,7 @@ public function testProcess() $expectedChannels[] = 'additional'; foreach ($expectedChannels as $channelName) { - $aliasName = LoggerInterface::class.' $monologLogger'.\ucfirst($channelName); + $aliasName = LoggerInterface::class.' $' .$channelName.'Logger'; $this->assertTrue($container->hasAlias($aliasName), 'type-hinted alias should be exists for each logger channel'); } } From 4120c49dfe96a2261dc88fac1ef3caac88c96b07 Mon Sep 17 00:00:00 2001 From: Viktor Linkin Date: Thu, 29 Aug 2019 21:32:15 +0300 Subject: [PATCH 4/5] ISSUE-278: - Fixed typo in the `LoggerChannelPass`. - Added separate test-case for the type-hinted aliases for each logger channel. --- DependencyInjection/Compiler/LoggerChannelPass.php | 2 +- .../Compiler/LoggerChannelPassTest.php | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/DependencyInjection/Compiler/LoggerChannelPass.php b/DependencyInjection/Compiler/LoggerChannelPass.php index 1245630b..eb165aeb 100644 --- a/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/DependencyInjection/Compiler/LoggerChannelPass.php @@ -135,7 +135,7 @@ protected function processChannels($configuration) } /** - * Create new logger from th monolog.logger_prototype + * Create new logger from the monolog.logger_prototype * * @param string $channel * @param string $loggerId diff --git a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php index aff3c2e5..0203bb0d 100644 --- a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php @@ -52,14 +52,16 @@ public function testProcess() } $this->assertNotNull($container->getDefinition('monolog.logger.additional')); + } - // test below acceptable only for Symfony 4.2+ + public function testTypeHintedAliasesExistForEachChannel() + { if (!\method_exists(ContainerBuilder::class, 'registerAliasForArgument')) { - return; + $this->markTestSkipped('Need DependencyInjection 4.2+ to register type-hinted aliases for channels.'); } - $expectedChannels = \array_keys($expected); - $expectedChannels[] = 'additional'; + $container = $this->getContainer(); + $expectedChannels = array('test', 'foo', 'bar', 'additional'); foreach ($expectedChannels as $channelName) { $aliasName = LoggerInterface::class.' $' .$channelName.'Logger'; From 9fc40dcbe130f00b518fe70fa1ac4f8a76650d84 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 2 Sep 2019 12:12:51 +0200 Subject: [PATCH 5/5] Update to use short array syntax --- Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php index d6b5ee3a..7cc7323c 100644 --- a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php @@ -61,7 +61,7 @@ public function testTypeHintedAliasesExistForEachChannel() } $container = $this->getContainer(); - $expectedChannels = array('test', 'foo', 'bar', 'additional'); + $expectedChannels = ['test', 'foo', 'bar', 'additional']; foreach ($expectedChannels as $channelName) { $aliasName = LoggerInterface::class.' $' .$channelName.'Logger';