forked from doctrine/mongodb-odm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocumentManager.php
588 lines (538 loc) · 19 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
<?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 LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ODM\MongoDB;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata,
Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory,
Doctrine\ODM\MongoDB\Mapping\Driver\PHPDriver,
Doctrine\ODM\MongoDB\Query,
Doctrine\ODM\MongoDB\Mongo,
Doctrine\ODM\MongoDB\PersistentCollection,
Doctrine\ODM\MongoDB\Proxy\ProxyFactory,
Doctrine\Common\Collections\ArrayCollection,
Doctrine\Common\EventManager;
/**
* The DocumentManager class is the central access point for managing the
* persistence of documents.
*
* <?php
*
* $config = new Configuration();
* $em = DocumentManager::create(new Mongo(), $config);
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 1.0
* @version $Revision$
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class DocumentManager
{
/**
* The Doctrine Mongo wrapper instance
*
* @var Doctrine\ODM\MongoDB\Mongo
*/
private $_mongo;
/**
* The used Configuration.
*
* @var Doctrine\ODM\MongoDB\Configuration
*/
private $_config;
/**
* The metadata factory, used to retrieve the ORM 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 Doctrine\ODM\MongoDB\UnitOfWork
*/
private $_unitOfWork;
/**
* The event manager that is the central point of the event system.
*
* @var Doctrine\Common\EventManager
*/
private $_eventManager;
/**
* The Document hydrator instance.
*
* @var Doctrine\ODM\MongoDB\Hydrator
*/
private $_hydrator;
/**
* Array of cached MongoDB instances that are lazily loaded.
*
* @var array
*/
private $_documentDBs = array();
/**
* Array of cached MongoCollection instances that are lazily loaded.
*
* @var array
*/
private $_documentCollections = array();
/**
* Whether the DocumentManager is closed or not.
*/
private $_closed = false;
/**
* Creates a new Document that operates on the given Mongo connection
* and uses the given Configuration.
*
* @param Doctrine\ODM\MongoDB\Mongo $mongo
* @param Doctrine\ODM\MongoDB\Configuration $config
* @param Doctrine\Common\EventManager $eventManager
*/
protected function __construct(Mongo $mongo = null, Configuration $config = null, EventManager $eventManager = null)
{
if (is_string($mongo) || $mongo instanceof \Mongo) {
$mongo = new Mongo($mongo);
}
$this->_mongo = $mongo ? $mongo : new Mongo();
$this->_config = $config ? $config : new Configuration();
$this->_eventManager = $eventManager ? $eventManager : new EventManager();
$this->_hydrator = new Hydrator($this);
$this->_metadataFactory = new ClassMetadataFactory($this);
if ($cacheDriver = $this->_config->getMetadataCacheImpl()) {
$this->_metadataFactory->setCacheDriver($cacheDriver);
}
$this->_unitOfWork = new UnitOfWork($this);
$this->_proxyFactory = new ProxyFactory($this,
$config->getProxyDir(),
$config->getProxyNamespace(),
$config->getAutoGenerateProxyClasses());
}
/**
* Creates a new Document that operates on the given Mongo connection
* and uses the given Configuration.
*
* @param Doctrine\ODM\MongoDB\Mongo $mongo
* @param Doctrine\ODM\MongoDB\Configuration $config
* @param Doctrine\Common\EventManager $eventManager
*/
public static function create(Mongo $mongo, Configuration $config = null, EventManager $eventManager = null)
{
return new self($mongo, $config, $eventManager);
}
/**
* Determines whether an document instance is managed in this DocumentManager.
*
* @param object $document
* @return boolean TRUE if this DocumentManager currently manages the given document, FALSE otherwise.
*/
public function contains($document)
{
return $this->_unitOfWork->isScheduledForInsert($document) ||
$this->_unitOfWork->isInIdentityMap($document) &&
! $this->_unitOfWork->isScheduledForDelete($document);
}
/**
* Gets the EventManager used by the DocumentManager.
*
* @return Doctrine\Common\EventManager
*/
public function getEventManager()
{
return $this->_eventManager;
}
public function getConfiguration()
{
return $this->_config;
}
public function getMongo()
{
return $this->_mongo;
}
/**
* Gets the metadata factory used to gather the metadata of classes.
*
* @return Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory
*/
public function getMetadataFactory()
{
return $this->_metadataFactory;
}
/**
* Gets the UnitOfWork used by the DocumentManager to coordinate operations.
*
* @return Doctrine\ODM\MongoDB\UnitOfWork
*/
public function getUnitOfWork()
{
return $this->_unitOfWork;
}
/**
* Gets the Hydrator used by the DocumentManager to hydrate document arrays
* to document objects.
*
* @return Doctrine\ODM\MongoDB\Hydrator
*/
public function getHydrator()
{
return $this->_hydrator;
}
/**
* 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($className);
}
/**
* Returns the MongoDB instance for a class.
*
* @param string $className The class name.
* @return Doctrine\ODM\MongoDB\MongoDB
*/
public function getDocumentDB($className)
{
$db = $this->_metadataFactory->getMetadataFor($className)->getDB();
$db = $db ? $db : $this->_config->getDefaultDB();
$db = $db ? $db : 'doctrine';
$db = sprintf('%s%s', $this->_config->getEnvironmentPrefix(), $db);
if ($db && ! isset($this->_documentDBs[$db])) {
$database = $this->_mongo->selectDB($db);
$this->_documentDBs[$db] = new MongoDB($database);
}
if ( ! isset($this->_documentDBs[$db])) {
throw MongoDBException::documentNotMappedToDB($className);
}
return $this->_documentDBs[$db];
}
/**
* Returns the MongoCollection instance for a class.
*
* @param string $className The class name.
* @return Doctrine\ODM\MongoDB\MongoCollection
*/
public function getDocumentCollection($className)
{
$metadata = $this->_metadataFactory->getMetadataFor($className);
$collection = $metadata->getCollection();
$db = $metadata->getDB();
$key = $db . '.' . $collection;
if ($collection && ! isset($this->_documentCollections[$key])) {
if ($metadata->isFile()) {
$collection = $this->getDocumentDB($className)->getGridFS($collection);
} else {
$collection = $this->getDocumentDB($className)->selectCollection($collection);
}
$mongoCollection = new MongoCollection($collection, $metadata, $this);
$this->_documentCollections[$key] = $mongoCollection;
}
if ( ! isset($this->_documentCollections[$key])) {
throw MongoDBException::documentNotMappedToCollection($className);
}
return $this->_documentCollections[$key];
}
/**
* Create a new Query instance for a class.
*
* @param string $className The class name.
* @return Document\ODM\MongoDB\Query
*/
public function createQuery($className = null)
{
return new Query($this, $className);
}
/**
* 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.
*/
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.
*/
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.
*/
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.
*/
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.
* @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);
}
/**
* Gets the repository for a document class.
*
* @param string $documentName The name of the Document.
* @return DocumentRepository The repository.
*/
public function getRepository($documentName)
{
if (isset($this->_repositories[$documentName])) {
return $this->_repositories[$documentName];
}
$metadata = $this->getClassMetadata($documentName);
$customRepositoryClassName = $metadata->customRepositoryClassName;
if ($customRepositoryClassName !== null) {
$repository = new $customRepositoryClassName($this, $metadata);
} else {
$repository = new DocumentRepository($this, $metadata);
}
$this->_repositories[$documentName] = $repository;
return $repository;
}
/**
* Loads a given document by its ID refreshing the values with the data from
* the database if the document already exists in the identity map.
*
* @param string $documentName The document name to load.
* @param string $id The id the document to load.
* @return object $document The loaded document.
*/
public function loadByID($documentName, $id)
{
$collection = $this->getDocumentCollection($documentName);
$result = $collection->findOne(array('_id' => new \MongoId($id)));
if ( ! $result) {
throw new \InvalidArgumentException(sprintf('Could not loadByID because ' . $documentName . ' '.$id . ' does not exist anymore.'));
}
return $this->load($documentName, $id, $result);
}
/**
* Loads data for a document id refreshing and overriding any local values
* if the document already exists in the identity map.
*
* @param string $documentName The document name to load.
* @param string $id The id of the document being loaded.
* @param string $data The data to load into the document.
* @return object $document The loaded document.
*/
public function load($documentName, $id, $data)
{
if ($data !== null) {
$hints = array(Query::HINT_REFRESH => Query::HINT_REFRESH);
return $this->_unitOfWork->getOrCreateDocument($documentName, $data, $hints);
}
return false;
}
/**
* 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.
*/
public function flush()
{
$this->_errorIfClosed();
$this->_unitOfWork->commit();
}
public function ensureDocumentIndexes($class)
{
if ($indexes = $class->getIndexes()) {
$collection = $this->getDocumentCollection($class->name);
foreach ($indexes as $index) {
$collection->ensureIndex($index['keys'], $index['options']);
}
}
}
public function deleteDocumentIndexes($documentName)
{
return $this->getDocumentCollection($documentName)->deleteIndexes();
}
public function mapReduce($documentName, $map, $reduce, array $query = array(), array $options = array())
{
$class = $this->getClassMetadata($documentName);
$db = $this->getDocumentDB($documentName);
if (is_string($map)) {
$map = new \MongoCode($map);
}
if (is_string($reduce)) {
$reduce = new \MongoCode($reduce);
}
$command = array(
'mapreduce' => $class->getCollection(),
'map' => $map,
'reduce' => $reduce,
'query' => $query
);
$command = array_merge($command, $options);
$result = $db->command($command);
if ( ! $result['ok']) {
throw new \RuntimeException($result['errmsg']);
}
$cursor = $db->selectCollection($result['result'])->find();
$cursor = new MongoCursor($this, $this->_hydrator, $class, $cursor);
$cursor->hydrate(false);
return $cursor;
}
/**
* 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.
*
* @return object The document reference.
*/
public function getReference($documentName, $identifier)
{
$class = $this->_metadataFactory->getMetadataFor($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;
}
/**
* Find a single document by its identifier or multiple by a given criteria.
*
* @param string $documentName The document to find.
* @param mixed $query A single identifier or an array of criteria.
* @param array $select The fields to select.
* @return Doctrine\ODM\MongoDB\MongoCursor $cursor
* @return object $document
*/
public function find($documentName, $query = array(), array $select = array())
{
return $this->getRepository($documentName)->find($query, $select);
}
/**
* Find a single document with the given query and select fields.
*
* @param string $documentName The document to find.
* @param array $query The query criteria.
* @param array $select The fields to select
* @return object $document
*/
public function findOne($documentName, array $query = array(), array $select = array())
{
return $this->getRepository($documentName)->findOne($query, $select);
}
/**
* Clears the DocumentManager. All documents that are currently managed
* by this DocumentManager become detached.
*
* @param string $documentName
*/
public function clear()
{
$this->_unitOfWork->clear();
}
/**
* 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;
}
/**
* Throws an exception if the EntityManager is closed or currently not active.
*
* @throws ORMException If the EntityManager is closed.
*/
private function _errorIfClosed()
{
if ($this->_closed) {
throw MongoDBException::documentManagerClosed();
}
}
public function formatDBName($dbName)
{
return sprintf('%s%s%s',
$this->_config->getDBPrefix(),
$dbName,
$this->_config->getDBSuffix()
);
}
}