Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repository class definition #877

Draft
wants to merge 2 commits into
base: 1.11
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion tests/Bundle/Resource/ResourceServicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member Author

@loic425 loic425 May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suspicious...
Why the instance is different...

}
}
Loading