|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class AbstractEntityRepository |
|
12
|
|
|
* |
|
13
|
|
|
* This provides a base class that handles instantiating the correctly configured EntityRepository and provides an |
|
14
|
|
|
* extensible baseline for further customisation |
|
15
|
|
|
* |
|
16
|
|
|
* We have extracted an interface from the standard Doctrine EntityRepository and implement that |
|
17
|
|
|
* |
|
18
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories |
|
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
20
|
|
|
* @SuppressWarnings(PHPMD.ExcessivePublicCount) |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractEntityRepository implements EntityRepositoryInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntityManager |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $entityManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var EntityRepository |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $entityRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $repositoryFactoryFqn; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var ClassMetadata|null |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $metaData; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* AbstractEntityRepositoryFactory constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param EntityManager $entityManager |
|
47
|
|
|
* @param ClassMetadata|null $metaData |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(EntityManager $entityManager, ?ClassMetadata $metaData) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->entityManager = $entityManager; |
|
52
|
|
|
$this->metaData = $metaData; |
|
53
|
|
|
$this->initRepository(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function initRepository(): void |
|
57
|
|
|
{ |
|
58
|
|
|
if (null === $this->metaData) { |
|
59
|
|
|
$entityFqn = $this->getEntityFqn(); |
|
60
|
|
|
$this->metaData = $this->entityManager->getClassMetadata($entityFqn); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->entityRepository = new EntityRepository($this->entityManager, $this->metaData); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function getEntityFqn(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return '\\'.\str_replace( |
|
69
|
|
|
[ |
|
70
|
|
|
'Entity\\Repositories', |
|
71
|
|
|
'Repository', |
|
72
|
|
|
], |
|
73
|
|
|
[ |
|
74
|
|
|
'Entities', |
|
75
|
|
|
'', |
|
76
|
|
|
], |
|
77
|
|
|
static::class |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function find($id, $lockMode = null, $lockVersion = null) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->entityRepository->find($id); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function findAll() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->entityRepository->findAll(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function findOneBy(array $criteria, array $orderBy = null) |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->entityRepository->findOneBy($criteria, $orderBy); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getClassName() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->entityRepository->getClassName(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function matching(Criteria $criteria) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->entityRepository->matching($criteria); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function createQueryBuilder($alias, $indexBy = null) |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->entityRepository->createQueryBuilder($alias, $indexBy); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function createResultSetMappingBuilder($alias) |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->entityRepository->createResultSetMappingBuilder($alias); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function createNamedQuery($queryName) |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->entityRepository->createNamedQuery($queryName); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function createNativeNamedQuery($queryName) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->entityRepository->createNativeNamedQuery($queryName); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function clear() |
|
132
|
|
|
{ |
|
133
|
|
|
$this->entityRepository->clear(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function count(array $criteria) |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->entityRepository->count($criteria); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|