diff --git a/DependencyInjection/Compiler/RegisterMonologChannels.php b/DependencyInjection/Compiler/RegisterMonologChannels.php index b180a2a..a7e69f9 100644 --- a/DependencyInjection/Compiler/RegisterMonologChannels.php +++ b/DependencyInjection/Compiler/RegisterMonologChannels.php @@ -5,6 +5,7 @@ use KoderHut\OnelogBundle\OneLog; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\DependencyInjection\Reference; /** @@ -19,7 +20,13 @@ class RegisterMonologChannels implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - if (!$container->getParameter('onelog.register_monolog_channels') || !$container->hasDefinition($container->getParameter('onelog.logger_service'))) { + try { + $monolog = $container->findDefinition('monolog.logger'); + } catch (ServiceNotFoundException $exc) { + $monolog = false; + } + + if (!$container->getParameter('onelog.register_monolog_channels') || false === $monolog) { return; } diff --git a/Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php b/Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php index 1620cb8..549e8b9 100644 --- a/Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php +++ b/Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php @@ -28,7 +28,28 @@ class RegisterMonologChannelsTest extends TestCase */ public function testExitEarlyIfNotEnabled() { - $container = $this->getContainer(['logger_service' => 'logger']); + $container = $this->getContainer(['logger_service' => 'monolog.logger']); + $container->compile(); + + $this->assertTrue($container->hasParameter('onelog.register_monolog_channels')); + $this->assertFalse($container->getParameter('onelog.register_monolog_channels')); + + $onelogDefinition = $container->getDefinition(OneLog::class); + $args = $onelogDefinition->getArguments(); + $methodCalls = $onelogDefinition->getMethodCalls(); + + $this->assertCount(1, $args); + $this->assertCount(0, $methodCalls); + } + + /** + * @test + */ + public function testExitEarlyIfMonologServiceIsNotDefined() + { + $container = $this->getContainer(['logger_service' => 'monolog.logger']); + $container->removeDefinition('monolog.logger'); + $container->compile(); $this->assertTrue($container->hasParameter('onelog.register_monolog_channels')); $this->assertFalse($container->getParameter('onelog.register_monolog_channels')); @@ -46,7 +67,8 @@ public function testExitEarlyIfNotEnabled() */ public function testRegisterAllMonologChannels() { - $container = $this->getContainer(['logger_service' => 'logger', 'register_monolog_channels' => true]); + $container = $this->getContainer(['logger_service' => 'monolog.logger', 'register_monolog_channels' => true]); + $container->compile(); $this->assertTrue($container->hasParameter('onelog.register_monolog_channels')); $this->assertTrue($container->getParameter('onelog.register_monolog_channels')); @@ -80,15 +102,13 @@ protected function getContainer(array $params) $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../../Resources/config')); $loader->load('onelog.xml'); - $container->addDefinitions(['logger' => $logger]); + $container->addDefinitions(['monolog.logger' => $logger]); $container->addDefinitions(['monolog.logger.test0' => $logger]); $container->addDefinitions(['monolog.logger.test1' => $logger]); $container->registerExtension(new OnelogExtension()); $container->loadFromExtension('onelog', $params); $container->addCompilerPass(new RegisterMonologChannels()); - $container->compile(); - return $container; } }