1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
6
|
|
|
use FOS\ElasticaBundle\Finder\FinderInterface; |
7
|
|
|
use FOS\ElasticaBundle\Repository; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Richard Miller <[email protected]> |
12
|
|
|
* |
13
|
|
|
* Allows retrieval of basic or custom repository for mapped Doctrine |
14
|
|
|
* entities/documents. |
15
|
|
|
*/ |
16
|
|
|
class RepositoryManager implements RepositoryManagerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $types; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Repository[] |
25
|
|
|
*/ |
26
|
|
|
private $repositories; |
27
|
|
|
|
28
|
5 |
|
public function __construct() |
29
|
|
|
{ |
30
|
5 |
|
$this->types = []; |
31
|
5 |
|
$this->repositories = []; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
5 |
|
public function addType($indexTypeName, FinderInterface $finder, $repositoryName = null) |
35
|
|
|
{ |
36
|
5 |
|
$this->types[$indexTypeName] = [ |
37
|
5 |
|
'finder' => $finder, |
38
|
5 |
|
'repositoryName' => $repositoryName |
39
|
|
|
]; |
40
|
5 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return repository for entity. |
44
|
|
|
* |
45
|
|
|
* Returns custom repository if one specified otherwise |
46
|
|
|
* returns a basic repository. |
47
|
|
|
* |
48
|
|
|
* @param string $typeName |
49
|
|
|
* |
50
|
|
|
* @return Repository |
51
|
|
|
*/ |
52
|
5 |
|
public function getRepository($typeName) |
53
|
|
|
{ |
54
|
5 |
|
if (isset($this->repositories[$typeName])) { |
55
|
|
|
return $this->repositories[$typeName]; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
if (!isset($this->types[$typeName])) { |
59
|
1 |
|
throw new RuntimeException(sprintf('No search finder configured for %s', $typeName)); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
$repository = $this->createRepository($typeName); |
63
|
3 |
|
$this->repositories[$typeName] = $repository; |
64
|
|
|
|
65
|
3 |
|
return $repository; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $typeName |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
4 |
|
protected function getRepositoryName($typeName) |
74
|
|
|
{ |
75
|
4 |
|
if (isset($this->types[$typeName]['repositoryName'])) { |
76
|
3 |
|
return $this->types[$typeName]['repositoryName']; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return 'FOS\ElasticaBundle\Repository'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $typeName |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
4 |
|
private function createRepository($typeName) |
88
|
|
|
{ |
89
|
4 |
|
if (!class_exists($repositoryName = $this->getRepositoryName($typeName))) { |
90
|
1 |
|
throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $typeName)); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
return new $repositoryName($this->types[$typeName]['finder']); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|