Skip to content

Commit

Permalink
Merge pull request #1 from koderhut/fix-monolog-channel-registration
Browse files Browse the repository at this point in the history
Fix Monolog channels registering
  • Loading branch information
rendler-denis authored Jul 25, 2018
2 parents bbb5032 + 329e1ac commit 509f826
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
9 changes: 8 additions & 1 deletion DependencyInjection/Compiler/RegisterMonologChannels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

Expand Down
30 changes: 25 additions & 5 deletions Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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'));
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 509f826

Please sign in to comment.