1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineElastic\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use Doctrine\ORM\Mapping\Entity; |
7
|
|
|
use DoctrineElastic\ElasticEntityManager; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Andsalves <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class ElasticRepositoryManager { |
13
|
|
|
|
14
|
|
|
/** @var ElasticEntityRepository[] */ |
15
|
|
|
protected $repositoryList; |
16
|
|
|
|
17
|
|
|
/** @var AnnotationReader */ |
18
|
|
|
protected $annotationReader; |
19
|
|
|
|
20
|
|
|
public function getRepository(ElasticEntityManager $entityManager, $className) { |
21
|
|
|
$hashKey = md5($className) . spl_object_hash($entityManager); |
22
|
|
|
|
23
|
|
|
if (!isset($this->repositoryList[$hashKey])) { |
24
|
|
|
$repositoryClass = ElasticEntityRepository::class; |
25
|
|
|
|
26
|
|
|
/** @var Entity $entityAnnotation */ |
27
|
|
|
$entityAnnotation = $this->getAnnotationReader()->getClassAnnotation( |
28
|
|
|
new \ReflectionClass($className), Entity::class |
29
|
|
|
); |
30
|
|
|
if (!is_null($entityAnnotation) && isset($entityAnnotation->repositoryClass)) { |
31
|
|
|
$repositoryClass = $entityAnnotation->repositoryClass; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$repository = new $repositoryClass($entityManager, $className); |
35
|
|
|
$this->repositoryList[$hashKey] = $repository; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->repositoryList[$hashKey]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getAnnotationReader() { |
42
|
|
|
if (is_null($this->annotationReader)) { |
43
|
|
|
$this->annotationReader = new AnnotationReader(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->annotationReader; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|