Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connection for doctrine event tag #363

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/DependencyInjection/SimpleThingsEntityAuditExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, if we harcode the parameter name here, we are making less flexible the replacing mechanism.
If in the future the parameter name used in these tags is renamed, we must replace this occurrence too.
I also think that the final users could not override these services if they need to use a different value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set this simplethings.entityaudit.connection parameter in the same SimpleThingsEntiyAuditExtension, so I think we do not have to afriaid about rename. Additionaly if users override this service then connection name must be set directly, oherwise expection will be throw.

We can also move it to the compiler pass. Then we can check whole services tagged by doctrine.event_listener and replace simplethings.entityaudit.connection parameter in connetion to propertly value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

symfony/symfony#40262
WDYT? @sonata-project/contributors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand, should we wait for symfony/symfony#40262 or can we merge this ?

Copy link
Member Author

@wbloszyk wbloszyk Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge this now. This code allow use param only for ower services.we can remove this code when symfony accept it and we bump doctrine-bridge.

Also they can accept it as feature and merge it in 5.2 version.

}
$definition->addTag('doctrine.event_subscriber', $attributes);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down