From 728e1de72e76841e25ad36a30c5390033fb0edef Mon Sep 17 00:00:00 2001 From: Maxime Veber Date: Mon, 29 Jul 2024 11:11:51 +0200 Subject: [PATCH] :sparkles: remove super custom code In previous versions of Doctrine, it was hard to act on entity load. Since doctrine changed its internal behavior, we managed to improve the situation as well on the bundle. The new PostLoadDispatcherInjecttionListener class allows us to remove a huge part of this bundle that was previously super-mega-custom. This is awesome! But it comes with a huge refactoring, and this is not even the last commit toward the next major version of this package. --- Tests/Event/DelayedListenerTest.php | 3 +- .../ClassMetadataFactoryTest.php | 54 ----------- .../DoctrineConfig/ClassMetadataTest.php | 56 ----------- .../DoctrineConfig/InstantiatorTest.php | 28 ------ .../EntitiesHasDispatcherCheckerTest.php | 2 +- Tests/SetupDatabaseTrait.php | 3 - ...InClassMetadataFactoryCompilerPassTest.php | 83 ----------------- ...yDoctrineConfigurationCompilerPassTest.php | 66 ------------- .../DomainExtensionTest.php | 1 + .../EntityManagerConfiguratorTest.php | 33 ------- composer.json | 4 +- phpunit.xml.dist | 22 ++--- phpunit.xml.dist.bak | 25 ----- ...cherInClassMetadataFactoryCompilerPass.php | 38 -------- ...erifyDoctrineConfigurationCompilerPass.php | 40 -------- .../DependencyInjection/Configuration.php | 3 - .../DependencyInjection/DomainExtension.php | 32 ------- .../EntityManagerConfigurator.php | 40 -------- src/Integration/Symfony/DomainBundle.php | 4 - .../Symfony/Resources/config/services.yaml | 6 ++ .../DoctrineConfig/ClassMetadata.php | 43 --------- .../DoctrineConfig/ClassMetadataFactory.php | 93 ------------------- .../DoctrineConfig/Instantiator.php | 25 ----- .../PostLoadDispatcherInjectionListener.php | 8 +- .../DomainModelInstantiatorInterface.php | 6 +- src/Model/Instantiator/Instantiator.php | 7 +- 26 files changed, 31 insertions(+), 694 deletions(-) delete mode 100644 Tests/Model/Instantiator/DoctrineConfig/ClassMetadataFactoryTest.php delete mode 100644 Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php delete mode 100644 Tests/Model/Instantiator/DoctrineConfig/InstantiatorTest.php delete mode 100644 Tests/Symfony/DependencyInjection/CompilerPass/InsertDispatcherInClassMetadataFactoryCompilerPassTest.php delete mode 100644 Tests/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPassTest.php delete mode 100644 Tests/Symfony/DependencyInjection/EntityManagerConfiguratorTest.php delete mode 100644 phpunit.xml.dist.bak delete mode 100644 src/Integration/Symfony/DependencyInjection/CompilerPass/InsertDispatcherInClassMetadataFactoryCompilerPass.php delete mode 100644 src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php delete mode 100644 src/Integration/Symfony/DependencyInjection/EntityManagerConfigurator.php delete mode 100644 src/Model/Instantiator/DoctrineConfig/ClassMetadata.php delete mode 100644 src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php delete mode 100644 src/Model/Instantiator/DoctrineConfig/Instantiator.php diff --git a/Tests/Event/DelayedListenerTest.php b/Tests/Event/DelayedListenerTest.php index 2c8a709..de38fe2 100644 --- a/Tests/Event/DelayedListenerTest.php +++ b/Tests/Event/DelayedListenerTest.php @@ -7,7 +7,6 @@ use Biig\Component\Domain\Event\DomainEventDispatcher; use Biig\Component\Domain\Exception\InvalidDomainEvent; use Biig\Component\Domain\Model\DomainModel; -use Biig\Component\Domain\PostPersistListener\DoctrinePostPersistListener; use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface; use Biig\Component\Domain\Tests\fixtures\Entity\FakeModel; use Biig\Component\Domain\Tests\SetupDatabaseTrait; @@ -103,7 +102,7 @@ public function testItDoesNotExecuteManyTimesSameEvent() { // Test setup $dispatcher = new DomainEventDispatcher(); - $entityManager = $this->setupDatabase($dispatcher, 'testItInsertInBddAfterFlushing'); + $entityManager = $this->setupDatabase($dispatcher, 'testItDoesNotExecuteManyTimesSameEvent'); $model = new FakeModel(); $model->setFoo(0); diff --git a/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataFactoryTest.php b/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataFactoryTest.php deleted file mode 100644 index 601c691..0000000 --- a/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataFactoryTest.php +++ /dev/null @@ -1,54 +0,0 @@ -assertInstanceOf(\Doctrine\ORM\Mapping\ClassMetadataFactory::class, $factory); - } - - public function testItReturnAnInstanceOfClassMetadata() - { - $entityManager = $this->setupDatabase(new DomainEventDispatcher(), 'testItReturnAnInstanceOfClassMetadata'); - - $metadata = $entityManager->getMetadataFactory()->getMetadataFor(FakeModel::class); - - $this->assertInstanceOf(ClassMetadata::class, $metadata); - - $this->dropDatabase(); - } - - public function testItAllowToRetrieveDomainModel() - { - $dispatcher = $this->prophesize(DomainEventDispatcherInterface::class); - $dispatcher->dispatch(Argument::cetera())->shouldBeCalled(); - - $entityManager = $this->setupDatabase($dispatcher->reveal(), 'testItAllowToRetrieveDomainModel'); - - $res = $entityManager->getRepository(FakeModel::class)->findAll(); - - /** @var FakeModel $item */ - $item = reset($res); - $item->doAction(); - $this->dropDatabase(); - } -} diff --git a/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php b/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php deleted file mode 100644 index 236c124..0000000 --- a/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataTest.php +++ /dev/null @@ -1,56 +0,0 @@ -metadata = new ClassMetadata(FakeModel::class, new Instantiator(new DomainEventDispatcher())); - } - - public function testItIsInstanceOfDoctrineClassMetadata() - { - $this->assertInstanceOf(\Doctrine\ORM\Mapping\ClassMetadata::class, $this->metadata); - } - - public function testItInstantiateEntities() - { - $model = $this->metadata->newInstance(); - - $this->assertInstanceOf(FakeModel::class, $model); - } - - public function testItsWakable() - { - $metadata = unserialize(serialize($this->metadata)); - - $this->assertInstanceOf(ClassMetadata::class, $metadata); - - if (interface_exists(\Doctrine\Persistence\Mapping\ReflectionService::class)) { - $refSer = $this->prophesize(\Doctrine\Persistence\Mapping\ReflectionService::class); - } else { - $refSer = $this->prophesize(\Doctrine\Common\Persistence\Mapping\ReflectionService::class); - } - $metadata->wakeupReflectionWithInstantiator($refSer->reveal(), new Instantiator(new DomainEventDispatcher())); - - $model = $metadata->newInstance(); - - $this->assertInstanceOf(FakeModel::class, $model); - $this->assertTrue($model->hasDispatcher()); - } -} diff --git a/Tests/Model/Instantiator/DoctrineConfig/InstantiatorTest.php b/Tests/Model/Instantiator/DoctrineConfig/InstantiatorTest.php deleted file mode 100644 index ba5671c..0000000 --- a/Tests/Model/Instantiator/DoctrineConfig/InstantiatorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -assertInstanceOf(InstantiatorInterface::class, $instantiator); - } - - public function testItUseTheGivenInstantiator() - { - $instantiator = new Instantiator(new DomainEventDispatcher()); - - $model = $instantiator->instantiate(new FakeModel); - $this->assertTrue($model->hasDispatcher()); - } -} - - diff --git a/Tests/PostFlushListener/EntitiesHasDispatcherCheckerTest.php b/Tests/PostFlushListener/EntitiesHasDispatcherCheckerTest.php index 866617c..38b23f2 100644 --- a/Tests/PostFlushListener/EntitiesHasDispatcherCheckerTest.php +++ b/Tests/PostFlushListener/EntitiesHasDispatcherCheckerTest.php @@ -42,7 +42,7 @@ public function testAnEntityThatDoesntHaveDispatcherWhileFlushedThrowAnError() { self::bootKernel(['debug' => true]); // You should not create your entites this way in your own code ! - // Use the Biig\Component\Domain\Model\Instantiator\Instantiator service to instanciate your entities. + // Use the Biig\Component\Domain\Model\Instantiator\Instantiator service to instantiate your entities. $model = new FakeModel(); $this->expectException(FlushedEntityDoesntContainsDispatcherException::class); diff --git a/Tests/SetupDatabaseTrait.php b/Tests/SetupDatabaseTrait.php index bb638bb..8475d9a 100644 --- a/Tests/SetupDatabaseTrait.php +++ b/Tests/SetupDatabaseTrait.php @@ -19,7 +19,6 @@ private function setupDatabase(DomainEventDispatcherInterface $dispatcher, strin copy(__DIR__ . '/fixtures/dbtest/initial_fake_model.db', $this->dbPath); $config = ORMSetup::createAttributeMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true); - $config->setClassMetadataFactoryName(ClassMetadataFactory::class); $conn = [ 'driver' => 'pdo_sqlite', 'path' => $this->dbPath, @@ -29,8 +28,6 @@ private function setupDatabase(DomainEventDispatcherInterface $dispatcher, strin $entityManager->getEventManager()->addEventSubscriber(new DoctrinePostPersistListener($dispatcher)); $entityManager->getEventManager()->addEventSubscriber(new PostLoadDispatcherInjectionListener($dispatcher)); - $entityManager->getMetadataFactory()->setDispatcher($dispatcher); - return $entityManager; } diff --git a/Tests/Symfony/DependencyInjection/CompilerPass/InsertDispatcherInClassMetadataFactoryCompilerPassTest.php b/Tests/Symfony/DependencyInjection/CompilerPass/InsertDispatcherInClassMetadataFactoryCompilerPassTest.php deleted file mode 100644 index e81044d..0000000 --- a/Tests/Symfony/DependencyInjection/CompilerPass/InsertDispatcherInClassMetadataFactoryCompilerPassTest.php +++ /dev/null @@ -1,83 +0,0 @@ -assertInstanceOf(CompilerPassInterface::class, $pass); - } - - public function testItDecorateEntityManagersConfigurators() - { - $container = $this->prophesize(ContainerBuilder::class); - $container->getParameter('biig_domain.entity_managers')->willReturn(['hello', 'world']); - $container->getParameter('biig_domain_doctrine_domain_event_instantiator')->willReturn(true); - - $defHello = new Definition(); - $defWorld = new Definition(); - - $container->register('biig_domain.hello_configurator', EntityManagerConfigurator::class)->shouldBeCalled()->willReturn($defHello); - $container->register('biig_domain.world_configurator', EntityManagerConfigurator::class)->shouldBeCalled()->willReturn($defWorld); - - $compilerPass = new InsertDispatcherInClassMetadataFactoryCompilerPass(); - $compilerPass->process($container->reveal()); - - $this->assertFalse($defHello->isPublic()); - $this->assertFalse($defWorld->isPublic()); - - $this->assertInstanceOf(Reference::class, $defHello->getArgument(0)); - $this->assertInstanceOf(Reference::class, $defWorld->getArgument(0)); - - $this->assertEquals((string) $defHello->getArgument(0), 'biig_domain.hello_configurator.inner'); - $this->assertEquals((string) $defWorld->getArgument(0), 'biig_domain.world_configurator.inner'); - $this->assertEquals((string) $defHello->getArgument(1), 'biig_domain.dispatcher'); - $this->assertEquals((string) $defWorld->getArgument(1), 'biig_domain.dispatcher'); - } - - public function testItUseDoctrineDefaultIfNoEntityManagerProvided() - { - $container = $this->prophesize(ContainerBuilder::class); - $container->getParameter('biig_domain.entity_managers')->willReturn([]); - $container->getParameter('doctrine.default_entity_manager')->willReturn('default'); - $container->getParameter('biig_domain_doctrine_domain_event_instantiator')->willReturn(true); - - $configurator = new Definition(); - - $container->register('biig_domain.default_configurator', EntityManagerConfigurator::class)->shouldBeCalled()->willReturn($configurator); - - $compilerPass = new InsertDispatcherInClassMetadataFactoryCompilerPass(); - $compilerPass->process($container->reveal()); - - $this->assertFalse($configurator->isPublic()); - $this->assertInstanceOf(Reference::class, $configurator->getArgument(0)); - $this->assertEquals((string) $configurator->getArgument(0), 'biig_domain.default_configurator.inner'); - $this->assertEquals((string) $configurator->getArgument(1), 'biig_domain.dispatcher'); - } - - public function testItDoesNotAddConfigurationForEntityManagers() - { - $container = $this->prophesize(ContainerBuilder::class); - $container->getParameter('biig_domain.entity_managers')->willReturn(['hello', 'world']); - $container->getParameter('biig_domain_doctrine_domain_event_instantiator')->willReturn(false); - - $container->register(Argument::cetera())->shouldNotBeCalled(); - - $compilerPass = new InsertDispatcherInClassMetadataFactoryCompilerPass(); - $compilerPass->process($container->reveal()); - } -} diff --git a/Tests/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPassTest.php b/Tests/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPassTest.php deleted file mode 100644 index eca364d..0000000 --- a/Tests/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPassTest.php +++ /dev/null @@ -1,66 +0,0 @@ -assertInstanceOf(CompilerPassInterface::class, $pass); - } - - public function testItThrowsAnErrorWhenTheConfigurationIsNotModifiedAsExpected() - { - $this->expectException(InvalidConfigurationException::class); - $container = $this->prophesize(ContainerBuilder::class); - $container->getParameter('biig_domain_doctrine_domain_event_instantiator')->willReturn(true); - $config = new Definition(Configuration::class); - $configChild = new ChildDefinition('doctrine.orm.configuration'); - - // The method call is to our ClassMetadataFactory if the previous parameter is defined, - // if something alter the value, we need to throw an error. - $configChild->addMethodCall('setClassMetadataFactoryName', [null]); - - $container->getDefinitions()->willReturn([ - 'doctrine.orm.configuration' => $config, - 'doctrine.orm.configuration.whatever' => $configChild, - ]); - - $compilerPass = new VerifyDoctrineConfigurationCompilerPass(); - $compilerPass->process($container->reveal()); - } - - public function testItDoesNothingWhenFeatureNotActivated() - { - $container = $this->prophesize(ContainerBuilder::class); - $container->getParameter('biig_domain_doctrine_domain_event_instantiator')->willReturn(false)->shouldBeCalled(); - $container->getDefinitions([])->shouldNotBeCalled(); - - $compilerPass = new VerifyDoctrineConfigurationCompilerPass(); - $compilerPass->process($container->reveal()); - } - - public function testItWorksWithFeatureActivatedButNoDoctrineConfiguration() - { - $container = new ContainerBuilder(); - $container->setParameter('biig_domain_doctrine_domain_event_instantiator', true); - $container->setDefinition('dispatcher', new Definition(DomainEventDispatcher::class)); - - $compilerPass = new VerifyDoctrineConfigurationCompilerPass(); - $this->assertNull($compilerPass->process($container)); - } -} diff --git a/Tests/Symfony/DependencyInjection/DomainExtensionTest.php b/Tests/Symfony/DependencyInjection/DomainExtensionTest.php index d3151ca..a44a1c7 100644 --- a/Tests/Symfony/DependencyInjection/DomainExtensionTest.php +++ b/Tests/Symfony/DependencyInjection/DomainExtensionTest.php @@ -25,6 +25,7 @@ public function testItAddsDoctrinePostPersistListenerToContainer() $extension->load($config, $container); $array = [ + 'biig_domain.postload_subscriber' => [[]], "biig_domain.post_persist_listener.doctrine_default" => [ [ "connection" => "default" diff --git a/Tests/Symfony/DependencyInjection/EntityManagerConfiguratorTest.php b/Tests/Symfony/DependencyInjection/EntityManagerConfiguratorTest.php deleted file mode 100644 index 6b6946c..0000000 --- a/Tests/Symfony/DependencyInjection/EntityManagerConfiguratorTest.php +++ /dev/null @@ -1,33 +0,0 @@ -prophesize(EntityManager::class); - $originalConfigurator = $this->prophesize(ManagerConfigurator::class)->reveal(); - $factory = new ClassMetadataFactory(); - - $entityManager->getMetadataFactory()->willReturn($factory); - - $configurator = new EntityManagerConfigurator($originalConfigurator, new DomainEventDispatcher()); - $configurator->configure($entityManager->reveal()); - - $ref = new \ReflectionObject($factory); - $property = $ref->getProperty('dispatcher'); - $property->setAccessible(true); - $this->assertNotNull($property->getValue($factory)); - } -} diff --git a/composer.json b/composer.json index 9925dc6..5518a93 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "php": ">=8.1", "symfony/event-dispatcher": "^5.0|^6.0|^7.0", "doctrine/doctrine-bundle": "^1.8|^2.0", - "doctrine/orm": "^2.6.3" + "doctrine/orm": "^2.18.3" }, "authors": [ { @@ -16,7 +16,7 @@ ], "require-dev": { "phpunit/phpunit": "^9.5 || ^10", - "doctrine/orm": "^2.6.3", + "doctrine/orm": "^2.18.3", "friendsofphp/php-cs-fixer": "^3.49.0", "symfony/framework-bundle": "^5.0 || ^6.0", "phpspec/prophecy-phpunit": "^2.0.1", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f44639b..120d2b7 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,15 +1,5 @@ - - - - ./ - - - ./docs - ./Tests - ./vendor - - + @@ -19,4 +9,14 @@ ./Tests + + + ./ + + + ./docs + ./Tests + ./vendor + + diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak deleted file mode 100644 index e7c2df6..0000000 --- a/phpunit.xml.dist.bak +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - ./Tests - - - - - - ./ - - ./docs - ./Tests - ./vendor - - - - diff --git a/src/Integration/Symfony/DependencyInjection/CompilerPass/InsertDispatcherInClassMetadataFactoryCompilerPass.php b/src/Integration/Symfony/DependencyInjection/CompilerPass/InsertDispatcherInClassMetadataFactoryCompilerPass.php deleted file mode 100644 index 9790a3a..0000000 --- a/src/Integration/Symfony/DependencyInjection/CompilerPass/InsertDispatcherInClassMetadataFactoryCompilerPass.php +++ /dev/null @@ -1,38 +0,0 @@ -getParameter('biig_domain.entity_managers'); - if (empty($entityManagers)) { - $entityManagers = [$container->getParameter('doctrine.default_entity_manager')]; - } - - if ($container->getParameter('biig_domain_doctrine_domain_event_instantiator')) { - foreach ($entityManagers as $entityManager) { - $this->addDecoratorToConfigurator($container, $entityManager); - } - } - } - - private function addDecoratorToConfigurator(ContainerBuilder $container, string $entityManager) - { - $serviceName = sprintf('biig_domain.%s_configurator', $entityManager); - $originalConfiguratorName = sprintf('doctrine.orm.%s_manager_configurator', $entityManager); - - $container->register($serviceName, EntityManagerConfigurator::class) - ->setDecoratedService($originalConfiguratorName) - ->addArgument(new Reference($serviceName . '.inner')) - ->addArgument(new Reference('biig_domain.dispatcher')) - ->setPublic(false) - ; - } -} diff --git a/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php b/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php deleted file mode 100644 index 6972527..0000000 --- a/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php +++ /dev/null @@ -1,40 +0,0 @@ -getParameter('biig_domain_doctrine_domain_event_instantiator')) { - $definitions = $container->getDefinitions(); - - foreach ($definitions as $name => $definition) { - if ($definition instanceof ChildDefinition && 'doctrine.orm.configuration' === $definition->getParent()) { - $calls = $definition->getMethodCalls(); - $this->verifyCalls($calls); - } - } - } - } - - /** - * @throws InvalidConfigurationException - */ - private function verifyCalls(array $calls) - { - foreach ($calls as $call) { - if ('setClassMetadataFactoryName' === $call[0]) { - if (ClassMetadataFactory::class !== $call[1][0]) { - throw new InvalidConfigurationException('The option "override_doctrine_instantiator", so this bundles tried to change the ClassMetadataFactory of doctrine by changing the DoctrineBundle configuration. The final configuration of the doctrine bundle doesn\'t looks like the one expected: Something probably altered the configuration. You may disable this feature by changing the default configuration or find what came override this. (It may be your manual configuration)'); - } - } - } - } -} diff --git a/src/Integration/Symfony/DependencyInjection/Configuration.php b/src/Integration/Symfony/DependencyInjection/Configuration.php index 329b97b..dc041e8 100644 --- a/src/Integration/Symfony/DependencyInjection/Configuration.php +++ b/src/Integration/Symfony/DependencyInjection/Configuration.php @@ -14,9 +14,6 @@ public function getConfigTreeBuilder(): TreeBuilder $treeBuilder ->getRootNode() ->children() - ->booleanNode('override_doctrine_instantiator') - ->defaultTrue() - ->end() ->arrayNode('entity_managers') ->scalarPrototype()->end() ->end() diff --git a/src/Integration/Symfony/DependencyInjection/DomainExtension.php b/src/Integration/Symfony/DependencyInjection/DomainExtension.php index 6b78202..1171cf6 100644 --- a/src/Integration/Symfony/DependencyInjection/DomainExtension.php +++ b/src/Integration/Symfony/DependencyInjection/DomainExtension.php @@ -2,7 +2,6 @@ namespace Biig\Component\Domain\Integration\Symfony\DependencyInjection; -use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory; use Biig\Component\Domain\PostPersistListener\DoctrinePostPersistListener; use Biig\Component\Domain\Rule\RuleInterface; use Symfony\Component\Config\FileLocator; @@ -31,8 +30,6 @@ public function load(array $configs, ContainerBuilder $container): void $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $container->setParameter('biig_domain_doctrine_domain_event_instantiator', $config['override_doctrine_instantiator']); - $container->registerForAutoconfiguration(RuleInterface::class)->addTag(self::DOMAIN_RULE_TAG); $container->setParameter('biig_domain.entity_managers', $config['entity_managers']); @@ -55,14 +52,6 @@ public function prepend(ContainerBuilder $container): void // Pre-process the configuration $configs = $container->getExtensionConfig($this->getAlias()); $config = $this->processConfiguration(new Configuration(), $configs); - - // This is true by default - if ($config['override_doctrine_instantiator']) { - $doctrineConfig = $container->getExtensionConfig('doctrine'); - $doctrineClassMetadataFactoryConfig = $this->buildClassMetadataFactoryConfig($doctrineConfig); - - $container->prependExtensionConfig('doctrine', $doctrineClassMetadataFactoryConfig); - } } } @@ -84,25 +73,4 @@ private function registerDoctrinePostPersistListener(array $config, ContainerBui ; } } - - private function buildClassMetadataFactoryConfig(array $doctrineConfig) - { - $doctrineClassMetadataFactoryConfig = [ - 'orm' => [ - 'entity_managers' => [ - 'default' => [ - 'class_metadata_factory_name' => ClassMetadataFactory::class, - ], - ], - ], - ]; - - if (isset($doctrineConfig[0]['orm']['entity_managers'])) { - foreach ($doctrineConfig[0]['orm']['entity_managers'] as $entityManagerName => $entityManagerConf) { - $doctrineClassMetadataFactoryConfig['orm']['entity_managers'][$entityManagerName]['class_metadata_factory_name'] = ClassMetadataFactory::class; - } - } - - return $doctrineClassMetadataFactoryConfig; - } } diff --git a/src/Integration/Symfony/DependencyInjection/EntityManagerConfigurator.php b/src/Integration/Symfony/DependencyInjection/EntityManagerConfigurator.php deleted file mode 100644 index dd83bc7..0000000 --- a/src/Integration/Symfony/DependencyInjection/EntityManagerConfigurator.php +++ /dev/null @@ -1,40 +0,0 @@ -originalConfigurator = $configurator; - $this->dispatcher = $dispatcher; - } - - public function configure(EntityManager $entityManager) - { - $this->originalConfigurator->configure($entityManager); - $metadataFactory = $entityManager->getMetadataFactory(); - - if ($metadataFactory instanceof ClassMetadataFactory) { - $metadataFactory->setDispatcher($this->dispatcher); - } - } -} diff --git a/src/Integration/Symfony/DomainBundle.php b/src/Integration/Symfony/DomainBundle.php index 70eb211..6c1b0c7 100644 --- a/src/Integration/Symfony/DomainBundle.php +++ b/src/Integration/Symfony/DomainBundle.php @@ -3,9 +3,7 @@ namespace Biig\Component\Domain\Integration\Symfony; use Biig\Component\Domain\Integration\Symfony\DependencyInjection\CompilerPass\EnableDomainDenormalizerCompilerPass; -use Biig\Component\Domain\Integration\Symfony\DependencyInjection\CompilerPass\InsertDispatcherInClassMetadataFactoryCompilerPass; use Biig\Component\Domain\Integration\Symfony\DependencyInjection\CompilerPass\RegisterDomainRulesCompilerPass; -use Biig\Component\Domain\Integration\Symfony\DependencyInjection\CompilerPass\VerifyDoctrineConfigurationCompilerPass; use Biig\Component\Domain\Integration\Symfony\DependencyInjection\DomainExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; @@ -22,8 +20,6 @@ public function build(ContainerBuilder $container) { parent::build($container); - $container->addCompilerPass(new VerifyDoctrineConfigurationCompilerPass()); - $container->addCompilerPass(new InsertDispatcherInClassMetadataFactoryCompilerPass()); $container->addCompilerPass(new RegisterDomainRulesCompilerPass()); $container->addCompilerPass(new EnableDomainDenormalizerCompilerPass()); } diff --git a/src/Integration/Symfony/Resources/config/services.yaml b/src/Integration/Symfony/Resources/config/services.yaml index f95c0cf..f16fee8 100644 --- a/src/Integration/Symfony/Resources/config/services.yaml +++ b/src/Integration/Symfony/Resources/config/services.yaml @@ -13,3 +13,9 @@ services: alias: Biig\Component\Domain\Model\Instantiator\Instantiator public: true + biig_domain.postload_subscriber: + class: Biig\Component\Domain\Model\Instantiator\DoctrineConfig\PostLoadDispatcherInjectionListener + arguments: + - '@biig_domain.dispatcher' + tags: + - { name: doctrine.event_subscriber } diff --git a/src/Model/Instantiator/DoctrineConfig/ClassMetadata.php b/src/Model/Instantiator/DoctrineConfig/ClassMetadata.php deleted file mode 100644 index 7ac8bd0..0000000 --- a/src/Model/Instantiator/DoctrineConfig/ClassMetadata.php +++ /dev/null @@ -1,43 +0,0 @@ -instantiator = $instantiator; - } - - public function newInstance(): object - { - return $this->instantiator->instantiate(parent::newInstance($this->name)); - } - - /** - * @param ReflectionService $reflService - * @param DomainModelInstantiatorInterface $instantiator - */ - public function wakeupReflectionWithInstantiator($reflService, $instantiator) - { - $this->instantiator = $instantiator; - parent::wakeupReflection($reflService); - } -} diff --git a/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php b/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php deleted file mode 100644 index f646446..0000000 --- a/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php +++ /dev/null @@ -1,93 +0,0 @@ -dispatcher), $this->entityManager->getConfiguration()->getNamingStrategy()); - } - - public function setDispatcher(DomainEventDispatcherInterface $dispatcher) - { - $this->dispatcher = $dispatcher; - } - - protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService): void - { - if ($class instanceof ClassMetadata) { - $class->wakeupReflectionWithInstantiator($reflService, new Instantiator($this->dispatcher)); - - return; - } - - $class->wakeupReflection($reflService); - } - - public function setEntityManager(EntityManagerInterface $em): void - { - $this->entityManager = $em; - parent::setEntityManager($em); - } - } -} else { - // Compatibility layer for Doctrine ORM <= 2.6 - final class ClassMetadataFactory extends BaseClassMetadataFactory - { - /** - * @var DomainEventDispatcherInterface - */ - private $dispatcher; - - /** - * @var EntityManagerInterface - */ - private $entityManager; - - public function newClassMetadataInstance($className): ClassMetadata - { - return new ClassMetadata($className, new Instantiator($this->dispatcher), $this->entityManager->getConfiguration()->getNamingStrategy()); - } - - public function setDispatcher(DomainEventDispatcherInterface $dispatcher) - { - $this->dispatcher = $dispatcher; - } - - protected function wakeupReflection(OldClassMetadataInterface $class, ReflectionService $reflService): void - { - if ($class instanceof ClassMetadata) { - $class->wakeupReflectionWithInstantiator($reflService, new Instantiator($this->dispatcher)); - - return; - } - - $class->wakeupReflection($reflService); - } - - public function setEntityManager(EntityManagerInterface $em): void - { - $this->entityManager = $em; - parent::setEntityManager($em); - } - } -} diff --git a/src/Model/Instantiator/DoctrineConfig/Instantiator.php b/src/Model/Instantiator/DoctrineConfig/Instantiator.php deleted file mode 100644 index 6cd82ef..0000000 --- a/src/Model/Instantiator/DoctrineConfig/Instantiator.php +++ /dev/null @@ -1,25 +0,0 @@ -injectDispatcher($object); - - return $object; - } -} diff --git a/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php b/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php index 620a6f2..14aa011 100644 --- a/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php +++ b/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php @@ -5,7 +5,7 @@ use Biig\Component\Domain\Event\DomainEventDispatcherInterface; use Biig\Component\Domain\Model\ModelInterface; use Doctrine\Common\EventSubscriber; -use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\ORM\Event\PostLoadEventArgs; class PostLoadDispatcherInjectionListener implements EventSubscriber { @@ -18,11 +18,7 @@ public function getSubscribedEvents() return ['postLoad']; } - /** - * BC Layer: typing LifecycleEventArgs for previous Doctrine versions. - * New versions use \Doctrine\ORM\Event\PostLoadEventArgs. - */ - public function postLoad(LifecycleEventArgs $args) + public function postLoad(PostLoadEventArgs $args) { $entity = $args->getObject(); diff --git a/src/Model/Instantiator/DomainModelInstantiatorInterface.php b/src/Model/Instantiator/DomainModelInstantiatorInterface.php index d09b704..2be6d2e 100644 --- a/src/Model/Instantiator/DomainModelInstantiatorInterface.php +++ b/src/Model/Instantiator/DomainModelInstantiatorInterface.php @@ -9,11 +9,9 @@ interface DomainModelInstantiatorInterface * We do not inherit from the InstantiatorInterface to allow the usage of this component without doctrine. * This is also the reason to not have arguments in this method. * - * @param string $className - * - * @return object + * @param class-string $className */ - public function instantiate($className); + public function instantiate(string $className): object; /** * @param array ...$args diff --git a/src/Model/Instantiator/Instantiator.php b/src/Model/Instantiator/Instantiator.php index c705ddd..17dffe0 100644 --- a/src/Model/Instantiator/Instantiator.php +++ b/src/Model/Instantiator/Instantiator.php @@ -23,7 +23,10 @@ public function __construct(DomainEventDispatcherInterface $dispatcher) $this->dispatcher = $dispatcher; } - public function instantiate($className) + /** + * @param class-string $className + */ + public function instantiate(string $className): object { $object = new $className(); $this->injectDispatcher($object); @@ -47,7 +50,7 @@ public function instantiateViaStaticFactory(string $className, string $factoryMe return $object; } - protected function injectDispatcher($object) + protected function injectDispatcher(object $object): void { if ($object instanceof ModelInterface) { $object->setDispatcher($this->dispatcher);