. */ 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.4 */ final class DefaultRepositoryFactory implements RepositoryFactory { /** * The list of EntityRepository instances. * * @var \Doctrine\Common\Persistence\ObjectRepository[] */ private $repositoryList = array(); /** * {@inheritdoc} */ public function getRepository(EntityManagerInterface $entityManager, $entityName) { $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($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 \Doctrine\ORM\EntityManagerInterface $entityManager The EntityManager instance. * @param string $entityName The name of the entity. * * @return \Doctrine\Common\Persistence\ObjectRepository */ private function createRepository(EntityManagerInterface $entityManager, $entityName) { /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */ $metadata = $entityManager->getClassMetadata($entityName); $repositoryClassName = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName(); return new $repositoryClassName($entityManager, $metadata); } }