1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Reducing dependency on RepositoryFactory by providing EntityManager as a getRepository argument.

This commit is contained in:
Guilherme Blanco 2013-06-13 23:53:53 -04:00
parent 37e7e841c3
commit a66fc03441
3 changed files with 15 additions and 37 deletions

View File

@ -161,13 +161,9 @@ use Doctrine\Common\Util\ClassUtils;
$this->metadataFactory->setEntityManager($this);
$this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl());
$repositoryFactory = $config->getRepositoryFactory();
$this->repositoryFactory = $repositoryFactory;
$this->repositoryFactory->setEntityManager($this);
$this->unitOfWork = new UnitOfWork($this);
$this->proxyFactory = new ProxyFactory(
$this->repositoryFactory = $config->getRepositoryFactory();
$this->unitOfWork = new UnitOfWork($this);
$this->proxyFactory = new ProxyFactory(
$this,
$config->getProxyDir(),
$config->getProxyNamespace(),
@ -767,7 +763,7 @@ use Doctrine\Common\Util\ClassUtils;
*/
public function getRepository($entityName)
{
return $this->repositoryFactory->getRepository($entityName);
return $this->repositoryFactory->getRepository($this, $entityName);
}
/**

View File

@ -36,23 +36,10 @@ class DefaultRepositoryFactory implements RepositoryFactory
*/
private $repositoryList = array();
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $entityManager;
/**
* {@inheritdoc}
*/
public function setEntityManager(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* {@inheritdoc}
*/
public function getRepository($entityName)
public function getRepository(EntityManagerInterface $entityManager, $entityName)
{
$entityName = ltrim($entityName, '\\');
@ -60,7 +47,7 @@ class DefaultRepositoryFactory implements RepositoryFactory
return $this->repositoryList[$entityName];
}
$repository = $this->createRepository($entityName);
$repository = $this->createRepository($entityManager, $entityName);
$this->repositoryList[$entityName] = $repository;
@ -70,20 +57,21 @@ class DefaultRepositoryFactory implements RepositoryFactory
/**
* Create a new repository instance for an entity class.
*
* @param string $entityName The name of the entity.
* @param \Doctrine\ORM\EntityManagerInterface $entityManager The EntityManager instance.
* @param string $entityName The name of the entity.
*
* @return \Doctrine\ORM\EntityRepository
*/
protected function createRepository($entityName)
protected function createRepository(EntityManagerInterface $entityManager, $entityName)
{
$metadata = $this->entityManager->getClassMetadata($entityName);
$metadata = $entityManager->getClassMetadata($entityName);
$repositoryClassName = $metadata->customRepositoryClassName;
if ($repositoryClassName === null) {
$configuration = $this->entityManager->getConfiguration();
$configuration = $entityManager->getConfiguration();
$repositoryClassName = $configuration->getDefaultRepositoryClassName();
}
return new $repositoryClassName($this->entityManager, $metadata);
return new $repositoryClassName($entityManager, $metadata);
}
}

View File

@ -29,19 +29,13 @@ use Doctrine\ORM\EntityManagerInterface;
*/
interface RepositoryFactory
{
/**
* Set the entity manager.
*
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
*/
public function setEntityManager(EntityManagerInterface $entityManager);
/**
* Gets the repository for an entity class.
*
* @param string $entityName The name of the entity.
* @param \Doctrine\ORM\EntityManagerInterface $entityManager The EntityManager instance.
* @param string $entityName The name of the entity.
*
* @return \Doctrine\ORM\EntityRepository
*/
public function getRepository($entityName);
public function getRepository(EntityManagerInterface $entityManager, $entityName);
}