1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Manager; |
13
|
|
|
|
14
|
|
|
use FOS\ElasticaBundle\Finder\FinderInterface; |
15
|
|
|
use FOS\ElasticaBundle\Repository; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Richard Miller <[email protected]> |
19
|
|
|
* |
20
|
|
|
* Allows retrieval of basic or custom repository for mapped Doctrine |
21
|
|
|
* entities/documents |
22
|
|
|
*/ |
23
|
|
|
class RepositoryManager implements RepositoryManagerInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $indexes = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $repositories = []; |
34
|
|
|
|
35
|
5 |
|
public function addIndex(string $indexName, FinderInterface $finder, ?string $repositoryName = null): void |
36
|
|
|
{ |
37
|
5 |
|
$this->indexes[$indexName] = [ |
38
|
5 |
|
'finder' => $finder, |
39
|
5 |
|
'repositoryName' => $repositoryName, |
40
|
|
|
]; |
41
|
5 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return repository for entity. |
45
|
|
|
* |
46
|
|
|
* Returns custom repository if one specified otherwise |
47
|
|
|
* returns a basic repository. |
48
|
|
|
*/ |
49
|
5 |
|
public function getRepository(string $indexName): Repository |
50
|
|
|
{ |
51
|
5 |
|
if (isset($this->repositories[$indexName])) { |
52
|
|
|
return $this->repositories[$indexName]; |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
if (!$this->hasRepository($indexName)) { |
56
|
1 |
|
throw new \RuntimeException(sprintf('No repository configured for %s', $indexName)); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
$repository = $this->createRepository($indexName); |
60
|
3 |
|
$this->repositories[$indexName] = $repository; |
61
|
|
|
|
62
|
3 |
|
return $repository; |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
public function hasRepository(string $indexName): bool |
66
|
|
|
{ |
67
|
5 |
|
return isset($this->indexes[$indexName]); |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
protected function getRepositoryName(string $indexName): string |
71
|
|
|
{ |
72
|
4 |
|
if (isset($this->indexes[$indexName]['repositoryName'])) { |
73
|
3 |
|
return $this->indexes[$indexName]['repositoryName']; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return 'FOS\ElasticaBundle\Repository'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
4 |
|
private function createRepository(string $indexName) |
83
|
|
|
{ |
84
|
4 |
|
if (!class_exists($repositoryName = $this->getRepositoryName($indexName))) { |
85
|
1 |
|
throw new \RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $indexName)); |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
return new $repositoryName($this->indexes[$indexName]['finder']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|