1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\EventManager; |
8
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
9
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
10
|
|
|
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory; |
11
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
12
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; |
13
|
|
|
use Doctrine\ODM\MongoDB\Mapping\MappingException; |
14
|
|
|
use Doctrine\ODM\MongoDB\Proxy\ClassNameResolver; |
15
|
|
|
use Doctrine\ODM\MongoDB\Proxy\Factory\ProxyFactory; |
16
|
|
|
use Doctrine\ODM\MongoDB\Proxy\Factory\StaticProxyFactory; |
17
|
|
|
use Doctrine\ODM\MongoDB\Query\FilterCollection; |
18
|
|
|
use Doctrine\ODM\MongoDB\Repository\DocumentRepository; |
19
|
|
|
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
use MongoDB\Client; |
22
|
|
|
use MongoDB\Collection; |
23
|
|
|
use MongoDB\Database; |
24
|
|
|
use MongoDB\Driver\ReadPreference; |
25
|
|
|
use MongoDB\GridFS\Bucket; |
26
|
|
|
use RuntimeException; |
27
|
|
|
use function array_search; |
28
|
|
|
use function assert; |
29
|
|
|
use function get_class; |
30
|
|
|
use function gettype; |
31
|
|
|
use function is_object; |
32
|
|
|
use function ltrim; |
33
|
|
|
use function sprintf; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The DocumentManager class is the central access point for managing the |
37
|
|
|
* persistence of documents. |
38
|
|
|
* |
39
|
|
|
* <?php |
40
|
|
|
* |
41
|
|
|
* $config = new Configuration(); |
42
|
|
|
* $dm = DocumentManager::create(new Connection(), $config); |
43
|
|
|
*/ |
44
|
|
|
class DocumentManager implements ObjectManager |
45
|
|
|
{ |
46
|
|
|
public const CLIENT_TYPEMAP = ['root' => 'array', 'document' => 'array']; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The Doctrine MongoDB connection instance. |
50
|
|
|
* |
51
|
|
|
* @var Client |
52
|
|
|
*/ |
53
|
|
|
private $client; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The used Configuration. |
57
|
|
|
* |
58
|
|
|
* @var Configuration |
59
|
|
|
*/ |
60
|
|
|
private $config; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The metadata factory, used to retrieve the ODM metadata of document classes. |
64
|
|
|
* |
65
|
|
|
* @var ClassMetadataFactory |
66
|
|
|
*/ |
67
|
|
|
private $metadataFactory; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The UnitOfWork used to coordinate object-level transactions. |
71
|
|
|
* |
72
|
|
|
* @var UnitOfWork |
73
|
|
|
*/ |
74
|
|
|
private $unitOfWork; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The event manager that is the central point of the event system. |
78
|
|
|
* |
79
|
|
|
* @var EventManager |
80
|
|
|
*/ |
81
|
|
|
private $eventManager; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The Hydrator factory instance. |
85
|
|
|
* |
86
|
|
|
* @var HydratorFactory |
87
|
|
|
*/ |
88
|
|
|
private $hydratorFactory; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The Proxy factory instance. |
92
|
|
|
* |
93
|
|
|
* @var ProxyFactory |
94
|
|
|
*/ |
95
|
|
|
private $proxyFactory; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The repository factory used to create dynamic repositories. |
99
|
|
|
* |
100
|
|
|
* @var RepositoryFactory |
101
|
|
|
*/ |
102
|
|
|
private $repositoryFactory; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* SchemaManager instance |
106
|
|
|
* |
107
|
|
|
* @var SchemaManager |
108
|
|
|
*/ |
109
|
|
|
private $schemaManager; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Array of cached document database instances that are lazily loaded. |
113
|
|
|
* |
114
|
|
|
* @var Database[] |
115
|
|
|
*/ |
116
|
|
|
private $documentDatabases = []; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Array of cached document collection instances that are lazily loaded. |
120
|
|
|
* |
121
|
|
|
* @var Collection[] |
122
|
|
|
*/ |
123
|
|
|
private $documentCollections = []; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Array of cached document bucket instances that are lazily loaded. |
127
|
|
|
* |
128
|
|
|
* @var Bucket[] |
129
|
|
|
*/ |
130
|
|
|
private $documentBuckets = []; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Whether the DocumentManager is closed or not. |
134
|
|
|
* |
135
|
|
|
* @var bool |
136
|
|
|
*/ |
137
|
|
|
private $closed = false; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Collection of query filters. |
141
|
|
|
* |
142
|
|
|
* @var FilterCollection |
143
|
|
|
*/ |
144
|
|
|
private $filterCollection; |
145
|
|
|
|
146
|
|
|
/** @var ClassNameResolver */ |
147
|
|
|
private $classNameResolver; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Creates a new Document that operates on the given Mongo connection |
151
|
|
|
* and uses the given Configuration. |
152
|
|
|
*/ |
153
|
1081 |
|
protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
154
|
|
|
{ |
155
|
1081 |
|
$this->config = $config ?: new Configuration(); |
156
|
1081 |
|
$this->eventManager = $eventManager ?: new EventManager(); |
157
|
1081 |
|
$this->client = $client ?: new Client('mongodb://127.0.0.1', [], ['typeMap' => self::CLIENT_TYPEMAP]); |
158
|
|
|
|
159
|
1081 |
|
$this->checkTypeMap(); |
160
|
|
|
|
161
|
1081 |
|
$metadataFactoryClassName = $this->config->getClassMetadataFactoryName(); |
162
|
1081 |
|
$this->metadataFactory = new $metadataFactoryClassName(); |
163
|
1081 |
|
$this->metadataFactory->setDocumentManager($this); |
164
|
1081 |
|
$this->metadataFactory->setConfiguration($this->config); |
165
|
|
|
|
166
|
1081 |
|
$cacheDriver = $this->config->getMetadataCacheImpl(); |
167
|
1081 |
|
if ($cacheDriver) { |
168
|
|
|
$this->metadataFactory->setCacheDriver($cacheDriver); |
169
|
|
|
} |
170
|
|
|
|
171
|
1081 |
|
$hydratorDir = $this->config->getHydratorDir(); |
172
|
1081 |
|
$hydratorNs = $this->config->getHydratorNamespace(); |
173
|
1081 |
|
$this->hydratorFactory = new HydratorFactory( |
174
|
1081 |
|
$this, |
175
|
1081 |
|
$this->eventManager, |
176
|
1081 |
|
$hydratorDir, |
177
|
1081 |
|
$hydratorNs, |
178
|
1081 |
|
$this->config->getAutoGenerateHydratorClasses() |
179
|
|
|
); |
180
|
|
|
|
181
|
1081 |
|
$this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory); |
182
|
1081 |
|
$this->hydratorFactory->setUnitOfWork($this->unitOfWork); |
183
|
1081 |
|
$this->schemaManager = new SchemaManager($this, $this->metadataFactory); |
184
|
1081 |
|
$this->proxyFactory = new StaticProxyFactory($this); |
185
|
1081 |
|
$this->repositoryFactory = $this->config->getRepositoryFactory(); |
186
|
1081 |
|
$this->classNameResolver = new ClassNameResolver($this->config); |
187
|
1081 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Gets the proxy factory used by the DocumentManager to create document proxies. |
191
|
|
|
*/ |
192
|
1 |
|
public function getProxyFactory() : ProxyFactory |
193
|
|
|
{ |
194
|
1 |
|
return $this->proxyFactory; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Creates a new Document that operates on the given Mongo connection |
199
|
|
|
* and uses the given Configuration. |
200
|
|
|
*/ |
201
|
1081 |
|
public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) : DocumentManager |
202
|
|
|
{ |
203
|
1081 |
|
return new static($client, $config, $eventManager); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Gets the EventManager used by the DocumentManager. |
208
|
|
|
*/ |
209
|
1081 |
|
public function getEventManager() : EventManager |
210
|
|
|
{ |
211
|
1081 |
|
return $this->eventManager; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Gets the MongoDB client instance that this DocumentManager wraps. |
216
|
|
|
*/ |
217
|
970 |
|
public function getClient() : Client |
218
|
|
|
{ |
219
|
970 |
|
return $this->client; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Gets the metadata factory used to gather the metadata of classes. |
224
|
|
|
* |
225
|
|
|
* @return ClassMetadataFactory |
226
|
|
|
*/ |
227
|
117 |
|
public function getMetadataFactory() |
228
|
|
|
{ |
229
|
117 |
|
return $this->metadataFactory; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Helper method to initialize a lazy loading proxy or persistent collection. |
234
|
|
|
* |
235
|
|
|
* This method is a no-op for other objects. |
236
|
|
|
* |
237
|
|
|
* @param object $obj |
238
|
|
|
*/ |
239
|
|
|
public function initializeObject($obj) |
240
|
|
|
{ |
241
|
|
|
$this->unitOfWork->initializeObject($obj); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
246
|
|
|
*/ |
247
|
1081 |
|
public function getUnitOfWork() : UnitOfWork |
248
|
|
|
{ |
249
|
1081 |
|
return $this->unitOfWork; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
254
|
|
|
* for each type of document. |
255
|
|
|
*/ |
256
|
3 |
|
public function getHydratorFactory() : HydratorFactory |
257
|
|
|
{ |
258
|
3 |
|
return $this->hydratorFactory; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns SchemaManager, used to create/drop indexes/collections/databases. |
263
|
|
|
*/ |
264
|
122 |
|
public function getSchemaManager() : SchemaManager |
265
|
|
|
{ |
266
|
122 |
|
return $this->schemaManager; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** Returns the class name resolver which is used to resolve real class names for proxy objects. */ |
270
|
903 |
|
public function getClassNameResolver() : ClassNameResolver |
271
|
|
|
{ |
272
|
903 |
|
return $this->classNameResolver; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Returns the metadata for a class. |
277
|
|
|
* |
278
|
|
|
* @internal Performance-sensitive method. |
279
|
|
|
* |
280
|
|
|
* @param string $className The class name. |
281
|
|
|
*/ |
282
|
846 |
|
public function getClassMetadata($className) : ClassMetadata |
283
|
|
|
{ |
284
|
846 |
|
return $this->metadataFactory->getMetadataFor($className); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Returns the MongoDB instance for a class. |
289
|
|
|
*/ |
290
|
766 |
|
public function getDocumentDatabase(string $className) : Database |
291
|
|
|
{ |
292
|
766 |
|
$className = $this->classNameResolver->getRealClass($className); |
293
|
|
|
|
294
|
766 |
|
if (isset($this->documentDatabases[$className])) { |
295
|
8 |
|
return $this->documentDatabases[$className]; |
296
|
|
|
} |
297
|
|
|
|
298
|
766 |
|
$metadata = $this->metadataFactory->getMetadataFor($className); |
299
|
766 |
|
assert($metadata instanceof ClassMetadata); |
300
|
|
|
|
301
|
766 |
|
$db = $metadata->getDatabase(); |
302
|
766 |
|
$db = $db ?: $this->config->getDefaultDB(); |
303
|
766 |
|
$db = $db ?: 'doctrine'; |
304
|
766 |
|
$this->documentDatabases[$className] = $this->client->selectDatabase($db); |
305
|
|
|
|
306
|
766 |
|
return $this->documentDatabases[$className]; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Gets the array of instantiated document database instances. |
311
|
|
|
* |
312
|
|
|
* @return Database[] |
313
|
|
|
*/ |
314
|
|
|
public function getDocumentDatabases() : array |
315
|
|
|
{ |
316
|
|
|
return $this->documentDatabases; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Returns the collection instance for a class. |
321
|
|
|
* |
322
|
|
|
* @throws MongoDBException When the $className param is not mapped to a collection. |
323
|
|
|
*/ |
324
|
740 |
|
public function getDocumentCollection(string $className) : Collection |
325
|
|
|
{ |
326
|
740 |
|
$className = $this->classNameResolver->getRealClass($className); |
327
|
|
|
|
328
|
|
|
/** @var ClassMetadata $metadata */ |
329
|
740 |
|
$metadata = $this->metadataFactory->getMetadataFor($className); |
330
|
740 |
|
assert($metadata instanceof ClassMetadata); |
331
|
740 |
|
if ($metadata->isFile) { |
332
|
22 |
|
return $this->getDocumentBucket($className)->getFilesCollection(); |
333
|
|
|
} |
334
|
|
|
|
335
|
731 |
|
$collectionName = $metadata->getCollection(); |
336
|
|
|
|
337
|
731 |
|
if (! $collectionName) { |
338
|
|
|
throw MongoDBException::documentNotMappedToCollection($className); |
339
|
|
|
} |
340
|
|
|
|
341
|
731 |
|
if (! isset($this->documentCollections[$className])) { |
342
|
731 |
|
$db = $this->getDocumentDatabase($className); |
343
|
|
|
|
344
|
731 |
|
$options = []; |
345
|
731 |
|
if ($metadata->readPreference !== null) { |
346
|
|
|
$options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); |
347
|
|
|
} |
348
|
|
|
|
349
|
731 |
|
$this->documentCollections[$className] = $db->selectCollection($collectionName, $options); |
350
|
|
|
} |
351
|
|
|
|
352
|
731 |
|
return $this->documentCollections[$className]; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Returns the bucket instance for a class. |
357
|
|
|
* |
358
|
|
|
* @throws MongoDBException When the $className param is not mapped to a collection. |
359
|
|
|
*/ |
360
|
24 |
|
public function getDocumentBucket(string $className) : Bucket |
361
|
|
|
{ |
362
|
24 |
|
$className = $this->classNameResolver->getRealClass($className); |
363
|
|
|
|
364
|
|
|
/** @var ClassMetadata $metadata */ |
365
|
24 |
|
$metadata = $this->metadataFactory->getMetadataFor($className); |
366
|
24 |
|
if (! $metadata->isFile) { |
367
|
|
|
throw MongoDBException::documentBucketOnlyAvailableForGridFSFiles($className); |
368
|
|
|
} |
369
|
|
|
|
370
|
24 |
|
$bucketName = $metadata->getBucketName(); |
371
|
|
|
|
372
|
24 |
|
if (! $bucketName) { |
373
|
|
|
throw MongoDBException::documentNotMappedToCollection($className); |
374
|
|
|
} |
375
|
|
|
|
376
|
24 |
|
if (! isset($this->documentBuckets[$className])) { |
377
|
24 |
|
$db = $this->getDocumentDatabase($className); |
378
|
|
|
|
379
|
24 |
|
$options = ['bucketName' => $bucketName]; |
380
|
24 |
|
if ($metadata->readPreference !== null) { |
381
|
|
|
$options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); |
382
|
|
|
} |
383
|
|
|
|
384
|
24 |
|
$this->documentBuckets[$className] = $db->selectGridFSBucket($options); |
385
|
|
|
} |
386
|
|
|
|
387
|
24 |
|
return $this->documentBuckets[$className]; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Gets the array of instantiated document collection instances. |
392
|
|
|
* |
393
|
|
|
* @return Collection[] |
394
|
|
|
*/ |
395
|
|
|
public function getDocumentCollections() : array |
396
|
|
|
{ |
397
|
|
|
return $this->documentCollections; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Create a new Query instance for a class. |
402
|
|
|
* |
403
|
|
|
* @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
404
|
|
|
*/ |
405
|
54 |
|
public function createQueryBuilder($documentName = null) : Query\Builder |
406
|
|
|
{ |
407
|
54 |
|
return new Query\Builder($this, $documentName); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Creates a new aggregation builder instance for a class. |
412
|
|
|
*/ |
413
|
19 |
|
public function createAggregationBuilder(string $documentName) : Aggregation\Builder |
414
|
|
|
{ |
415
|
19 |
|
return new Aggregation\Builder($this, $documentName); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Tells the DocumentManager to make an instance managed and persistent. |
420
|
|
|
* |
421
|
|
|
* The document will be entered into the database at or before transaction |
422
|
|
|
* commit or as a result of the flush operation. |
423
|
|
|
* |
424
|
|
|
* NOTE: The persist operation always considers documents that are not yet known to |
425
|
|
|
* this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
426
|
|
|
* |
427
|
|
|
* @param object $document The instance to make managed and persistent. |
428
|
|
|
* |
429
|
|
|
* @throws InvalidArgumentException When the given $document param is not an object. |
430
|
|
|
*/ |
431
|
73 |
|
public function persist($document) |
432
|
|
|
{ |
433
|
73 |
|
if (! is_object($document)) { |
434
|
1 |
|
throw new InvalidArgumentException(gettype($document)); |
435
|
|
|
} |
436
|
72 |
|
$this->errorIfClosed(); |
437
|
71 |
|
$this->unitOfWork->persist($document); |
438
|
67 |
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Removes a document instance. |
442
|
|
|
* |
443
|
|
|
* A removed document will be removed from the database at or before transaction commit |
444
|
|
|
* or as a result of the flush operation. |
445
|
|
|
* |
446
|
|
|
* @param object $document The document instance to remove. |
447
|
|
|
* |
448
|
|
|
* @throws InvalidArgumentException When the $document param is not an object. |
449
|
|
|
*/ |
450
|
2 |
|
public function remove($document) |
451
|
|
|
{ |
452
|
2 |
|
if (! is_object($document)) { |
453
|
1 |
|
throw new InvalidArgumentException(gettype($document)); |
454
|
|
|
} |
455
|
1 |
|
$this->errorIfClosed(); |
456
|
|
|
$this->unitOfWork->remove($document); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Refreshes the persistent state of a document from the database, |
461
|
|
|
* overriding any local changes that have not yet been persisted. |
462
|
|
|
* |
463
|
|
|
* @param object $document The document to refresh. |
464
|
|
|
* |
465
|
|
|
* @throws InvalidArgumentException When the given $document param is not an object. |
466
|
|
|
*/ |
467
|
2 |
|
public function refresh($document) |
468
|
|
|
{ |
469
|
2 |
|
if (! is_object($document)) { |
470
|
1 |
|
throw new InvalidArgumentException(gettype($document)); |
471
|
|
|
} |
472
|
1 |
|
$this->errorIfClosed(); |
473
|
|
|
$this->unitOfWork->refresh($document); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Detaches a document from the DocumentManager, causing a managed document to |
478
|
|
|
* become detached. Unflushed changes made to the document if any |
479
|
|
|
* (including removal of the document), will not be synchronized to the database. |
480
|
|
|
* Documents which previously referenced the detached document will continue to |
481
|
|
|
* reference it. |
482
|
|
|
* |
483
|
|
|
* @param object $document The document to detach. |
484
|
|
|
* |
485
|
|
|
* @throws InvalidArgumentException When the $document param is not an object. |
486
|
|
|
*/ |
487
|
3 |
|
public function detach($document) |
488
|
|
|
{ |
489
|
3 |
|
if (! is_object($document)) { |
490
|
1 |
|
throw new InvalidArgumentException(gettype($document)); |
491
|
|
|
} |
492
|
2 |
|
$this->unitOfWork->detach($document); |
493
|
2 |
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Merges the state of a detached document into the persistence context |
497
|
|
|
* of this DocumentManager and returns the managed copy of the document. |
498
|
|
|
* The document passed to merge will not become associated/managed with this DocumentManager. |
499
|
|
|
* |
500
|
|
|
* @param object $document The detached document to merge into the persistence context. |
501
|
|
|
* |
502
|
|
|
* @return object The managed copy of the document. |
503
|
|
|
* |
504
|
|
|
* @throws LockException |
505
|
|
|
* @throws InvalidArgumentException If the $document param is not an object. |
506
|
|
|
*/ |
507
|
3 |
|
public function merge($document) |
508
|
|
|
{ |
509
|
3 |
|
if (! is_object($document)) { |
510
|
1 |
|
throw new InvalidArgumentException(gettype($document)); |
511
|
|
|
} |
512
|
2 |
|
$this->errorIfClosed(); |
513
|
|
|
|
514
|
1 |
|
return $this->unitOfWork->merge($document); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Acquire a lock on the given document. |
519
|
|
|
* |
520
|
|
|
* @throws InvalidArgumentException |
521
|
|
|
* @throws LockException |
522
|
|
|
*/ |
523
|
1 |
|
public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
524
|
|
|
{ |
525
|
1 |
|
$this->unitOfWork->lock($document, $lockMode, $lockVersion); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Releases a lock on the given document. |
530
|
|
|
*/ |
531
|
|
|
public function unlock(object $document) : void |
532
|
|
|
{ |
533
|
|
|
$this->unitOfWork->unlock($document); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* Gets the repository for a document class. |
538
|
|
|
* |
539
|
|
|
* @param string $documentName The name of the Document. |
540
|
|
|
* |
541
|
|
|
* @return ObjectRepository The repository. |
542
|
|
|
*/ |
543
|
12 |
|
public function getRepository($documentName) |
544
|
|
|
{ |
545
|
12 |
|
return $this->repositoryFactory->getRepository($this, $documentName); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* Flushes all changes to objects that have been queued up to now to the database. |
550
|
|
|
* This effectively synchronizes the in-memory state of managed objects with the |
551
|
|
|
* database. |
552
|
|
|
* |
553
|
|
|
* @param array $options Array of options to be used with batchInsert(), update() and remove() |
554
|
|
|
* |
555
|
|
|
* @throws MongoDBException |
556
|
|
|
*/ |
557
|
32 |
|
public function flush(array $options = []) |
558
|
|
|
{ |
559
|
32 |
|
$this->errorIfClosed(); |
560
|
31 |
|
$this->unitOfWork->commit($options); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Gets a reference to the document identified by the given type and identifier |
565
|
|
|
* without actually loading it. |
566
|
|
|
* |
567
|
|
|
* If partial objects are allowed, this method will return a partial object that only |
568
|
|
|
* has its identifier populated. Otherwise a proxy is returned that automatically |
569
|
|
|
* loads itself on first access. |
570
|
|
|
* |
571
|
|
|
* @param mixed $identifier |
572
|
|
|
*/ |
573
|
2 |
|
public function getReference(string $documentName, $identifier) : object |
574
|
|
|
{ |
575
|
|
|
/** @var ClassMetadata $class */ |
576
|
2 |
|
$class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\')); |
577
|
2 |
|
$document = $this->unitOfWork->tryGetById($identifier, $class); |
578
|
|
|
|
579
|
|
|
// Check identity map first, if its already in there just return it. |
580
|
2 |
|
if ($document) { |
581
|
|
|
return $document; |
582
|
|
|
} |
583
|
|
|
|
584
|
2 |
|
$document = $this->proxyFactory->getProxy($class, $identifier); |
585
|
2 |
|
$this->unitOfWork->registerManaged($document, $identifier, []); |
586
|
|
|
|
587
|
2 |
|
return $document; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Gets a partial reference to the document identified by the given type and identifier |
592
|
|
|
* without actually loading it, if the document is not yet loaded. |
593
|
|
|
* |
594
|
|
|
* The returned reference may be a partial object if the document is not yet loaded/managed. |
595
|
|
|
* If it is a partial object it will not initialize the rest of the document state on access. |
596
|
|
|
* Thus you can only ever safely access the identifier of a document obtained through |
597
|
|
|
* this method. |
598
|
|
|
* |
599
|
|
|
* The use-cases for partial references involve maintaining bidirectional associations |
600
|
|
|
* without loading one side of the association or to update a document without loading it. |
601
|
|
|
* Note, however, that in the latter case the original (persistent) document data will |
602
|
|
|
* never be visible to the application (especially not event listeners) as it will |
603
|
|
|
* never be loaded in the first place. |
604
|
|
|
* |
605
|
|
|
* @param mixed $identifier The document identifier. |
606
|
|
|
*/ |
607
|
1 |
|
public function getPartialReference(string $documentName, $identifier) : object |
608
|
|
|
{ |
609
|
1 |
|
$class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\')); |
610
|
1 |
|
assert($class instanceof ClassMetadata); |
611
|
1 |
|
$document = $this->unitOfWork->tryGetById($identifier, $class); |
612
|
|
|
|
613
|
|
|
// Check identity map first, if its already in there just return it. |
614
|
1 |
|
if ($document) { |
615
|
|
|
return $document; |
616
|
|
|
} |
617
|
1 |
|
$document = $class->newInstance(); |
618
|
1 |
|
$class->setIdentifierValue($document, $identifier); |
619
|
1 |
|
$this->unitOfWork->registerManaged($document, $identifier, []); |
620
|
|
|
|
621
|
1 |
|
return $document; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* Finds a Document by its identifier. |
626
|
|
|
* |
627
|
|
|
* This is just a convenient shortcut for getRepository($documentName)->find($id). |
628
|
|
|
* |
629
|
|
|
* @param string $documentName |
630
|
|
|
* @param mixed $identifier |
631
|
|
|
* @param int $lockMode |
632
|
|
|
* @param int $lockVersion |
633
|
|
|
*/ |
634
|
1 |
|
public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) : ?object |
635
|
|
|
{ |
636
|
1 |
|
$repository = $this->getRepository($documentName); |
637
|
1 |
|
if ($repository instanceof DocumentRepository) { |
638
|
1 |
|
return $repository->find($identifier, $lockMode, $lockVersion); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
return $repository->find($identifier); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Clears the DocumentManager. |
646
|
|
|
* |
647
|
|
|
* All documents that are currently managed by this DocumentManager become |
648
|
|
|
* detached. |
649
|
|
|
* |
650
|
|
|
* @param string|null $documentName if given, only documents of this type will get detached |
651
|
|
|
*/ |
652
|
8 |
|
public function clear($documentName = null) |
653
|
|
|
{ |
654
|
8 |
|
$this->unitOfWork->clear($documentName); |
655
|
8 |
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* Closes the DocumentManager. All documents that are currently managed |
659
|
|
|
* by this DocumentManager become detached. The DocumentManager may no longer |
660
|
|
|
* be used after it is closed. |
661
|
|
|
*/ |
662
|
6 |
|
public function close() |
663
|
|
|
{ |
664
|
6 |
|
$this->clear(); |
665
|
6 |
|
$this->closed = true; |
666
|
6 |
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* Determines whether a document instance is managed in this DocumentManager. |
670
|
|
|
* |
671
|
|
|
* @param object $document |
672
|
|
|
* |
673
|
|
|
* @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
674
|
|
|
* |
675
|
|
|
* @throws InvalidArgumentException When the $document param is not an object. |
676
|
|
|
*/ |
677
|
1 |
|
public function contains($document) |
678
|
|
|
{ |
679
|
1 |
|
if (! is_object($document)) { |
680
|
|
|
throw new InvalidArgumentException(gettype($document)); |
681
|
|
|
} |
682
|
|
|
|
683
|
1 |
|
return $this->unitOfWork->isScheduledForInsert($document) || |
684
|
1 |
|
$this->unitOfWork->isInIdentityMap($document) && |
685
|
1 |
|
! $this->unitOfWork->isScheduledForDelete($document); |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* Gets the Configuration used by the DocumentManager. |
690
|
|
|
*/ |
691
|
1081 |
|
public function getConfiguration() : Configuration |
692
|
|
|
{ |
693
|
1081 |
|
return $this->config; |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* Returns a reference to the supplied document. |
698
|
|
|
* |
699
|
|
|
* @return mixed The reference for the document in question, according to the desired mapping |
700
|
|
|
* |
701
|
|
|
* @throws MappingException |
702
|
|
|
* @throws RuntimeException |
703
|
|
|
*/ |
704
|
31 |
|
public function createReference(object $document, array $referenceMapping) |
705
|
|
|
{ |
706
|
31 |
|
$class = $this->getClassMetadata(get_class($document)); |
707
|
31 |
|
$id = $this->unitOfWork->getDocumentIdentifier($document); |
708
|
|
|
|
709
|
31 |
|
if ($id === null) { |
710
|
1 |
|
throw new RuntimeException( |
711
|
1 |
|
sprintf('Cannot create a DBRef for class %s without an identifier. Have you forgotten to persist/merge the document first?', $class->name) |
712
|
|
|
); |
713
|
|
|
} |
714
|
|
|
|
715
|
30 |
|
$storeAs = $referenceMapping['storeAs'] ?? null; |
716
|
30 |
|
switch ($storeAs) { |
717
|
|
|
case ClassMetadata::REFERENCE_STORE_AS_ID: |
718
|
6 |
|
if ($class->inheritanceType === ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION) { |
719
|
1 |
|
throw MappingException::simpleReferenceMustNotTargetDiscriminatedDocument($referenceMapping['targetDocument']); |
720
|
|
|
} |
721
|
|
|
|
722
|
5 |
|
return $class->getDatabaseIdentifierValue($id); |
723
|
|
|
case ClassMetadata::REFERENCE_STORE_AS_REF: |
724
|
1 |
|
$reference = ['id' => $class->getDatabaseIdentifierValue($id)]; |
725
|
1 |
|
break; |
726
|
|
|
|
727
|
|
|
case ClassMetadata::REFERENCE_STORE_AS_DB_REF: |
728
|
|
|
$reference = [ |
729
|
26 |
|
'$ref' => $class->getCollection(), |
730
|
26 |
|
'$id' => $class->getDatabaseIdentifierValue($id), |
731
|
|
|
]; |
732
|
26 |
|
break; |
733
|
|
|
|
734
|
|
|
case ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB: |
735
|
|
|
$reference = [ |
736
|
2 |
|
'$ref' => $class->getCollection(), |
737
|
2 |
|
'$id' => $class->getDatabaseIdentifierValue($id), |
738
|
2 |
|
'$db' => $this->getDocumentDatabase($class->name)->getDatabaseName(), |
739
|
|
|
]; |
740
|
2 |
|
break; |
741
|
|
|
|
742
|
|
|
default: |
743
|
|
|
throw new InvalidArgumentException(sprintf('Reference type %s is invalid.', $storeAs)); |
744
|
|
|
} |
745
|
|
|
|
746
|
27 |
|
return $reference + $this->getDiscriminatorData($referenceMapping, $class); |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Build discriminator portion of reference for specified reference mapping and class metadata. |
751
|
|
|
* |
752
|
|
|
* @param array $referenceMapping Mappings of reference for which discriminator data is created. |
753
|
|
|
* @param ClassMetadata $class Metadata of reference document class. |
754
|
|
|
* |
755
|
|
|
* @return array with next structure [{discriminator field} => {discriminator value}] |
756
|
|
|
* |
757
|
|
|
* @throws MappingException When discriminator map is present and reference class in not registered in it. |
758
|
|
|
*/ |
759
|
27 |
|
private function getDiscriminatorData(array $referenceMapping, ClassMetadata $class) : array |
760
|
|
|
{ |
761
|
27 |
|
$discriminatorField = null; |
|
|
|
|
762
|
27 |
|
$discriminatorValue = null; |
763
|
27 |
|
$discriminatorMap = null; |
764
|
|
|
|
765
|
27 |
|
if (isset($referenceMapping['discriminatorField'])) { |
766
|
15 |
|
$discriminatorField = $referenceMapping['discriminatorField']; |
767
|
|
|
|
768
|
15 |
|
if (isset($referenceMapping['discriminatorMap'])) { |
769
|
15 |
|
$discriminatorMap = $referenceMapping['discriminatorMap']; |
770
|
|
|
} |
771
|
|
|
} else { |
772
|
14 |
|
$discriminatorField = $class->discriminatorField; |
773
|
14 |
|
$discriminatorValue = $class->discriminatorValue; |
774
|
14 |
|
$discriminatorMap = $class->discriminatorMap; |
775
|
|
|
} |
776
|
|
|
|
777
|
27 |
|
if ($discriminatorField === null) { |
778
|
9 |
|
return []; |
779
|
|
|
} |
780
|
|
|
|
781
|
20 |
|
if ($discriminatorValue === null) { |
782
|
19 |
|
if (! empty($discriminatorMap)) { |
783
|
7 |
|
$pos = array_search($class->name, $discriminatorMap); |
784
|
|
|
|
785
|
7 |
|
if ($pos !== false) { |
786
|
7 |
|
$discriminatorValue = $pos; |
787
|
|
|
} |
788
|
|
|
} else { |
789
|
12 |
|
$discriminatorValue = $class->name; |
790
|
|
|
} |
791
|
|
|
} |
792
|
|
|
|
793
|
20 |
|
if ($discriminatorValue === null) { |
794
|
6 |
|
throw MappingException::unlistedClassInDiscriminatorMap($class->name); |
795
|
|
|
} |
796
|
|
|
|
797
|
16 |
|
return [$discriminatorField => $discriminatorValue]; |
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* Throws an exception if the DocumentManager is closed or currently not active. |
802
|
|
|
* |
803
|
|
|
* @throws MongoDBException If the DocumentManager is closed. |
804
|
|
|
*/ |
805
|
77 |
|
private function errorIfClosed() : void |
806
|
|
|
{ |
807
|
77 |
|
if ($this->closed) { |
808
|
5 |
|
throw MongoDBException::documentManagerClosed(); |
809
|
|
|
} |
810
|
72 |
|
} |
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* Check if the Document manager is open or closed. |
814
|
|
|
*/ |
815
|
1 |
|
public function isOpen() : bool |
816
|
|
|
{ |
817
|
1 |
|
return ! $this->closed; |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
/** |
821
|
|
|
* Gets the filter collection. |
822
|
|
|
*/ |
823
|
76 |
|
public function getFilterCollection() : FilterCollection |
824
|
|
|
{ |
825
|
76 |
|
if ($this->filterCollection === null) { |
826
|
76 |
|
$this->filterCollection = new FilterCollection($this); |
827
|
|
|
} |
828
|
|
|
|
829
|
76 |
|
return $this->filterCollection; |
830
|
|
|
} |
831
|
|
|
|
832
|
1081 |
|
private function checkTypeMap() : void |
833
|
|
|
{ |
834
|
1081 |
|
$typeMap = $this->client->getTypeMap(); |
835
|
|
|
|
836
|
1081 |
|
foreach (self::CLIENT_TYPEMAP as $part => $expectedType) { |
837
|
1081 |
|
if (! isset($typeMap[$part]) || $typeMap[$part] !== $expectedType) { |
838
|
3 |
|
throw MongoDBException::invalidTypeMap($part, $expectedType); |
839
|
|
|
} |
840
|
|
|
} |
841
|
1081 |
|
} |
842
|
|
|
} |
843
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.