Extracted the Doctine specific parts of the Repository Manager
This commit is contained in:
parent
8487ab1788
commit
ba976e3a41
4 changed files with 219 additions and 104 deletions
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace FOQ\ElasticaBundle\Manager;
|
||||
|
||||
use Doctrine\Common\Annotations\Reader;
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use FOQ\ElasticaBundle\Finder\FinderInterface;
|
||||
use FOQ\ElasticaBundle\Repository;
|
||||
use RuntimeException;
|
||||
|
|
@ -17,14 +15,6 @@ class RepositoryManager implements RepositoryManagerInterface
|
|||
{
|
||||
protected $entities = array();
|
||||
protected $repositories = array();
|
||||
protected $managerRegistry;
|
||||
protected $reader;
|
||||
|
||||
public function __construct(ManagerRegistry $managerRegistry, Reader $reader)
|
||||
{
|
||||
$this->managerRegistry = $managerRegistry;
|
||||
$this->reader = $reader;
|
||||
}
|
||||
|
||||
public function addEntity($entityName, FinderInterface $finder, $repositoryName = null)
|
||||
{
|
||||
|
|
@ -41,49 +31,34 @@ class RepositoryManager implements RepositoryManagerInterface
|
|||
*/
|
||||
public function getRepository($entityName)
|
||||
{
|
||||
$realEntityName = $entityName;
|
||||
if (strpos($entityName, ':') !== false) {
|
||||
list($namespaceAlias, $simpleClassName) = explode(':', $entityName);
|
||||
$realEntityName = $this->managerRegistry->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
|
||||
if (isset($this->repositories[$entityName])) {
|
||||
return $this->repositories[$entityName];
|
||||
}
|
||||
|
||||
if (isset($this->repositories[$realEntityName])) {
|
||||
return $this->repositories[$realEntityName];
|
||||
if (!isset($this->entities[$entityName])) {
|
||||
throw new RuntimeException(sprintf('No search finder configured for %s', $entityName));
|
||||
}
|
||||
|
||||
if (!isset($this->entities[$realEntityName])) {
|
||||
throw new RuntimeException(sprintf('No search finder configured for %s', $realEntityName));
|
||||
}
|
||||
$repositoryName = $this->getCustomRepositoryName($entityName);
|
||||
|
||||
$this->setRepositoryFromAnnotation($realEntityName);
|
||||
|
||||
if (isset($this->entities[$realEntityName]['repositoryName'])) {
|
||||
|
||||
$repositoryName = $this->entities[$realEntityName]['repositoryName'];
|
||||
if ($repositoryName) {
|
||||
if (!class_exists($repositoryName)) {
|
||||
throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $realEntityName));
|
||||
throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $entityName));
|
||||
}
|
||||
$repository = new $repositoryName($this->entities[$realEntityName]['finder']);
|
||||
$this->repositories[$realEntityName] = $repository;
|
||||
$repository = new $repositoryName($this->entities[$entityName]['finder']);
|
||||
$this->repositories[$entityName] = $repository;
|
||||
return $repository;
|
||||
}
|
||||
|
||||
$repository = new Repository($this->entities[$realEntityName]['finder']);
|
||||
$this->repositories[$realEntityName] = $repository;
|
||||
$repository = new Repository($this->entities[$entityName]['finder']);
|
||||
$this->repositories[$entityName] = $repository;
|
||||
return $repository;
|
||||
}
|
||||
|
||||
private function setRepositoryFromAnnotation($realEntityName)
|
||||
protected function getCustomRepositoryName($realEntityName)
|
||||
{
|
||||
if (isset($this->entities[$realEntityName]['repositoryName'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$refClass = new \ReflectionClass($realEntityName);
|
||||
$annotation = $this->reader->getClassAnnotation($refClass, 'FOQ\\ElasticaBundle\\Configuration\\Search');
|
||||
if ($annotation) {
|
||||
$this->entities[$realEntityName]['repositoryName']
|
||||
= $annotation->repositoryClass;
|
||||
return $this->entities[$realEntityName]['repositoryName'];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue