From 3179d40befac74856e025cf4b7342f016a100248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20B=C5=82oszyk?= Date: Wed, 24 Feb 2021 00:49:29 +0100 Subject: [PATCH] Fix connection for doctrine event tag (#363) * Fix connection for doctrine event tag * Fix code * Update src/DependencyInjection/SimpleThingsEntityAuditExtension.php Co-authored-by: Javier Spagnoletti Co-authored-by: Javier Spagnoletti --- .../SimpleThingsEntityAuditExtension.php | 21 +++++++++++++++++++ .../SimpleThingsEntityAuditExtensionTest.php | 7 +++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/DependencyInjection/SimpleThingsEntityAuditExtension.php b/src/DependencyInjection/SimpleThingsEntityAuditExtension.php index 8a71ff79..182bc298 100644 --- a/src/DependencyInjection/SimpleThingsEntityAuditExtension.php +++ b/src/DependencyInjection/SimpleThingsEntityAuditExtension.php @@ -50,5 +50,26 @@ public function load(array $configs, ContainerBuilder $container): void $container->setAlias('simplethings_entityaudit.'.$key, $service); } } + + $this->fixParametersFromDoctrineEventSubscriberTag($container, [ + 'simplethings_entityaudit.log_revisions_listener', + 'simplethings_entityaudit.create_schema_listener', + ]); + } + + private function fixParametersFromDoctrineEventSubscriberTag(ContainerBuilder $container, array $definitionNames): void + { + foreach ($definitionNames as $definitionName) { + $definition = $container->getDefinition($definitionName); + $tags = $definition->getTag('doctrine.event_subscriber'); + $definition->clearTag('doctrine.event_subscriber'); + + foreach ($tags as $attributes) { + if (isset($attributes['connection'])) { + $attributes['connection'] = (string) $container->getParameter('simplethings.entityaudit.connection'); + } + $definition->addTag('doctrine.event_subscriber', $attributes); + } + } } } diff --git a/tests/DependencyInjection/SimpleThingsEntityAuditExtensionTest.php b/tests/DependencyInjection/SimpleThingsEntityAuditExtensionTest.php index aa71a033..c056d48a 100644 --- a/tests/DependencyInjection/SimpleThingsEntityAuditExtensionTest.php +++ b/tests/DependencyInjection/SimpleThingsEntityAuditExtensionTest.php @@ -30,11 +30,11 @@ public function testItRegistersDefaultServices(): void $this->assertContainerBuilderHasService('simplethings_entityaudit.log_revisions_listener', 'SimpleThings\EntityAudit\EventListener\LogRevisionsListener'); $this->assertContainerBuilderHasServiceDefinitionWithArgument('simplethings_entityaudit.log_revisions_listener', 0, 'simplethings_entityaudit.manager'); - $this->assertContainerBuilderHasServiceDefinitionWithTag('simplethings_entityaudit.log_revisions_listener', 'doctrine.event_subscriber', ['connection' => '%simplethings.entityaudit.connection%']); + $this->assertContainerBuilderHasServiceDefinitionWithTag('simplethings_entityaudit.log_revisions_listener', 'doctrine.event_subscriber', ['connection' => 'default']); $this->assertContainerBuilderHasService('simplethings_entityaudit.create_schema_listener', 'SimpleThings\EntityAudit\EventListener\CreateSchemaListener'); $this->assertContainerBuilderHasServiceDefinitionWithArgument('simplethings_entityaudit.create_schema_listener', 0, 'simplethings_entityaudit.manager'); - $this->assertContainerBuilderHasServiceDefinitionWithTag('simplethings_entityaudit.create_schema_listener', 'doctrine.event_subscriber', ['connection' => '%simplethings.entityaudit.connection%']); + $this->assertContainerBuilderHasServiceDefinitionWithTag('simplethings_entityaudit.create_schema_listener', 'doctrine.event_subscriber', ['connection' => 'default']); $this->assertContainerBuilderHasService('simplethings_entityaudit.username_callable.token_storage', 'SimpleThings\EntityAudit\User\TokenStorageUsernameCallable'); $this->assertContainerBuilderHasServiceDefinitionWithArgument('simplethings_entityaudit.username_callable.token_storage', 0, 'service_container'); @@ -116,6 +116,9 @@ public function testItSetsConfiguredParameters(): void $this->assertContainerBuilderHasParameter('simplethings.entityaudit.revision_id_field_type', 'guid'); $this->assertContainerBuilderHasParameter('simplethings.entityaudit.revision_field_name', 'revision'); $this->assertContainerBuilderHasParameter('simplethings.entityaudit.revision_type_field_name', 'action'); + + $this->assertContainerBuilderHasServiceDefinitionWithTag('simplethings_entityaudit.log_revisions_listener', 'doctrine.event_subscriber', ['connection' => 'my_custom_connection']); + $this->assertContainerBuilderHasServiceDefinitionWithTag('simplethings_entityaudit.create_schema_listener', 'doctrine.event_subscriber', ['connection' => 'my_custom_connection']); } protected function getContainerExtensions(): array