diff --git a/Repository/LazyServiceEntityRepository.php b/Repository/LazyServiceEntityRepository.php new file mode 100644 index 000000000..a725fd9a4 --- /dev/null +++ b/Repository/LazyServiceEntityRepository.php @@ -0,0 +1,62 @@ + + */ +class LazyServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface +{ + use LazyGhostTrait; + + /** + * @param string $entityClass The class name of the entity this repository manages + * @psalm-param class-string $entityClass + */ + public function __construct(ManagerRegistry $registry, string $entityClass) + { + $initializer = function ($instance, $property) use ($registry, $entityClass) { + $manager = $registry->getManagerForClass($entityClass); + + if ($manager === null) { + throw new LogicException(sprintf( + 'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.', + $entityClass + )); + } + + parent::__construct($manager, $manager->getClassMetadata($entityClass)); + + return $this->$property; + }; + + self::createLazyGhost([ + "\0*\0_em" => $initializer, + "\0*\0_class" => $initializer, + "\0*\0_entityName" => $initializer, + ], null, $this); + } +} diff --git a/Repository/ServiceEntityRepository.php b/Repository/ServiceEntityRepository.php index dd274065f..9a94e6d8b 100644 --- a/Repository/ServiceEntityRepository.php +++ b/Repository/ServiceEntityRepository.php @@ -5,43 +5,51 @@ use Doctrine\ORM\EntityRepository; use Doctrine\Persistence\ManagerRegistry; use LogicException; +use Symfony\Component\VarExporter\LazyGhostTrait; use function sprintf; +use function trait_exists; -/** - * Optional EntityRepository base class with a simplified constructor (for autowiring). - * - * To use in your class, inject the "registry" service and call - * the parent constructor. For example: - * - * class YourEntityRepository extends ServiceEntityRepository - * { - * public function __construct(ManagerRegistry $registry) - * { - * parent::__construct($registry, YourEntity::class); - * } - * } - * - * @template T of object - * @template-extends EntityRepository - */ -class ServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface -{ +if (trait_exists(LazyGhostTrait::class)) { + class ServiceEntityRepository extends LazyServiceEntityRepository + { + } +} else { /** - * @param string $entityClass The class name of the entity this repository manages - * @psalm-param class-string $entityClass + * Optional EntityRepository base class with a simplified constructor (for autowiring). + * + * To use in your class, inject the "registry" service and call + * the parent constructor. For example: + * + * class YourEntityRepository extends ServiceEntityRepository + * { + * public function __construct(ManagerRegistry $registry) + * { + * parent::__construct($registry, YourEntity::class); + * } + * } + * + * @template T of object + * @template-extends EntityRepository */ - public function __construct(ManagerRegistry $registry, string $entityClass) + class ServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface { - $manager = $registry->getManagerForClass($entityClass); + /** + * @param string $entityClass The class name of the entity this repository manages + * @psalm-param class-string $entityClass + */ + public function __construct(ManagerRegistry $registry, string $entityClass) + { + $manager = $registry->getManagerForClass($entityClass); - if ($manager === null) { - throw new LogicException(sprintf( - 'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.', - $entityClass - )); - } + if ($manager === null) { + throw new LogicException(sprintf( + 'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.', + $entityClass + )); + } - parent::__construct($manager, $manager->getClassMetadata($entityClass)); + parent::__construct($manager, $manager->getClassMetadata($entityClass)); + } } } diff --git a/Tests/Repository/ServiceEntityRepositoryTest.php b/Tests/Repository/ServiceEntityRepositoryTest.php index 5dc9e4067..dcd6952aa 100644 --- a/Tests/Repository/ServiceEntityRepositoryTest.php +++ b/Tests/Repository/ServiceEntityRepositoryTest.php @@ -30,6 +30,7 @@ public function testConstructorThrowsExceptionWhenNoManagerFound(): void EXCEPTION ); /** @psalm-suppress UndefinedClass */ - new ServiceEntityRepository($registry, TestEntity::class); + $repo = new ServiceEntityRepository($registry, TestEntity::class); + $repo->getClassName(); } } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 39a1b37a8..294705c59 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -24,6 +24,7 @@ Tests/* + Repository/ServiceEntityRepository.php Tests/* diff --git a/psalm.xml.dist b/psalm.xml.dist index cea4dab3b..a725fe9d0 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -51,6 +51,7 @@ + @@ -62,5 +63,17 @@ + + + + + + + + + + + +