Skip to content

Commit

Permalink
Fix MonologBundle 3.8 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed May 15, 2022
1 parent 71cba11 commit 7143f35
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ handler.
enable_annotations: false
# Configuration option used by both handlers.
process_psr_3_messages: true
process_psr_3_messages:
# Examples:
- false
- { enabled: false }
- { date_format: Y-m-d, remove_used_context_fields: true }
enabled: true
date_format: ~
remove_used_context_fields: ~
```

1. Enable the bundle in `config/bundles.php` by adding it to the array:
Expand Down
28 changes: 26 additions & 2 deletions src/DependencyInjection/BizkitLoggableCommandExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
final class BizkitLoggableCommandExtension extends ConfigurableExtension implements PrependExtensionInterface, CompilerPassInterface
{
/**
* @var bool
* @var array{enabled: bool, date_format?: ?string, remove_used_context_fields?: bool}
*/
private $processPsr3Messages;

Expand Down Expand Up @@ -149,20 +149,44 @@ private function setLoggableOutputConfigurator(ContainerBuilder $container): voi
*/
private function registerPsrLogMessageProcessor(ContainerBuilder $container): void
{
if (!$this->processPsr3Messages) {
if (!$this->processPsr3Messages['enabled']) {
return;
}

static $hasConstructorArguments;

if (!isset($hasConstructorArguments)) {
$r = (new \ReflectionClass(PsrLogMessageProcessor::class))->getConstructor();
$hasConstructorArguments = null !== $r && $r->getNumberOfParameters() > 0;
unset($r);
}

$monologProcessorId = 'monolog.processor.psr_log_message';
$monologProcessorArguments = [];
$processorId = 'bizkit_loggable_command.processor.psr_log_message';

$processorOptions = $this->processPsr3Messages;
unset($processorOptions['enabled']);

if ($processorOptions) {
if (!$hasConstructorArguments) {
throw new RuntimeException('Monolog 1.26 or higher is required for the "date_format" and "remove_used_context_fields" options to be used.');
}
$monologProcessorArguments = [
$processorOptions['date_format'] ?? null,
$processorOptions['remove_used_context_fields'] ?? false,
];
$monologProcessorId .= '.'.ContainerBuilder::hash($monologProcessorArguments);
}

if ($container->hasDefinition($monologProcessorId)) {
$container->setAlias($processorId, $monologProcessorId)
->setPublic(false)
;
} else {
$processor = new Definition(PsrLogMessageProcessor::class);
$processor->setPublic(false);
$processor->setArguments($monologProcessorArguments);
$container->setDefinition($processorId, $processor);
}
}
Expand Down
27 changes: 22 additions & 5 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,28 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()

->append(
$monologHandlersConfig->find('process_psr_3_messages')
->info('Configuration option used by both handlers.')
->defaultTrue()
)
->arrayNode('process_psr_3_messages')
->info('Configuration option used by both handlers.')
->example([
false,
['enabled' => false],
['date_format' => 'Y-m-d', 'remove_used_context_fields' => true],
])
->addDefaultsIfNotSet()
->beforeNormalization()
->ifTrue(static function ($v): bool {
return !\is_array($v);
})
->then(static function ($v): array {
return ['enabled' => $v];
})
->end()
->children()
->booleanNode('enabled')->defaultTrue()->end()
->scalarNode('date_format')->end()
->booleanNode('remove_used_context_fields')->end()
->end()
->end()
->end()

->validate()
Expand Down
89 changes: 89 additions & 0 deletions tests/DependencyInjection/BizkitLoggableCommandExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;

/**
Expand Down Expand Up @@ -324,4 +325,92 @@ public function testPsrLogMessageProcessorIsAliasedWhenTrueAndMonologServiceExis
$this->assertFalse($container->hasDefinition('bizkit_loggable_command.processor.psr_log_message'));
$this->assertTrue($container->hasAlias('bizkit_loggable_command.processor.psr_log_message'));
}

public function testExceptionIsThrownWhenPsrLogMessageProcessorDoesNotHaveConstructorArguments(): void
{
if ($this->psrLogMessageProcessorHasConstructorArguments()) {
$this->markTestSkipped('Monolog < 1.26 is needed.');
}

$container = new ContainerBuilder();
$container->registerExtension($loggableCommandExtension = new BizkitLoggableCommandExtension());

$loggableCommandExtension->load([[
'process_psr_3_messages' => [
'date_format' => 'Y-m-d',
],
]], $container);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(
'Monolog 1.26 or higher is required for the "date_format" and "remove_used_context_fields" options to be used.'
);

$loggableCommandExtension->process($container);
}

public function testPsrLogMessageProcessorIsRegisteredWhenTrueWithArgumentsAndMonologServiceDoesNotExist(): void
{
if (!$this->psrLogMessageProcessorHasConstructorArguments()) {
$this->markTestSkipped('Monolog >= 1.26 is needed.');
}

$container = new ContainerBuilder();
$container->registerExtension($loggableCommandExtension = new BizkitLoggableCommandExtension());

$container->register(
'monolog.processor.psr_log_message.'.ContainerBuilder::hash([null, true]),
PsrLogMessageProcessor::class
);

$loggableCommandExtension->load([[
'process_psr_3_messages' => [
'enabled' => true,
'date_format' => 'Y-m-d',
],
]], $container);
$loggableCommandExtension->process($container);

$this->assertTrue($container->hasDefinition('bizkit_loggable_command.processor.psr_log_message'));
$this->assertFalse($container->hasAlias('bizkit_loggable_command.processor.psr_log_message'));
}

public function testPsrLogMessageProcessorIsAliasedWhenTrueWithArgumentsAndMonologServiceExists(): void
{
if (!$this->psrLogMessageProcessorHasConstructorArguments()) {
$this->markTestSkipped('Monolog >= 1.26 is needed.');
}

$container = new ContainerBuilder();
$container->registerExtension($loggableCommandExtension = new BizkitLoggableCommandExtension());

$container->register(
'monolog.processor.psr_log_message.'.ContainerBuilder::hash([null, true]),
PsrLogMessageProcessor::class
);

$loggableCommandExtension->load([[
'process_psr_3_messages' => [
'enabled' => true,
'remove_used_context_fields' => true,
],
]], $container);
$loggableCommandExtension->process($container);

$this->assertFalse($container->hasDefinition('bizkit_loggable_command.processor.psr_log_message'));
$this->assertTrue($container->hasAlias('bizkit_loggable_command.processor.psr_log_message'));
}

private function psrLogMessageProcessorHasConstructorArguments(): bool
{
static $hasConstructorArguments;

if (!isset($hasConstructorArguments)) {
$r = (new \ReflectionClass(PsrLogMessageProcessor::class))->getConstructor();
$hasConstructorArguments = null !== $r && $r->getNumberOfParameters() > 0;
unset($r);
}

return $hasConstructorArguments;
}
}
49 changes: 48 additions & 1 deletion tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,54 @@ public function testDefaultConfig(): void
'extra_options' => [],
'enable_annotations' => false,
],
'process_psr_3_messages' => true,
'process_psr_3_messages' => [
'enabled' => true,
],
], $config);
}

public function testConfigWhenProcessPsr3MessageIsTrue(): void
{
$config = (new Processor())->processConfiguration(new Configuration(), [
'bizkit_loggable_command' => [
'process_psr_3_messages' => true,
],
]);

$this->assertArrayHasKey('process_psr_3_messages', $config);
$this->assertSame([
'enabled' => true,
], $config['process_psr_3_messages']);
}

public function testConfigWhenProcessPsr3MessageIsFalse(): void
{
$config = (new Processor())->processConfiguration(new Configuration(), [
'bizkit_loggable_command' => [
'process_psr_3_messages' => false,
],
]);

$this->assertArrayHasKey('process_psr_3_messages', $config);
$this->assertSame([
'enabled' => false,
], $config['process_psr_3_messages']);
}

public function testConfigWhenProcessPsr3MessageIsArray(): void
{
$config = (new Processor())->processConfiguration(new Configuration(), [
'bizkit_loggable_command' => [
'process_psr_3_messages' => [
'date_format' => 'Y-m-d',
],
],
]);

$this->assertArrayHasKey('process_psr_3_messages', $config);
$this->assertSame([
'date_format' => 'Y-m-d',
'enabled' => true,
], $config['process_psr_3_messages']);
}
}

0 comments on commit 7143f35

Please sign in to comment.