diff --git a/src/Bundle/DependencyInjection/Driver/Doctrine/DoctrineORMDriver.php b/src/Bundle/DependencyInjection/Driver/Doctrine/DoctrineORMDriver.php index c0393169b..665c34540 100644 --- a/src/Bundle/DependencyInjection/Driver/Doctrine/DoctrineORMDriver.php +++ b/src/Bundle/DependencyInjection/Driver/Doctrine/DoctrineORMDriver.php @@ -18,6 +18,7 @@ use Doctrine\Common\Persistence\ObjectManager as DeprecatedObjectManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; +use Doctrine\ORM\Mapping\Entity; use Doctrine\Persistence\ObjectManager; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; @@ -39,7 +40,7 @@ public function getType(): string protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata): void { $repositoryClassParameterName = sprintf('%s.repository.%s.class', $metadata->getApplicationName(), $metadata->getName()); - $repositoryClass = EntityRepository::class; + $repositoryClass = null; /** @var string[] $genericEntities */ $genericEntities = $container->hasParameter(self::GENERIC_ENTITIES_PARAMETER) ? $container->getParameter(self::GENERIC_ENTITIES_PARAMETER) : []; @@ -54,6 +55,21 @@ protected function addRepository(ContainerBuilder $container, MetadataInterface $repositoryClass = $metadata->getClass('repository'); } + $entityAttributes = (new \ReflectionClass($metadata->getClass('model'))) + ->getAttributes(Entity::class); + + $entityAttributeRepositoryClass = ($entityAttributes[0] ?? null) + ?->newInstance() + ->repositoryClass; + + if (null !== $entityAttributeRepositoryClass) { + $repositoryClass = $entityAttributeRepositoryClass; + } + + if (null === $repositoryClass) { + $repositoryClass = EntityRepository::class; + } + $serviceId = $metadata->getServiceId('repository'); $managerReference = new Reference($metadata->getServiceId('manager')); $definition = new Definition($repositoryClass); diff --git a/tests/Bundle/DependencyInjection/SyliusResourceExtensionTest.php b/tests/Bundle/DependencyInjection/SyliusResourceExtensionTest.php index a41af29ce..bf7049675 100644 --- a/tests/Bundle/DependencyInjection/SyliusResourceExtensionTest.php +++ b/tests/Bundle/DependencyInjection/SyliusResourceExtensionTest.php @@ -18,6 +18,7 @@ use App\Entity\ComicBook; use App\Factory\BookFactory; use App\Form\Type\BookType; +use App\Repository\ComicBookRepository; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; use Sylius\Bundle\ResourceBundle\Controller\ResourceController; use Sylius\Bundle\ResourceBundle\DependencyInjection\SyliusResourceExtension; @@ -65,6 +66,26 @@ public function it_registers_services_and_parameters_for_resources(): void $this->assertContainerBuilderHasParameter('app.form.book.class', BookType::class); } + /** + * @test + */ + public function it_registers_the_entity_repository_classes_from_entity_attributes(): void + { + $this->setParameter('kernel.bundles', []); + + $this->load([ + 'resources' => [ + 'app.comic_book' => [ + 'classes' => [ + 'model' => ComicBook::class, + ], + ], + ], + ]); + + $this->assertContainerBuilderHasService('app.repository.comic_book', ComicBookRepository::class); + } + /** * @test */ diff --git a/tests/Bundle/Resource/ResourceServicesTest.php b/tests/Bundle/Resource/ResourceServicesTest.php index 636e59388..d95ab6208 100644 --- a/tests/Bundle/Resource/ResourceServicesTest.php +++ b/tests/Bundle/Resource/ResourceServicesTest.php @@ -79,6 +79,6 @@ public function it_will_return_the_same_repository_instance_for_default_reposito $repositoryAlias = $em->getRepository(ComicBook::class); $this->assertInstanceOf(RepositoryInterface::class, $repository); - $this->assertSame($repository, $repositoryAlias); + $this->assertSame($repository::class, $repositoryAlias::class); } }