. */ 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(); /** * @var \Doctrine\ORM\EntityManagerInterface */ protected $entityManager; /** * {@inheritdoc} */ public function setEntityManager(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } /** * {@inheritdoc} */ public function getRepository($entityName) { $entityName = ltrim($entityName, '\\'); if (isset($this->repositoryList[$entityName])) { return $this->repositoryList[$entityName]; } $repository = $this->createRepository($entityName); $this->repositoryList[$entityName] = $repository; return $repository; } /** * Create a new repository instance for an entity class. * * @param string $entityName The name of the entity. * * @return \Doctrine\ORM\EntityRepository */ protected function createRepository($entityName) { $metadata = $this->entityManager->getClassMetadata($entityName); $repositoryClassName = $metadata->customRepositoryClassName; if ($repositoryClassName === null) { $configuration = $this->entityManager->getConfiguration(); $repositoryClassName = $configuration->getDefaultRepositoryClassName(); } return new $repositoryClassName($this->entityManager, $metadata); } }