1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xsolve\Associate; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Xsolve\Associate\AssociationCollecting\BasicAssociationCollectingStrategy; |
7
|
|
|
use Xsolve\Associate\AssociationCollecting\DoctrineOrmAssociationCollectingStrategy; |
8
|
|
|
use Xsolve\Associate\BufferedCollector\BufferedCollector; |
9
|
|
|
use Xsolve\Associate\BufferedCollector\BufferedCollectorInterface; |
10
|
|
|
use Xsolve\Associate\CollectionTraversal\ArrayCollectionTraversalStrategy; |
11
|
|
|
use Xsolve\Associate\CollectionTraversal\DoctrineOrmCollectionCollectionTraversalStrategy; |
12
|
|
|
use Xsolve\Associate\CollectionTraversal\TraversableCollectionTraversalStrategy; |
13
|
|
|
use Xsolve\Associate\Loader\DoctrineOrmEntityLoader; |
14
|
|
|
use Xsolve\Associate\Loader\DoctrineOrmNonProxiedAssociationQueryExecutor; |
15
|
|
|
use Xsolve\Associate\Loader\DoctrineOrmUninitializedProxiesQueryExecutor; |
16
|
|
|
use Xsolve\Associate\Metadata\MetadataWrapperProvider; |
17
|
|
|
|
18
|
|
|
// TODO Make separate facade for Doctrine. |
19
|
|
|
class Facade |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EntityManagerInterface |
23
|
|
|
*/ |
24
|
|
|
protected $entityManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var MetadataWrapperProvider |
28
|
|
|
*/ |
29
|
|
|
protected $metadataWrapperProvider; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var BasicAssociationCollectingStrategy |
33
|
|
|
*/ |
34
|
|
|
protected $basicAssociationCollectingStrategy; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DoctrineOrmAssociationCollectingStrategy |
38
|
|
|
*/ |
39
|
|
|
protected $doctrineOrmAssociationCollectingStrategy; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var DoctrineOrmEntityLoader |
43
|
|
|
*/ |
44
|
|
|
protected $doctrineOrmEntityLoader; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var CollectorInterface |
48
|
|
|
*/ |
49
|
|
|
protected $basicCollector; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var CollectorInterface |
53
|
|
|
*/ |
54
|
|
|
protected $doctrineOrmCollector; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var BufferedCollector |
58
|
|
|
*/ |
59
|
|
|
protected $bufferedCollector; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param EntityManagerInterface|null $entityManager |
63
|
|
|
*/ |
64
|
|
|
public function __construct(EntityManagerInterface $entityManager = null) |
65
|
|
|
{ |
66
|
|
|
$this->entityManager = $entityManager; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return CollectorInterface |
71
|
|
|
*/ |
72
|
|
|
public function getBasicCollector(): CollectorInterface |
73
|
|
|
{ |
74
|
|
|
if (!$this->basicCollector instanceof CollectorInterface) { |
75
|
|
|
$this->basicCollector = new Collector( |
76
|
|
|
$this->getBasicAssociationCollectingStrategy() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->basicCollector; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return CollectorInterface |
85
|
|
|
*/ |
86
|
|
|
public function getDoctrineOrmCollector(): CollectorInterface |
87
|
|
|
{ |
88
|
|
|
if (!$this->doctrineOrmCollector instanceof CollectorInterface) { |
89
|
|
|
$this->doctrineOrmCollector = new Collector( |
90
|
|
|
$this->getDoctrineOrmAssociationCollectingStrategy() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->doctrineOrmCollector; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return BufferedCollectorInterface |
99
|
|
|
*/ |
100
|
|
|
public function getBufferedDoctrineOrmCollector(): BufferedCollectorInterface |
101
|
|
|
{ |
102
|
|
|
if (!$this->bufferedCollector instanceof BufferedCollector) { |
103
|
|
|
$this->bufferedCollector = new BufferedCollector( |
104
|
|
|
$this->getMetadataWrapperProvider(), |
105
|
|
|
$this->getDoctrineOrmCollector() |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->bufferedCollector; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return MetadataWrapperProvider |
114
|
|
|
*/ |
115
|
|
|
protected function getMetadataWrapperProvider(): MetadataWrapperProvider |
116
|
|
|
{ |
117
|
|
|
$this->assertEntityManagerAvailable(); |
118
|
|
|
if (!$this->metadataWrapperProvider instanceof MetadataWrapperProvider) { |
119
|
|
|
$this->metadataWrapperProvider = new MetadataWrapperProvider($this->entityManager); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->metadataWrapperProvider; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return BasicAssociationCollectingStrategy |
127
|
|
|
*/ |
128
|
|
|
protected function getBasicAssociationCollectingStrategy(): BasicAssociationCollectingStrategy |
129
|
|
|
{ |
130
|
|
|
if (!$this->basicAssociationCollectingStrategy instanceof BasicAssociationCollectingStrategy) { |
131
|
|
|
$this->basicAssociationCollectingStrategy = new BasicAssociationCollectingStrategy(); |
132
|
|
|
$this->basicAssociationCollectingStrategy->addCollectionTraversalStrategy( |
133
|
|
|
new ArrayCollectionTraversalStrategy() |
134
|
|
|
); |
135
|
|
|
$this->basicAssociationCollectingStrategy->addCollectionTraversalStrategy( |
136
|
|
|
new TraversableCollectionTraversalStrategy() |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->basicAssociationCollectingStrategy; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return DoctrineOrmAssociationCollectingStrategy |
145
|
|
|
*/ |
146
|
|
|
protected function getDoctrineOrmAssociationCollectingStrategy(): DoctrineOrmAssociationCollectingStrategy |
147
|
|
|
{ |
148
|
|
|
if (!$this->doctrineOrmAssociationCollectingStrategy instanceof DoctrineOrmAssociationCollectingStrategy) { |
149
|
|
|
$this->doctrineOrmAssociationCollectingStrategy = new DoctrineOrmAssociationCollectingStrategy( |
150
|
|
|
$this->getMetadataWrapperProvider(), |
151
|
|
|
$this->getDoctrineOrmEntityLoader() |
152
|
|
|
); |
153
|
|
|
$this->doctrineOrmAssociationCollectingStrategy->addCollectionTraversalStrategy( |
154
|
|
|
new ArrayCollectionTraversalStrategy() |
155
|
|
|
); |
156
|
|
|
$this->doctrineOrmAssociationCollectingStrategy->addCollectionTraversalStrategy( |
157
|
|
|
new TraversableCollectionTraversalStrategy() |
158
|
|
|
); |
159
|
|
|
$this->doctrineOrmAssociationCollectingStrategy->addCollectionTraversalStrategy( |
160
|
|
|
new DoctrineOrmCollectionCollectionTraversalStrategy() |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->doctrineOrmAssociationCollectingStrategy; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return DoctrineOrmEntityLoader |
169
|
|
|
*/ |
170
|
|
|
protected function getDoctrineOrmEntityLoader(): DoctrineOrmEntityLoader |
171
|
|
|
{ |
172
|
|
|
if (!$this->doctrineOrmEntityLoader instanceof DoctrineOrmEntityLoader) { |
173
|
|
|
$this->doctrineOrmEntityLoader = new DoctrineOrmEntityLoader( |
174
|
|
|
$this->getMetadataWrapperProvider(), |
175
|
|
|
new BasicAssociationCollectingStrategy(), |
176
|
|
|
new DoctrineOrmUninitializedProxiesQueryExecutor(), |
177
|
|
|
new DoctrineOrmNonProxiedAssociationQueryExecutor() |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->doctrineOrmEntityLoader; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @throws \Exception |
186
|
|
|
*/ |
187
|
|
|
protected function assertEntityManagerAvailable() |
188
|
|
|
{ |
189
|
|
|
if (!$this->entityManager instanceof EntityManagerInterface) { |
190
|
|
|
throw new \Exception('Entity manager not available.'); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|