Skip to content

Commit

Permalink
Fix repository class definition
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed May 17, 2024
1 parent 5129b8a commit f253579
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) : [];
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions tests/Bundle/DependencyInjection/SyliusResourceExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit f253579

Please sign in to comment.