-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSiteAccessAwareEntityManager.php
More file actions
344 lines (291 loc) · 8.4 KB
/
Copy pathSiteAccessAwareEntityManager.php
File metadata and controls
344 lines (291 loc) · 8.4 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Core\Persistence\Doctrine;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Cache;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\NativeQuery;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\FilterCollection;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\UnitOfWork;
use Ibexa\Bundle\Core\Entity\EntityManagerFactory;
use Ibexa\Contracts\Core\MVC\EventSubscriber\ConfigScopeChangeSubscriber;
use Ibexa\Core\MVC\Symfony\Event\ScopeChangeEvent;
use Symfony\Contracts\Service\ResetInterface;
/**
* @internal
*
* SiteAccessAwareEntityManager is a cross-compatible class supporting doctrine/persistence v2 and v3.
*/
final class SiteAccessAwareEntityManager implements EntityManagerInterface, ConfigScopeChangeSubscriber, ResetInterface
{
private EntityManagerFactory $entityManagerFactory;
private ?EntityManagerInterface $resolvedEntityManager = null;
public function __construct(EntityManagerFactory $entityManagerFactory)
{
$this->entityManagerFactory = $entityManagerFactory;
}
public function onConfigScopeChange(ScopeChangeEvent $event): void
{
$this->resolvedEntityManager = null;
}
public function reset(): void
{
$this->resolvedEntityManager = null;
}
private function getWrapped(): EntityManagerInterface
{
return $this->resolvedEntityManager ??= $this->entityManagerFactory->getEntityManager();
}
public function getConnection(): Connection
{
return $this->getWrapped()->getConnection();
}
public function getExpressionBuilder(): Expr
{
return $this->getWrapped()->getExpressionBuilder();
}
public function beginTransaction(): void
{
$this->getWrapped()->beginTransaction();
}
public function transactional($func)
{
return $this->getWrapped()->transactional($func);
}
/**
* @return mixed
*/
public function wrapInTransaction(callable $func)
{
return $this->getWrapped()->wrapInTransaction($func);
}
public function commit(): void
{
$this->getWrapped()->commit();
}
public function rollback(): void
{
$this->getWrapped()->rollback();
}
public function createQuery($dql = ''): Query
{
return $this->getWrapped()->createQuery($dql);
}
public function createNamedQuery($name): Query
{
return $this->getWrapped()->createNamedQuery($name);
}
public function createNativeQuery($sql, ResultSetMapping $rsm): NativeQuery
{
return $this->getWrapped()->createNativeQuery($sql, $rsm);
}
public function createNamedNativeQuery($name): NativeQuery
{
return $this->getWrapped()->createNamedNativeQuery($name);
}
public function createQueryBuilder(): QueryBuilder
{
return $this->getWrapped()->createQueryBuilder();
}
/**
* @template T of object
*
* @param class-string<T> $entityName
*
* @return T|null
*/
public function getReference($entityName, $id): ?object
{
return $this->getWrapped()->getReference($entityName, $id);
}
/**
* @template T of object
*
* @param class-string<T> $entityName
*
* @return T|null
*/
public function getPartialReference($entityName, $identifier): ?object
{
return $this->getWrapped()->getPartialReference($entityName, $identifier);
}
public function close(): void
{
$this->getWrapped()->close();
}
/**
* @template T of object
*
* @param T $entity
* @param bool $deep
*
* @return T
*/
public function copy($entity, $deep = false): object
{
/** @var T */
return $this->getWrapped()->copy($entity, $deep);
}
public function lock($entity, $lockMode, $lockVersion = null): void
{
$this->getWrapped()->lock($entity, $lockMode, $lockVersion);
}
public function getEventManager(): \Doctrine\Common\EventManager
{
return $this->getWrapped()->getEventManager();
}
public function getConfiguration(): Configuration
{
return $this->getWrapped()->getConfiguration();
}
public function isOpen(): bool
{
return $this->getWrapped()->isOpen();
}
public function getUnitOfWork(): UnitOfWork
{
return $this->getWrapped()->getUnitOfWork();
}
public function getHydrator($hydrationMode): AbstractHydrator
{
return $this->getWrapped()->getHydrator($hydrationMode);
}
public function newHydrator($hydrationMode): AbstractHydrator
{
return $this->getWrapped()->newHydrator($hydrationMode);
}
public function getProxyFactory(): ProxyFactory
{
return $this->getWrapped()->getProxyFactory();
}
public function getFilters(): FilterCollection
{
return $this->getWrapped()->getFilters();
}
public function isFiltersStateClean(): bool
{
return $this->getWrapped()->isFiltersStateClean();
}
public function hasFilters(): bool
{
return $this->getWrapped()->hasFilters();
}
public function getCache(): ?Cache
{
return $this->getWrapped()->getCache();
}
/**
* @template T of object
*
* @phpstan-param class-string<T> $className
* @phpstan-param \Doctrine\DBAL\LockMode::*|null $lockMode
*
* @param string $className
* @param mixed $id
* @param int|null $lockMode
* @param int|null $lockVersion
*
* @return T|null
*/
public function find($className, $id, ?int $lockMode = null, ?int $lockVersion = null): ?object
{
return $this->getWrapped()->find($className, $id, $lockMode, $lockVersion);
}
/**
* @param object $object
*/
public function persist($object): void
{
$this->getWrapped()->persist($object);
}
public function remove($object): void
{
$this->getWrapped()->remove($object);
}
/**
* @param string|null $objectName
*/
public function clear($objectName = null): void
{
$this->getWrapped()->clear($objectName);
}
/**
* @param object $object
*/
public function detach($object): void
{
$this->getWrapped()->detach($object);
}
public function refresh($object, ?int $lockMode = null): void
{
$this->getWrapped()->refresh($object, $lockMode);
}
public function flush(): void
{
$this->getWrapped()->flush();
}
public function getRepository($className): EntityRepository
{
return $this->getWrapped()->getRepository($className);
}
public function getClassMetadata($className): ClassMetadata
{
return $this->getWrapped()->getClassMetadata($className);
}
public function getMetadataFactory(): ClassMetadataFactory
{
return $this->getWrapped()->getMetadataFactory();
}
/**
* @param object $obj
*/
public function initializeObject($obj): void
{
$this->getWrapped()->initializeObject($obj);
}
/**
* @param mixed $value
*/
public function isUninitializedObject($value): bool
{
$entityManager = $this->getWrapped();
// workaround for doctrine/persistence v2 and v3 cross-compatibility
return method_exists($entityManager, 'isUninitializedObject')
? $entityManager->isUninitializedObject($value)
: false;
}
/**
* @param object $object
*/
public function contains($object): bool
{
return $this->getWrapped()->contains($object);
}
/**
* @param object $object
*
* @return object|null
*/
public function merge($object)
{
$entityManager = $this->getWrapped();
// workaround for doctrine/persistence v2 and v3 cross-compatibility
return method_exists($entityManager, 'merge')
? $entityManager->merge($object)
: null;
}
}