1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
<?php
declare(strict_types=1);
namespace Doctrine\ORM\Repository;
use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
use function spl_object_id;
/**
* This factory is used to create default repository objects for entities at runtime.
*/
final class DefaultRepositoryFactory implements RepositoryFactory
{
/**
* The list of EntityRepository instances.
*
* @var ObjectRepository[]
* @psalm-var array<string, ObjectRepository>
*/
private $repositoryList = [];
/**
* {@inheritdoc}
*/
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
{
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_id($entityManager);
if (isset($this->repositoryList[$repositoryHash])) {
return $this->repositoryList[$repositoryHash];
}
return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
}
/**
* Create a new repository instance for an entity class.
*
* @param EntityManagerInterface $entityManager The EntityManager instance.
* @param string $entityName The name of the entity.
*/
private function createRepository(
EntityManagerInterface $entityManager,
string $entityName
): ObjectRepository {
$metadata = $entityManager->getClassMetadata($entityName);
$repositoryClassName = $metadata->customRepositoryClassName
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
$repository = new $repositoryClassName($entityManager, $metadata);
if (! $repository instanceof EntityRepository) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9533',
'Configuring %s as repository class is deprecated because it does not extend %s.',
$repositoryClassName,
EntityRepository::class
);
}
return $repository;
}
}
|