. */ namespace Doctrine\ORM\Repository; use Doctrine\ORM\EntityManagerInterface; /** * This factory is used to create default repository objects for entities at runtime. * * @author Guilherme Blanco * @since 2.5 */ class DefaultRepositoryFactory implements RepositoryFactory { /** * The list of EntityRepository instances. * * @var array<\Doctrine\ORM\EntityRepository> */ private $repositoryList = array(); /** * {@inheritdoc} */ public function getRepository(EntityManagerInterface $entityManager, $entityName) { $entityName = ltrim($entityName, '\\'); if (isset($this->repositoryList[$entityName])) { return $this->repositoryList[$entityName]; } $repository = $this->createRepository($entityManager, $entityName); $this->repositoryList[$entityName] = $repository; return $repository; } /** * Create a new repository instance for an entity class. * * @param \Doctrine\ORM\EntityManagerInterface $entityManager The EntityManager instance. * @param string $entityName The name of the entity. * * @return \Doctrine\ORM\EntityRepository */ protected function createRepository(EntityManagerInterface $entityManager, $entityName) { $metadata = $entityManager->getClassMetadata($entityName); $repositoryClassName = $metadata->customRepositoryClassName; if ($repositoryClassName === null) { $configuration = $entityManager->getConfiguration(); $repositoryClassName = $configuration->getDefaultRepositoryClassName(); } return new $repositoryClassName($entityManager, $metadata); } }