From c9d500f1e6287a3e2aa85ad9753e7898a9adecfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Merlet?= Date: Sun, 24 Mar 2024 20:42:52 +0100 Subject: [PATCH 1/3] Add new SchemaConfigurationListener --- sources/ORM/SchemaConfigurationListener.php | 29 +++++++++++++ tests/ORM/SchemaConfigurationListenerTest.php | 41 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 sources/ORM/SchemaConfigurationListener.php create mode 100644 tests/ORM/SchemaConfigurationListenerTest.php diff --git a/sources/ORM/SchemaConfigurationListener.php b/sources/ORM/SchemaConfigurationListener.php new file mode 100644 index 0000000..212dd03 --- /dev/null +++ b/sources/ORM/SchemaConfigurationListener.php @@ -0,0 +1,29 @@ +configurators = $configurators; + } + + public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs): void + { + $schema = $eventArgs->getSchema(); + $connection = $eventArgs->getEntityManager()->getConnection(); + + foreach ($this->configurators as $configurator) { + $configurator->configureSchema($schema, $connection); + } + } +} diff --git a/tests/ORM/SchemaConfigurationListenerTest.php b/tests/ORM/SchemaConfigurationListenerTest.php new file mode 100644 index 0000000..18d9c65 --- /dev/null +++ b/tests/ORM/SchemaConfigurationListenerTest.php @@ -0,0 +1,41 @@ + 'sqlite:///:memory:']); + + $entityManager = $this->prophesize(EntityManagerInterface::class); + $entityManager->getConnection()->willReturn($connection); + + $configurator = $this->prophesize(SchemaConfigurator::class); + $configurator->configureSchema($schema, $connection)->shouldBeCalled(); + + $eventManager = new EventManager(); + $eventManager->addEventListener( + ToolEvents::postGenerateSchema, + new SchemaConfigurationListener($configurator->reveal()) + ); + $eventManager->dispatchEvent( + ToolEvents::postGenerateSchema, + new GenerateSchemaEventArgs($entityManager->reveal(), $schema) + ); + } +} From f4d142ed56a26d1bd3f2d6390a6c896a93a52e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Merlet?= Date: Sun, 24 Mar 2024 20:44:20 +0100 Subject: [PATCH 2/3] Replace subscriber by listener in Bundle extension --- sources/Bundle/FeatureTogglesExtension.php | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/sources/Bundle/FeatureTogglesExtension.php b/sources/Bundle/FeatureTogglesExtension.php index bbbe1a8..4ee7f66 100644 --- a/sources/Bundle/FeatureTogglesExtension.php +++ b/sources/Bundle/FeatureTogglesExtension.php @@ -2,6 +2,7 @@ namespace Trompette\FeatureToggles\Bundle; +use Doctrine\ORM\Tools\ToolEvents; use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -15,7 +16,7 @@ use Trompette\FeatureToggles\FeatureDefinition; use Trompette\FeatureToggles\FeatureRegistry; use Trompette\FeatureToggles\OnOffStrategy\OnOff; -use Trompette\FeatureToggles\ORM\SchemaSubscriber; +use Trompette\FeatureToggles\ORM\SchemaConfigurationListener; use Trompette\FeatureToggles\PercentageStrategy\Percentage; use Trompette\FeatureToggles\ToggleRouter; use Trompette\FeatureToggles\ToggleRouterInterface; @@ -31,7 +32,7 @@ public function load(array $configs, ContainerBuilder $container): void $this->defineTogglingStrategies($config['doctrine_dbal_connection'], $container); $this->defineToggleRouter($container); $this->defineConsoleCommands($container); - $this->defineDoctrineEventSubscriber($container); + $this->defineDoctrineEventListener($container); } /** @@ -118,14 +119,16 @@ private function defineConsoleCommands(ContainerBuilder $container): void ; } - private function defineDoctrineEventSubscriber(ContainerBuilder $container): void + private function defineDoctrineEventListener(ContainerBuilder $container): void { - $container - ->register(SchemaSubscriber::class, SchemaSubscriber::class) - ->addArgument(new Reference(OnOffStrategyConfigurationRepository::class)) - ->addArgument(new Reference(WhitelistStrategyConfigurationRepository::class)) - ->addArgument(new Reference(PercentageStrategyConfigurationRepository::class)) - ->addTag('doctrine.event_subscriber') - ; + if (class_exists(ToolEvents::class)) { + $container + ->register(SchemaConfigurationListener::class, SchemaConfigurationListener::class) + ->addArgument(new Reference(OnOffStrategyConfigurationRepository::class)) + ->addArgument(new Reference(WhitelistStrategyConfigurationRepository::class)) + ->addArgument(new Reference(PercentageStrategyConfigurationRepository::class)) + ->addTag('doctrine.event_listener', ['event' => ToolEvents::postGenerateSchema]) + ; + } } } From 7bc1f5327f499fc7487a8176485db68f3b76c579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Merlet?= Date: Sun, 24 Mar 2024 20:56:28 +0100 Subject: [PATCH 3/3] Remove now unused SchemaSubscriber --- sources/ORM/SchemaSubscriber.php | 37 ------------------------------ tests/ORM/SchemaSubscriberTest.php | 37 ------------------------------ 2 files changed, 74 deletions(-) delete mode 100644 sources/ORM/SchemaSubscriber.php delete mode 100644 tests/ORM/SchemaSubscriberTest.php diff --git a/sources/ORM/SchemaSubscriber.php b/sources/ORM/SchemaSubscriber.php deleted file mode 100644 index c2c40fa..0000000 --- a/sources/ORM/SchemaSubscriber.php +++ /dev/null @@ -1,37 +0,0 @@ -configurators = $configurators; - } - - public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs): void - { - $schema = $eventArgs->getSchema(); - $connection = $eventArgs->getEntityManager()->getConnection(); - - foreach ($this->configurators as $configurator) { - $configurator->configureSchema($schema, $connection); - } - } - - public function getSubscribedEvents(): array - { - // subscribe to event only if Doctrine ORM is installed - return class_exists(ToolEvents::class) ? [ToolEvents::postGenerateSchema] : []; - } -} diff --git a/tests/ORM/SchemaSubscriberTest.php b/tests/ORM/SchemaSubscriberTest.php deleted file mode 100644 index 8266444..0000000 --- a/tests/ORM/SchemaSubscriberTest.php +++ /dev/null @@ -1,37 +0,0 @@ - 'sqlite:///:memory:']); - - $configurator = $this->prophesize(SchemaConfigurator::class); - $configurator->configureSchema($schema, $connection)->shouldBeCalled(); - - $connection->getEventManager()->addEventSubscriber(new SchemaSubscriber($configurator->reveal())); - - $entityManager = $this->prophesize(EntityManagerInterface::class); - $entityManager->getConnection()->willReturn($connection); - - $connection->getEventManager()->dispatchEvent( - ToolEvents::postGenerateSchema, - new GenerateSchemaEventArgs($entityManager->reveal(), $schema) - ); - } -}