Last active
August 28, 2015 08:35
-
-
Save maglnet/36e1c963f598fa748879 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
'doctrine' => array( | |
'configuration' => array( | |
'orm_default' => array( | |
'repository_factory' => 'ServiceLocatorRepositoryFactoryFactory' | |
), | |
), | |
), | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\EntityManagerInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class ServiceLocatorRepositoryFactory implements \Doctrine\ORM\Repository\RepositoryFactory | |
{ | |
/** | |
* @var ServiceLocatorInterface | |
*/ | |
private $serviceLocator; | |
/** | |
* The list of EntityRepository instances. | |
* | |
* @var \Doctrine\Common\Persistence\ObjectRepository[] | |
*/ | |
private $repositoryList = array(); | |
public function __construct(ServiceLocatorInterface $serviceLocator) | |
{ | |
$this->serviceLocator = $serviceLocator; | |
} | |
/** | |
* {@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); | |
if($metadata->customRepositoryClassName && $this->serviceLocator->has($metadata->customRepositoryClassName)) { | |
return $this->serviceLocator->get($metadata->customRepositoryClassName); | |
} | |
$repositoryClassName = $metadata->customRepositoryClassName | |
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName(); | |
return new $repositoryClassName($entityManager, $metadata); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Zend\ServiceManager\FactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class ServiceLocatorRepositoryFactoryFactory implements FactoryInterface | |
{ | |
/** | |
* Create service | |
* | |
* @param ServiceLocatorInterface $serviceLocator | |
* @return mixed | |
*/ | |
public function createService(ServiceLocatorInterface $serviceLocator) | |
{ | |
return new ServiceLocatorRepositoryFactory($serviceLocator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment