|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Doctrine\ORM\EntityRepository; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractEntityRepositoryFactory |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var EntityManagerInterface |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $entityManager; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \ReflectionClass |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $reflectionClass; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var EntityRepository |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $entityRepository; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $repositoryFactoryFqn; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* AbstractEntityRepositoryFactory constructor. |
|
32
|
|
|
* @param EntityManagerInterface $entityManager |
|
33
|
|
|
* @throws \ReflectionException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->entityManager = $entityManager; |
|
38
|
|
|
$this->reflectionClass = new \ReflectionClass($this); |
|
39
|
|
|
$this->repositoryFactoryFqn = $this->reflectionClass->getName(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getRepository() |
|
43
|
|
|
{ |
|
44
|
|
|
if (null === $this->entityRepository) { |
|
45
|
|
|
$entityFqn = $this->getEntityFqn(); |
|
46
|
|
|
$repositoryFqn = $this->getRepositoryFqn(); |
|
47
|
|
|
$metaData = $this->entityManager->getClassMetadata($entityFqn); |
|
48
|
|
|
|
|
49
|
|
|
$this->entityRepository = new $repositoryFqn($this->entityManager, $metaData); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->entityRepository; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function getEntityFqn() |
|
56
|
|
|
{ |
|
57
|
|
|
return '\\'.\str_replace( |
|
58
|
|
|
['Entity\\Repositories', 'RepositoryFactory'], |
|
59
|
|
|
['Entities', ''], |
|
60
|
|
|
$this->repositoryFactoryFqn |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getRepositoryFqn() |
|
65
|
|
|
{ |
|
66
|
|
|
return '\\'.\str_replace( |
|
67
|
|
|
'Factory', |
|
68
|
|
|
'', |
|
69
|
|
|
$this->repositoryFactoryFqn |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|