forked from doctrine/mongodb-odm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentManager.php
758 lines (683 loc) · 24.8 KB
/
DocumentManager.php
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ODM\MongoDB;
use Doctrine\Common\EventManager;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\MongoDB\Connection;
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
use Doctrine\ODM\MongoDB\Proxy\ProxyFactory;
use Doctrine\ODM\MongoDB\Query\FilterCollection;
/**
* The DocumentManager class is the central access point for managing the
* persistence of documents.
*
* <?php
*
* $config = new Configuration();
* $dm = DocumentManager::create(new Connection(), $config);
*
* @since 1.0
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class DocumentManager implements ObjectManager
{
/**
* The Doctrine MongoDB connection instance.
*
* @var \Doctrine\MongoDB\Connection
*/
private $connection;
/**
* The used Configuration.
*
* @var \Doctrine\ODM\MongoDB\Configuration
*/
private $config;
/**
* The metadata factory, used to retrieve the ODM metadata of document classes.
*
* @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory
*/
private $metadataFactory;
/**
* The DocumentRepository instances.
*
* @var array
*/
private $repositories = array();
/**
* The UnitOfWork used to coordinate object-level transactions.
*
* @var UnitOfWork
*/
private $unitOfWork;
/**
* The event manager that is the central point of the event system.
*
* @var \Doctrine\Common\EventManager
*/
private $eventManager;
/**
* The Hydrator factory instance.
*
* @var HydratorFactory
*/
private $hydratorFactory;
/**
* SchemaManager instance
*
* @var SchemaManager
*/
private $schemaManager;
/**
* Array of cached document database instances that are lazily loaded.
*
* @var array
*/
private $documentDatabases = array();
/**
* Array of cached document collection instances that are lazily loaded.
*
* @var array
*/
private $documentCollections = array();
/**
* Whether the DocumentManager is closed or not.
*
* @var bool
*/
private $closed = false;
/**
* Collection of query filters.
*
* @var \Doctrine\ODM\MongoDB\Query\FilterCollection
*/
private $filterCollection;
/**
* Creates a new Document that operates on the given Mongo connection
* and uses the given Configuration.
*
* @param \Doctrine\MongoDB\Connection|null $conn
* @param Configuration|null $config
* @param \Doctrine\Common\EventManager|null $eventManager
*/
protected function __construct(Connection $conn = null, Configuration $config = null, EventManager $eventManager = null) {
$this->config = $config ?: new Configuration();
$this->eventManager = $eventManager ?: new EventManager();
$this->connection = $conn ?: new Connection(null, array(), $this->config, $this->eventManager);
$metadataFactoryClassName = $this->config->getClassMetadataFactoryName();
$this->metadataFactory = new $metadataFactoryClassName();
$this->metadataFactory->setDocumentManager($this);
$this->metadataFactory->setConfiguration($this->config);
if ($cacheDriver = $this->config->getMetadataCacheImpl()) {
$this->metadataFactory->setCacheDriver($cacheDriver);
}
$hydratorDir = $this->config->getHydratorDir();
$hydratorNs = $this->config->getHydratorNamespace();
$this->hydratorFactory = new HydratorFactory(
$this,
$this->eventManager,
$hydratorDir,
$hydratorNs,
$this->config->getAutoGenerateHydratorClasses()
);
$this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory);
$this->hydratorFactory->setUnitOfWork($this->unitOfWork);
$this->schemaManager = new SchemaManager($this, $this->metadataFactory);
$this->proxyFactory = new ProxyFactory($this,
$this->config->getProxyDir(),
$this->config->getProxyNamespace(),
$this->config->getAutoGenerateProxyClasses()
);
}
/**
* Gets the proxy factory used by the DocumentManager to create document proxies.
*
* @return ProxyFactory
*/
public function getProxyFactory()
{
return $this->proxyFactory;
}
/**
* Creates a new Document that operates on the given Mongo connection
* and uses the given Configuration.
*
* @static
* @param \Doctrine\MongoDB\Connection|null $conn
* @param Configuration|null $config
* @param \Doctrine\Common\EventManager|null $eventManager
* @return DocumentManager
*/
public static function create(Connection $conn = null, Configuration $config = null, EventManager $eventManager = null)
{
return new DocumentManager($conn, $config, $eventManager);
}
/**
* Gets the EventManager used by the DocumentManager.
*
* @return \Doctrine\Common\EventManager
*/
public function getEventManager()
{
return $this->eventManager;
}
/**
* Gets the PHP Mongo instance that this DocumentManager wraps.
*
* @return \Doctrine\MongoDB\Connection
*/
public function getConnection()
{
return $this->connection;
}
/**
* Gets the metadata factory used to gather the metadata of classes.
*
* @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory
*/
public function getMetadataFactory()
{
return $this->metadataFactory;
}
/**
* Helper method to initialize a lazy loading proxy or persistent collection.
*
* This method is a no-op for other objects.
*
* @param object $obj
*/
public function initializeObject($obj)
{
$this->unitOfWork->initializeObject($obj);
}
/**
* Gets the UnitOfWork used by the DocumentManager to coordinate operations.
*
* @return UnitOfWork
*/
public function getUnitOfWork()
{
return $this->unitOfWork;
}
/**
* Gets the Hydrator factory used by the DocumentManager to generate and get hydrators
* for each type of document.
*
* @return \Doctrine\ODM\MongoDB\Hydrator\HydratorInterface
*/
public function getHydratorFactory()
{
return $this->hydratorFactory;
}
/**
* Returns SchemaManager, used to create/drop indexes/collections/databases.
*
* @return \Doctrine\ODM\MongoDB\SchemaManager
*/
public function getSchemaManager()
{
return $this->schemaManager;
}
/**
* Returns the metadata for a class.
*
* @param string $className The class name.
* @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata
* @internal Performance-sensitive method.
*/
public function getClassMetadata($className)
{
return $this->metadataFactory->getMetadataFor(ltrim($className, '\\'));
}
/**
* Returns the MongoDB instance for a class.
*
* @param string $className The class name.
* @return \Doctrine\MongoDB\Database
*/
public function getDocumentDatabase($className)
{
$className = ltrim($className, '\\');
if (isset($this->documentDatabases[$className])) {
return $this->documentDatabases[$className];
}
$metadata = $this->metadataFactory->getMetadataFor($className);
$db = $metadata->getDatabase();
$db = $db ? $db : $this->config->getDefaultDB();
$db = $db ? $db : 'doctrine';
$this->documentDatabases[$className] = $this->connection->selectDatabase($db);
return $this->documentDatabases[$className];
}
/**
* Gets the array of instantiated document database instances.
*
* @return array
*/
public function getDocumentDatabases()
{
return $this->documentDatabases;
}
/**
* Returns the MongoCollection instance for a class.
*
* @param string $className The class name.
* @throws MongoDBException When the $className param is not mapped to a collection
* @return \Doctrine\MongoDB\Collection
*/
public function getDocumentCollection($className)
{
$className = ltrim($className, '\\');
$metadata = $this->metadataFactory->getMetadataFor($className);
$collection = $metadata->getCollection();
if ( ! $collection) {
throw MongoDBException::documentNotMappedToCollection($className);
}
$db = $this->getDocumentDatabase($className);
if ( ! isset($this->documentCollections[$className])) {
if ($metadata->isFile()) {
$this->documentCollections[$className] = $db->getGridFS($collection);
} else {
$this->documentCollections[$className] = $db->selectCollection($collection);
}
}
$collection = $this->documentCollections[$className];
if (isset($metadata->slaveOkay)) {
$collection->setSlaveOkay($metadata->slaveOkay);
}
return $this->documentCollections[$className];
}
/**
* Gets the array of instantiated document collection instances.
*
* @return array
*/
public function getDocumentCollections()
{
return $this->documentCollections;
}
/**
* Create a new Query instance for a class.
*
* @param string $documentName The document class name.
* @return Query\Builder
*/
public function createQueryBuilder($documentName = null)
{
return new Query\Builder($this, $documentName);
}
/**
* Tells the DocumentManager to make an instance managed and persistent.
*
* The document will be entered into the database at or before transaction
* commit or as a result of the flush operation.
*
* NOTE: The persist operation always considers documents that are not yet known to
* this DocumentManager as NEW. Do not pass detached documents to the persist operation.
*
* @param object $document The instance to make managed and persistent.
* @throws \InvalidArgumentException When the given $document param is not an object
*/
public function persist($document)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->errorIfClosed();
$this->unitOfWork->persist($document);
}
/**
* Removes a document instance.
*
* A removed document will be removed from the database at or before transaction commit
* or as a result of the flush operation.
*
* @param object $document The document instance to remove.
* @throws \InvalidArgumentException when the $document param is not an object
*/
public function remove($document)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->errorIfClosed();
$this->unitOfWork->remove($document);
}
/**
* Refreshes the persistent state of a document from the database,
* overriding any local changes that have not yet been persisted.
*
* @param object $document The document to refresh.
* @throws \InvalidArgumentException When the given $document param is not an object
*/
public function refresh($document)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->errorIfClosed();
$this->unitOfWork->refresh($document);
}
/**
* Detaches a document from the DocumentManager, causing a managed document to
* become detached. Unflushed changes made to the document if any
* (including removal of the document), will not be synchronized to the database.
* Documents which previously referenced the detached document will continue to
* reference it.
*
* @param object $document The document to detach.
* @throws \InvalidArgumentException when the $document param is not an object
*/
public function detach($document)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->unitOfWork->detach($document);
}
/**
* Merges the state of a detached document into the persistence context
* of this DocumentManager and returns the managed copy of the document.
* The document passed to merge will not become associated/managed with this DocumentManager.
*
* @param object $document The detached document to merge into the persistence context.
* @throws LockException
* @throws \InvalidArgumentException if the $document param is not an object
* @return object The managed copy of the document.
*/
public function merge($document)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->errorIfClosed();
return $this->unitOfWork->merge($document);
}
/**
* Acquire a lock on the given document.
*
* @param object $document
* @param int $lockMode
* @param int $lockVersion
* @throws \InvalidArgumentException
*/
public function lock($document, $lockMode, $lockVersion = null)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->unitOfWork->lock($document, $lockMode, $lockVersion);
}
/**
* Releases a lock on the given document.
*
* @param object $document
* @throws \InvalidArgumentException if the $document param is not an object
*/
public function unlock($document)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->unitOfWork->unlock($document);
}
/**
* Gets the repository for a document class.
*
* @param string $documentName The name of the Document.
* @return DocumentRepository The repository.
*/
public function getRepository($documentName)
{
$documentName = ltrim($documentName, '\\');
if (isset($this->repositories[$documentName])) {
return $this->repositories[$documentName];
}
$metadata = $this->getClassMetadata($documentName);
$customRepositoryClassName = $metadata->customRepositoryClassName;
if ($customRepositoryClassName !== null) {
$repository = new $customRepositoryClassName($this, $this->unitOfWork, $metadata);
} else {
$repository = new DocumentRepository($this, $this->unitOfWork, $metadata);
}
$this->repositories[$documentName] = $repository;
return $repository;
}
/**
* Flushes all changes to objects that have been queued up to now to the database.
* This effectively synchronizes the in-memory state of managed objects with the
* database.
*
* @param object $document
* @param array $options Array of options to be used with batchInsert(), update() and remove()
* @throws \InvalidArgumentException
*/
public function flush($document = null, array $options = array())
{
if (null !== $document && ! is_object($document) && ! is_array($document)) {
throw new \InvalidArgumentException(gettype($document));
}
$this->errorIfClosed();
$this->unitOfWork->commit($document, $options);
}
/**
* Gets a reference to the document identified by the given type and identifier
* without actually loading it.
*
* If partial objects are allowed, this method will return a partial object that only
* has its identifier populated. Otherwise a proxy is returned that automatically
* loads itself on first access.
*
* @param string $documentName
* @param string|object $identifier
* @return mixed|object The document reference.
*/
public function getReference($documentName, $identifier)
{
$class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
// Check identity map first, if its already in there just return it.
if ($document = $this->unitOfWork->tryGetById($identifier, $class->rootDocumentName)) {
return $document;
}
$document = $this->proxyFactory->getProxy($class->name, $identifier);
$this->unitOfWork->registerManaged($document, $identifier, array());
return $document;
}
/**
* Gets a partial reference to the document identified by the given type and identifier
* without actually loading it, if the document is not yet loaded.
*
* The returned reference may be a partial object if the document is not yet loaded/managed.
* If it is a partial object it will not initialize the rest of the document state on access.
* Thus you can only ever safely access the identifier of an document obtained through
* this method.
*
* The use-cases for partial references involve maintaining bidirectional associations
* without loading one side of the association or to update an document without loading it.
* Note, however, that in the latter case the original (persistent) document data will
* never be visible to the application (especially not event listeners) as it will
* never be loaded in the first place.
*
* @param string $documentName The name of the document type.
* @param mixed $identifier The document identifier.
* @return object The (partial) document reference.
*/
public function getPartialReference($documentName, $identifier)
{
$class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
// Check identity map first, if its already in there just return it.
if ($document = $this->unitOfWork->tryGetById($identifier, $class->rootDocumentName)) {
return $document;
}
$document = $class->newInstance();
$class->setIdentifierValue($document, $identifier);
$this->unitOfWork->registerManaged($document, $identifier, array());
return $document;
}
/**
* Finds a Document by its identifier.
*
* This is just a convenient shortcut for getRepository($documentName)->find($id).
*
* @param string $documentName
* @param mixed $identifier
* @param int $lockMode
* @param int $lockVersion
* @return object $document
*/
public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null)
{
return $this->getRepository($documentName)->find($identifier, $lockMode, $lockVersion);
}
/**
* Clears the DocumentManager.
*
* All documents that are currently managed by this DocumentManager become
* detached.
*
* @param string|null $documentName if given, only documents of this type will get detached
*/
public function clear($documentName = null)
{
$this->unitOfWork->clear($documentName);
}
/**
* Closes the DocumentManager. All documents that are currently managed
* by this DocumentManager become detached. The DocumentManager may no longer
* be used after it is closed.
*/
public function close()
{
$this->clear();
$this->closed = true;
}
/**
* Determines whether a document instance is managed in this DocumentManager.
*
* @param object $document
* @throws \InvalidArgumentException When the $document param is not an object
* @return boolean TRUE if this DocumentManager currently manages the given document, FALSE otherwise.
*/
public function contains($document)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException(gettype($document));
}
return $this->unitOfWork->isScheduledForInsert($document) ||
$this->unitOfWork->isInIdentityMap($document) &&
! $this->unitOfWork->isScheduledForDelete($document);
}
/**
* Gets the Configuration used by the DocumentManager.
*
* @return Configuration
*/
public function getConfiguration()
{
return $this->config;
}
public function getClassNameFromDiscriminatorValue(array $mapping, $value)
{
$discriminatorField = isset($mapping['discriminatorField']) ? $mapping['discriminatorField'] : '_doctrine_class_name';
if (is_array($value) && isset($value[$discriminatorField])) {
$discriminatorValue = $value[$discriminatorField];
return isset($mapping['discriminatorMap'][$discriminatorValue]) ? $mapping['discriminatorMap'][$discriminatorValue] : $discriminatorValue;
} else {
$class = $this->getClassMetadata($mapping['targetDocument']);
// @TODO figure out how to remove this
if ($class->discriminatorField) {
if (isset($value[$class->discriminatorField['name']])) {
return $class->discriminatorMap[$value[$class->discriminatorField['name']]];
}
}
}
return $mapping['targetDocument'];
}
/**
* Returns a DBRef array for the supplied document.
*
* @param mixed $document A document object
* @param array $referenceMapping Mapping for the field the references the document
*
* @throws \InvalidArgumentException
* @return array A DBRef array
*/
public function createDBRef($document, array $referenceMapping = null)
{
if ( ! is_object($document)) {
throw new \InvalidArgumentException('Cannot create a DBRef, the document is not an object');
}
$className = get_class($document);
$class = $this->getClassMetadata($className);
$id = $this->unitOfWork->getDocumentIdentifier($document);
if (isset($referenceMapping['simple']) && $referenceMapping['simple']) {
return $class->getDatabaseIdentifierValue($id);
}
$dbRef = array(
'$ref' => $class->getCollection(),
'$id' => $class->getDatabaseIdentifierValue($id),
'$db' => $this->getDocumentDatabase($className)->getName()
);
if ($class->discriminatorField) {
$dbRef[$class->discriminatorField['name']] = $class->discriminatorValue;
}
// add a discriminator value if the referenced document is not mapped explicitly to a targetDocument
if ($referenceMapping && ! isset($referenceMapping['targetDocument'])) {
$discriminatorField = isset($referenceMapping['discriminatorField']) ? $referenceMapping['discriminatorField'] : '_doctrine_class_name';
$discriminatorValue = isset($referenceMapping['discriminatorMap']) ? array_search($class->getName(), $referenceMapping['discriminatorMap']) : $class->getName();
$dbRef[$discriminatorField] = $discriminatorValue;
}
return $dbRef;
}
/**
* Throws an exception if the DocumentManager is closed or currently not active.
*
* @throws MongoDBException If the DocumentManager is closed.
*/
private function errorIfClosed()
{
if ($this->closed) {
throw MongoDBException::documentManagerClosed();
}
}
/**
* Check if the Document manager is open or closed.
*
* @return bool
*/
public function isOpen()
{
return ( ! $this->closed);
}
/**
* Gets the filter collection.
*
* @return \Doctrine\ODM\MongoDB\Query\FilterCollection The active filter collection.
*/
public function getFilterCollection()
{
if (null === $this->filterCollection) {
$this->filterCollection = new FilterCollection($this);
}
return $this->filterCollection;
}
}