/
RepositoryManager.php
77 lines (66 loc) · 2.53 KB
/
RepositoryManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\ElasticaBundle\Doctrine;
use Doctrine\Persistence\ManagerRegistry;
use FOS\ElasticaBundle\Finder\FinderInterface;
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
use FOS\ElasticaBundle\Repository;
/**
* @author Richard Miller <info@limethinking.co.uk>
*
* Allows retrieval of basic or custom repository for mapped Doctrine
* entities/documents
*/
class RepositoryManager implements RepositoryManagerInterface
{
protected array $entities = [];
protected array $repositories = [];
protected ManagerRegistry $managerRegistry;
private RepositoryManagerInterface $repositoryManager;
public function __construct(ManagerRegistry $managerRegistry, RepositoryManagerInterface $repositoryManager)
{
$this->managerRegistry = $managerRegistry;
$this->repositoryManager = $repositoryManager;
}
public function addIndex(string $indexName, FinderInterface $finder, ?string $repositoryName = null): void
{
throw new \LogicException(__METHOD__.' should not be called. Call addIndex on the main repository manager');
}
public function addEntity($entityName, $indexName): void
{
$this->entities[$entityName] = $indexName;
}
/**
* Returns custom repository if one specified otherwise returns a basic repository.
*
* {@inheritdoc}
*/
public function getRepository(string $entityName): Repository
{
$realEntityName = $entityName;
if (false !== \strpos($entityName, ':')) {
[$namespaceAlias, $simpleClassName] = \explode(':', $entityName);
// @link https://github.com/doctrine/persistence/pull/204
if (\method_exists($this->managerRegistry, 'getAliasNamespace')) {
$realEntityName = $this->managerRegistry->getAliasNamespace($namespaceAlias).'\\'.$simpleClassName;
} else {
$realEntityName = $simpleClassName.'::class';
}
}
if (isset($this->entities[$realEntityName])) {
$realEntityName = $this->entities[$realEntityName];
}
return $this->repositoryManager->getRepository($realEntityName);
}
public function hasRepository(string $indexName): bool
{
return $this->repositoryManager->hasRepository($indexName);
}
}