1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasEntity\Service; |
6
|
|
|
|
7
|
|
|
use Arp\DoctrineEntityRepository\EntityRepositoryInterface; |
8
|
|
|
use Arp\DoctrineEntityRepository\EntityRepositoryProviderInterface; |
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
10
|
|
|
use Doctrine\ORM\Repository\RepositoryFactory; |
11
|
|
|
use Doctrine\Persistence\ObjectRepository; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Alex Patterson <[email protected]> |
15
|
|
|
* @package Arp\LaminasEntity\Service |
16
|
|
|
*/ |
17
|
|
|
class EntityRepositoryFactory implements RepositoryFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityRepositoryProviderInterface |
21
|
|
|
*/ |
22
|
|
|
private EntityRepositoryProviderInterface $repositoryProvider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The default (fallback) repository factory. |
26
|
|
|
* |
27
|
|
|
* @var RepositoryFactory |
28
|
|
|
*/ |
29
|
|
|
private RepositoryFactory $repositoryFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param EntityRepositoryProviderInterface $repositoryProvider |
33
|
|
|
* @param RepositoryFactory $repositoryFactory |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
EntityRepositoryProviderInterface $repositoryProvider, |
37
|
|
|
RepositoryFactory $repositoryFactory |
38
|
|
|
) { |
39
|
|
|
$this->repositoryProvider = $repositoryProvider; |
40
|
|
|
$this->repositoryFactory = $repositoryFactory; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param EntityManagerInterface $entityManager |
45
|
|
|
* @param string $entityName |
46
|
|
|
* |
47
|
|
|
* @return EntityRepositoryInterface|ObjectRepository |
48
|
|
|
* |
49
|
|
|
* @throws \Throwable |
50
|
|
|
*/ |
51
|
|
|
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository |
52
|
|
|
{ |
53
|
|
|
if ($this->repositoryProvider->hasRepository($entityName)) { |
54
|
|
|
$options = [ |
55
|
|
|
'entity_name' => $entityName, |
56
|
|
|
'entity_manager' => $entityManager, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
return $this->repositoryProvider->getRepository($entityName, $options); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->repositoryFactory->getRepository($entityManager, $entityName); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|