diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5fa789c..b329692 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.4', '8.0', '8.1'] + php: ['8.1', '8.2', '8.3'] steps: - uses: actions/checkout@v2 diff --git a/Tests/Event/DelayedListenerTest.php b/Tests/Event/DelayedListenerTest.php index 1f17c01..2c8a709 100644 --- a/Tests/Event/DelayedListenerTest.php +++ b/Tests/Event/DelayedListenerTest.php @@ -7,17 +7,16 @@ use Biig\Component\Domain\Event\DomainEventDispatcher; use Biig\Component\Domain\Exception\InvalidDomainEvent; use Biig\Component\Domain\Model\DomainModel; -use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory; 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; use Doctrine\ORM\EntityManager; -use Doctrine\ORM\ORMSetup; use PHPUnit\Framework\TestCase; class DelayedListenerTest extends TestCase { - private $dbPath; + use SetupDatabaseTrait; public function testICanInstantiateDelayedListener() { @@ -62,7 +61,7 @@ public function testItFailsToRegisterOtherThanCurrentModel() public function testItInsertInBddAfterFlushing() { $dispatcher = new DomainEventDispatcher(); - $entityManager = $this->setupDatabase($dispatcher); + $entityManager = $this->setupDatabase($dispatcher, 'testItInsertInBddAfterFlushing'); $model = new FakeModel(); $model->setFoo('Model1'); @@ -104,7 +103,7 @@ public function testItDoesNotExecuteManyTimesSameEvent() { // Test setup $dispatcher = new DomainEventDispatcher(); - $entityManager = $this->setupDatabase($dispatcher); + $entityManager = $this->setupDatabase($dispatcher, 'testItInsertInBddAfterFlushing'); $model = new FakeModel(); $model->setFoo(0); @@ -122,35 +121,6 @@ public function testItDoesNotExecuteManyTimesSameEvent() $this->assertEquals(2, $model->getFoo()); $this->dropDatabase(); } - - private function setupDatabase(DomainEventDispatcher $dispatcher) - { - $this->dbPath = \sys_get_temp_dir() . '/testItInsertInBddAfterFlushing.' . \microtime() . '.sqlite'; - copy(__DIR__ . '/../fixtures/dbtest/initial_fake_model.db', $this->dbPath); - - $config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true); - $config->setClassMetadataFactoryName(ClassMetadataFactory::class); - $conn = [ - 'driver' => 'pdo_sqlite', - 'path' => $this->dbPath, - ]; - - $entityManager = EntityManager::create($conn, $config); - $entityManager->getEventManager()->addEventSubscriber(new DoctrinePostPersistListener($dispatcher)); - - $entityManager->getMetadataFactory()->setDispatcher($dispatcher); - - return $entityManager; - } - - private function dropDatabase() - { - if (!$this->dbPath) { - return; - } - - @unlink($this->dbPath); - } } class FakeDomainModel extends DomainModel diff --git a/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataFactoryTest.php b/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataFactoryTest.php index 952893d..601c691 100644 --- a/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataFactoryTest.php +++ b/Tests/Model/Instantiator/DoctrineConfig/ClassMetadataFactoryTest.php @@ -8,6 +8,7 @@ use Biig\Component\Domain\Event\DomainEventDispatcherInterface; use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadata; use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory; +use Biig\Component\Domain\Tests\SetupDatabaseTrait; use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMSetup; use PHPUnit\Framework\TestCase; @@ -17,6 +18,7 @@ class ClassMetadataFactoryTest extends TestCase { use ProphecyTrait; + use SetupDatabaseTrait; public function testItIsAnInstanceOfDoctrineClassMetadataFactory() { @@ -26,42 +28,27 @@ public function testItIsAnInstanceOfDoctrineClassMetadataFactory() public function testItReturnAnInstanceOfClassMetadata() { - $dbpath = \sys_get_temp_dir() . '/testItReturnAnInstanceOfClassMetadata.' . \microtime() . '.sqlite'; - - $config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true); - $config->setClassMetadataFactoryName(ClassMetadataFactory::class); - - $conn = [ - 'driver' => 'pdo_sqlite', - 'path' => $dbpath, - ]; - $entityManager = EntityManager::create($conn, $config); - $entityManager->getMetadataFactory()->setDispatcher(new DomainEventDispatcher()); + $entityManager = $this->setupDatabase(new DomainEventDispatcher(), 'testItReturnAnInstanceOfClassMetadata'); $metadata = $entityManager->getMetadataFactory()->getMetadataFor(FakeModel::class); $this->assertInstanceOf(ClassMetadata::class, $metadata); - @unlink($dbpath); + $this->dropDatabase(); } public function testItAllowToRetrieveDomainModel() { - $config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true); - $config->setClassMetadataFactoryName(ClassMetadataFactory::class); - $dispatcher = $this->prophesize(DomainEventDispatcherInterface::class); $dispatcher->dispatch(Argument::cetera())->shouldBeCalled(); - $conn = [ - 'driver' => 'pdo_sqlite', - 'path' => __DIR__ . '/../../../fixtures/dbtest/initial_fake_model.db', - ]; - $entityManager = EntityManager::create($conn, $config); - $entityManager->getMetadataFactory()->setDispatcher($dispatcher->reveal()); + $entityManager = $this->setupDatabase($dispatcher->reveal(), 'testItAllowToRetrieveDomainModel'); $res = $entityManager->getRepository(FakeModel::class)->findAll(); - reset($res)->doAction(); + /** @var FakeModel $item */ + $item = reset($res); + $item->doAction(); + $this->dropDatabase(); } } diff --git a/Tests/Model/Instantiator/PostLoadDispatcherInjectionListenerTest.php b/Tests/Model/Instantiator/PostLoadDispatcherInjectionListenerTest.php new file mode 100644 index 0000000..4b5a44a --- /dev/null +++ b/Tests/Model/Instantiator/PostLoadDispatcherInjectionListenerTest.php @@ -0,0 +1,37 @@ +setupDatabase($dispatcher, 'testPostLoadDispatcherInjection'); + $entity = $entityManager->getRepository(FakeModel::class)->find(1); + + $this->assertTrue($entity->hasDispatcher()); + $this->dropDatabase(); + } + + public function testItLoadsDispatcherInProxyEntity() + { + $dispatcher = new DomainEventDispatcher(); + $entityManager = $this->setupDatabase($dispatcher, 'testItLoadsDispatcherInProxyEntity'); + $entity = $entityManager->getRepository(FakeModel::class)->find(1); + + $related = $entity->getRelated(); + $this->assertInstanceOf(Proxy::class, $related); + $this->assertTrue($related->hasDispatcher(), 'The given proxy has no dispatcher yet'); + + $this->dropDatabase(); + } +} diff --git a/Tests/SetupDatabaseTrait.php b/Tests/SetupDatabaseTrait.php new file mode 100644 index 0000000..bb638bb --- /dev/null +++ b/Tests/SetupDatabaseTrait.php @@ -0,0 +1,45 @@ +dbPath = \sys_get_temp_dir() . '/'.$name.'.' . \microtime() . '.sqlite'; + 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, + ]; + + $entityManager = EntityManager::create($conn, $config); + $entityManager->getEventManager()->addEventSubscriber(new DoctrinePostPersistListener($dispatcher)); + $entityManager->getEventManager()->addEventSubscriber(new PostLoadDispatcherInjectionListener($dispatcher)); + + $entityManager->getMetadataFactory()->setDispatcher($dispatcher); + + return $entityManager; + } + + private function dropDatabase() + { + if (!$this->dbPath) { + return; + } + + @unlink($this->dbPath); + } +} diff --git a/Tests/config/symfony_test_kernel_config.yaml b/Tests/config/symfony_test_kernel_config.yaml index cd16e86..b5bc8cd 100644 --- a/Tests/config/symfony_test_kernel_config.yaml +++ b/Tests/config/symfony_test_kernel_config.yaml @@ -8,8 +8,8 @@ framework: serializer: ~ form: ~ test: ~ - annotations: ~ assets: ~ + property_access: ~ doctrine: dbal: @@ -25,7 +25,7 @@ doctrine: mappings: TestApplication: is_bundle: false - type: annotation + type: attribute dir: '%kernel.project_dir%/Tests/fixtures/Entity' prefix: 'Biig\Component\Domain\Tests\fixtures\Entity' alias: App diff --git a/Tests/fixtures/Entity/FakeModel.php b/Tests/fixtures/Entity/FakeModel.php index 780319a..3a657a7 100644 --- a/Tests/fixtures/Entity/FakeModel.php +++ b/Tests/fixtures/Entity/FakeModel.php @@ -3,23 +3,23 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - * @ORM\Table(name="fake_model") - */ +#[ORM\Entity] +#[ORM\Table(name: 'fake_model')] class FakeModel implements \Biig\Component\Domain\Model\ModelInterface { use \Biig\Component\Domain\Model\DomainModelTrait; - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id()] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** @ORM\Column() */ + #[ORM\Column(nullable: true)] private $foo; + + #[ORM\OneToOne(targetEntity: FakeModelRelation::class)] + private FakeModelRelation|null $related = null; + private $something; public function hasDispatcher() @@ -27,46 +27,41 @@ public function hasDispatcher() return null !== $this->dispatcher; } - /** - * @return int - */ - public function getId() + public function getId(): int { return $this->id; } - /** - * @return string - */ - public function getFoo() + public function getFoo(): string { return $this->foo; } - /** - * @return mixed - */ - public function getSomething() + public function getSomething(): mixed { return $this->something; } - /** - * @param mixed $something - */ - public function setSomething($something) + public function setSomething(string $something) { $this->something = $something; } - /** - * @param string $foo - */ - public function setFoo($foo) + public function setFoo(string $foo) { $this->foo = $foo; } + public function getRelated(): ?FakeModelRelation + { + return $this->related; + } + + public function setRelated(?FakeModelRelation $related): void + { + $this->related = $related; + } + /** * Raise a domain event. */ diff --git a/Tests/fixtures/Entity/FakeModelRelation.php b/Tests/fixtures/Entity/FakeModelRelation.php new file mode 100644 index 0000000..f06f206 --- /dev/null +++ b/Tests/fixtures/Entity/FakeModelRelation.php @@ -0,0 +1,45 @@ +content = $content; + } + + public function getId(): int + { + return $this->id; + } + + public function getContent(): string + { + return $this->content; + } + + public function setContent(string $content): void + { + $this->content = $content; + } + + public function hasDispatcher() + { + return null !== $this->dispatcher; + } +} diff --git a/Tests/fixtures/dbtest/fake_model.db b/Tests/fixtures/dbtest/fake_model.db index c86adb6..b985bf1 100644 Binary files a/Tests/fixtures/dbtest/fake_model.db and b/Tests/fixtures/dbtest/fake_model.db differ diff --git a/Tests/fixtures/dbtest/initial_fake_model.db b/Tests/fixtures/dbtest/initial_fake_model.db index d50c571..b985bf1 100644 Binary files a/Tests/fixtures/dbtest/initial_fake_model.db and b/Tests/fixtures/dbtest/initial_fake_model.db differ diff --git a/Tests/schema.php b/Tests/schema.php new file mode 100644 index 0000000..9d0464c --- /dev/null +++ b/Tests/schema.php @@ -0,0 +1,39 @@ + 'pdo_sqlite', + 'path' => $dbPath, +]; + +$dsnParser = new \Doctrine\DBAL\Tools\DsnParser(); +$connectionParams = $dsnParser->parse('sqlite3:///'.$dbPath); + +$connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams); +$entityManager = new EntityManager($connection, $config); + +$schemaManager = $connection->createSchemaManager(); +$schemaManager->dropDatabase($dbPath); +$schemaManager->createDatabase($dbPath); + + +$tool = new \Doctrine\ORM\Tools\SchemaTool($entityManager); +$classes = $entityManager->getMetadataFactory()->getAllMetadata(); +$tool->createSchema($classes); + +$fakeModel = new \Biig\Component\Domain\Tests\fixtures\Entity\FakeModel(); +$fakeModel->setFoo('test'); +$fakeRelated = new \Biig\Component\Domain\Tests\fixtures\Entity\FakeModelRelation('related'); +$fakeModel->setRelated($fakeRelated); +$entityManager->persist($fakeRelated); +$entityManager->persist($fakeModel); +$entityManager->flush(); diff --git a/composer.json b/composer.json index 6f53f2d..9925dc6 100644 --- a/composer.json +++ b/composer.json @@ -3,8 +3,8 @@ "type": "symfony-bundle", "license": "MIT", "require": { - "php": ">=7.4", - "symfony/event-dispatcher": "^4.3|^5.0|^6.0", + "php": ">=8.1", + "symfony/event-dispatcher": "^5.0|^6.0|^7.0", "doctrine/doctrine-bundle": "^1.8|^2.0", "doctrine/orm": "^2.6.3" }, @@ -15,12 +15,19 @@ } ], "require-dev": { - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^9.5 || ^10", "doctrine/orm": "^2.6.3", - "friendsofphp/php-cs-fixer": "^3.11.0", - "symfony/symfony": "^4.3 || ^5.0 || ^6.0", + "friendsofphp/php-cs-fixer": "^3.49.0", + "symfony/framework-bundle": "^5.0 || ^6.0", "phpspec/prophecy-phpunit": "^2.0.1", - "monolog/monolog": "^2.8.0 || ^3.2.0" + "monolog/monolog": "^2.8.0 || ^3.2.0", + "symfony/var-dumper": "^5.0 || ^6.0", + "symfony/serializer": "^5.0 || ^6.0", + "symfony/asset": "^5.0 || ^6.0", + "symfony/form": "^5.0 || ^6.0", + "symfony/property-access": "^5.0 || ^6.0", + "symfony/debug-pack": "^1.0", + "symfony/yaml": "^5.0 || ^6.0" }, "conflict": { "doctrine/orm": "<2.6.3" diff --git a/config/bundles.php b/config/bundles.php index 8653dd1..b125d6e 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -5,5 +5,5 @@ return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true], - \Biig\Component\Domain\Integration\Symfony\DomainBundle::class => ['all' => true], + Biig\Component\Domain\Integration\Symfony\DomainBundle::class => ['all' => true], ]; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e7c2df6..f44639b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,25 +1,22 @@ - - - - - - - - - - ./Tests - - - - - - ./ - - ./docs - ./Tests - ./vendor - - - + + + + ./ + + + ./docs + ./Tests + ./vendor + + + + + + + + + ./Tests + + diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak new file mode 100644 index 0000000..e7c2df6 --- /dev/null +++ b/phpunit.xml.dist.bak @@ -0,0 +1,25 @@ + + + + + + + + + + + ./Tests + + + + + + ./ + + ./docs + ./Tests + ./vendor + + + + diff --git a/src/DataCollector/DomainEventDataCollector.php b/src/DataCollector/DomainEventDataCollector.php index fd551b3..01720e8 100644 --- a/src/DataCollector/DomainEventDataCollector.php +++ b/src/DataCollector/DomainEventDataCollector.php @@ -50,7 +50,7 @@ public function reset(): void $this->dispatcher->reset(); } - public function collect(Request $request, Response $response, \Throwable $exception = null) + public function collect(Request $request, Response $response, ?\Throwable $exception = null) { $this->currentRequest = $this->requestStack->getMainRequest() !== $request ? $request : null; $this->data = [ diff --git a/src/Debug/TraceableDomainEventDispatcher.php b/src/Debug/TraceableDomainEventDispatcher.php index 961eb82..a09611e 100644 --- a/src/Debug/TraceableDomainEventDispatcher.php +++ b/src/Debug/TraceableDomainEventDispatcher.php @@ -98,7 +98,7 @@ public function getDelayedListenersCalled(): array * * @return object The passed $event MUST be returned */ - public function dispatch(object $event, string $eventName = null): object + public function dispatch(object $event, ?string $eventName = null): object { $eventName = $eventName ?? get_class($event); $this->eventsFired[] = $eventName; diff --git a/src/Event/DomainEvent.php b/src/Event/DomainEvent.php index b9d51fb..575ebde 100644 --- a/src/Event/DomainEvent.php +++ b/src/Event/DomainEvent.php @@ -14,7 +14,7 @@ class DomainEvent extends GenericEvent */ private bool $delayed; - public function __construct($subject = null, $arguments = [], Event $originalEvent = null) + public function __construct($subject = null, $arguments = [], ?Event $originalEvent = null) { parent::__construct($subject, $arguments); $this->originalEvent = $originalEvent; diff --git a/src/Event/DomainEventDispatcher.php b/src/Event/DomainEventDispatcher.php index 1e0fc6b..1698165 100644 --- a/src/Event/DomainEventDispatcher.php +++ b/src/Event/DomainEventDispatcher.php @@ -78,7 +78,7 @@ public function addPostPersistDomainRuleInterface(PostPersistDomainRuleInterface * * @return Event */ - public function dispatch($event, string $eventName = null): object + public function dispatch($event, ?string $eventName = null): object { $event = parent::dispatch($event, $eventName); diff --git a/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php b/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php index 3117cfa..6972527 100644 --- a/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php +++ b/src/Integration/Symfony/DependencyInjection/CompilerPass/VerifyDoctrineConfigurationCompilerPass.php @@ -10,9 +10,6 @@ class VerifyDoctrineConfigurationCompilerPass implements CompilerPassInterface { - /** - * {@inheritdoc} - */ public function process(ContainerBuilder $container) { if ($container->getParameter('biig_domain_doctrine_domain_event_instantiator')) { diff --git a/src/Integration/Symfony/DependencyInjection/Configuration.php b/src/Integration/Symfony/DependencyInjection/Configuration.php index 1f716c0..329b97b 100644 --- a/src/Integration/Symfony/DependencyInjection/Configuration.php +++ b/src/Integration/Symfony/DependencyInjection/Configuration.php @@ -7,7 +7,7 @@ class Configuration implements ConfigurationInterface { - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('biig_domain'); diff --git a/src/Integration/Symfony/DomainBundle.php b/src/Integration/Symfony/DomainBundle.php index 8805789..70eb211 100644 --- a/src/Integration/Symfony/DomainBundle.php +++ b/src/Integration/Symfony/DomainBundle.php @@ -13,9 +13,6 @@ class DomainBundle extends Bundle { - /** - * {@inheritdoc} - */ public function getContainerExtension(): ?ExtensionInterface { return new DomainExtension(); diff --git a/src/Integration/Symfony/Serializer/DomainDenormalizer.php b/src/Integration/Symfony/Serializer/DomainDenormalizer.php index 73b02df..b035cc8 100644 --- a/src/Integration/Symfony/Serializer/DomainDenormalizer.php +++ b/src/Integration/Symfony/Serializer/DomainDenormalizer.php @@ -32,10 +32,6 @@ public function __construct(NormalizerInterface $decorated, DomainEventDispatche $this->dispatcher = $dispatcher; } - /** - * {@inheritdoc} - * @return mixed - */ public function denormalize($data, $class, $format = null, array $context = []) { $domain = $this->decorated->denormalize($data, $class, $format, $context); @@ -47,16 +43,12 @@ public function denormalize($data, $class, $format = null, array $context = []) return $domain; } - /** - * {@inheritdoc} - */ public function supportsDenormalization($data, $type, $format = null, array $context = []): bool { return $this->decorated->supportsDenormalization($data, $type, $format, $context); } /** - * {@inheritdoc} * @return array|string|int|float|bool|\ArrayObject|null */ public function normalize($object, $format = null, array $context = []) @@ -64,9 +56,6 @@ public function normalize($object, $format = null, array $context = []) return $this->decorated->normalize($object, $format, $context); } - /** - * {@inheritdoc} - */ public function supportsNormalization($data, $format = null, array $context = []): bool { return $this->decorated->supportsNormalization($data, $format, $context); diff --git a/src/Model/DomainModelTrait.php b/src/Model/DomainModelTrait.php index 327847a..83e32dd 100644 --- a/src/Model/DomainModelTrait.php +++ b/src/Model/DomainModelTrait.php @@ -38,7 +38,7 @@ public function setDispatcher(DomainEventDispatcherInterface $dispatcher) } } - protected function dispatch(DomainEvent $event, string $name = null) + protected function dispatch(DomainEvent $event, ?string $name = null) { if (null === $this->dispatcher) { $eventToStack['event'] = $event; diff --git a/src/Model/Instantiator/DoctrineConfig/ClassMetadata.php b/src/Model/Instantiator/DoctrineConfig/ClassMetadata.php index 84de74b..7ac8bd0 100644 --- a/src/Model/Instantiator/DoctrineConfig/ClassMetadata.php +++ b/src/Model/Instantiator/DoctrineConfig/ClassMetadata.php @@ -20,15 +20,12 @@ class ClassMetadata extends BaseClassMetadata */ private $instantiator; - public function __construct($entityName, InstantiatorInterface $instantiator, NamingStrategy $namingStrategy = null) + public function __construct($entityName, InstantiatorInterface $instantiator, ?NamingStrategy $namingStrategy = null) { parent::__construct($entityName, $namingStrategy); $this->instantiator = $instantiator; } - /** - * {@inheritdoc} - */ public function newInstance(): object { return $this->instantiator->instantiate(parent::newInstance($this->name)); diff --git a/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php b/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php index f951dff..f646446 100644 --- a/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php +++ b/src/Model/Instantiator/DoctrineConfig/ClassMetadataFactory.php @@ -9,7 +9,7 @@ use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface; use Doctrine\Persistence\Mapping\ReflectionService; -if (interface_exists(\Doctrine\Persistence\Mapping\ClassMetadata::class)) { +if (interface_exists(ClassMetadataInterface::class)) { final class ClassMetadataFactory extends BaseClassMetadataFactory { /** @@ -22,9 +22,6 @@ final class ClassMetadataFactory extends BaseClassMetadataFactory */ private $entityManager; - /** - * {@inheritdoc} - */ public function newClassMetadataInstance($className): ClassMetadata { return new ClassMetadata($className, new Instantiator($this->dispatcher), $this->entityManager->getConfiguration()->getNamingStrategy()); @@ -35,9 +32,6 @@ public function setDispatcher(DomainEventDispatcherInterface $dispatcher) $this->dispatcher = $dispatcher; } - /** - * {@inheritdoc} - */ protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService): void { if ($class instanceof ClassMetadata) { @@ -69,9 +63,6 @@ final class ClassMetadataFactory extends BaseClassMetadataFactory */ private $entityManager; - /** - * {@inheritdoc} - */ public function newClassMetadataInstance($className): ClassMetadata { return new ClassMetadata($className, new Instantiator($this->dispatcher), $this->entityManager->getConfiguration()->getNamingStrategy()); @@ -82,9 +73,6 @@ public function setDispatcher(DomainEventDispatcherInterface $dispatcher) $this->dispatcher = $dispatcher; } - /** - * {@inheritdoc} - */ protected function wakeupReflection(OldClassMetadataInterface $class, ReflectionService $reflService): void { if ($class instanceof ClassMetadata) { diff --git a/src/Model/Instantiator/DoctrineConfig/Instantiator.php b/src/Model/Instantiator/DoctrineConfig/Instantiator.php index a343f1e..6cd82ef 100644 --- a/src/Model/Instantiator/DoctrineConfig/Instantiator.php +++ b/src/Model/Instantiator/DoctrineConfig/Instantiator.php @@ -16,7 +16,7 @@ public function __construct(DomainEventDispatcherInterface $dispatcher) parent::__construct($dispatcher); } - public function instantiate($object) + public function instantiate($object): object { $this->injectDispatcher($object); diff --git a/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php b/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php new file mode 100644 index 0000000..620a6f2 --- /dev/null +++ b/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php @@ -0,0 +1,33 @@ +getObject(); + + if ($entity instanceof ModelInterface) { + $entity->setDispatcher($this->dispatcher); + } + } +} diff --git a/src/PostFlushListener/EntitiesHasDispatcherChecker.php b/src/PostFlushListener/EntitiesHasDispatcherChecker.php index 793c545..e52ea14 100644 --- a/src/PostFlushListener/EntitiesHasDispatcherChecker.php +++ b/src/PostFlushListener/EntitiesHasDispatcherChecker.php @@ -10,7 +10,6 @@ use Biig\Component\Domain\Model\ModelInterface; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\PostFlushEventArgs; -use Doctrine\ORM\Proxy\Proxy; final class EntitiesHasDispatcherChecker { @@ -28,7 +27,7 @@ public function postFlush(PostFlushEventArgs $args): void private function checkModelEntityHasDispatcher(object $entity, EntityManagerInterface $entityManager): void { - if (!$entity instanceof ModelInterface || $entity instanceof Proxy) { + if (!$entity instanceof ModelInterface) { return; } diff --git a/src/PostPersistListener/DoctrinePostPersistListener.php b/src/PostPersistListener/DoctrinePostPersistListener.php index efdddca..a1d5ddc 100644 --- a/src/PostPersistListener/DoctrinePostPersistListener.php +++ b/src/PostPersistListener/DoctrinePostPersistListener.php @@ -27,9 +27,6 @@ public function __construct(DomainEventDispatcherInterface $dispatcher) $this->modelsStageForFlush = []; } - /** - * {@inheritdoc} - */ public function getSubscribedEvents() { return ['onFlush', 'postFlush'];