Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like UnitOfWork often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UnitOfWork, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class UnitOfWork implements PropertyChangedListener |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 50 | */ |
||
| 51 | const STATE_MANAGED = 1; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 55 | * and is not (yet) managed by a DocumentManager. |
||
| 56 | */ |
||
| 57 | const STATE_NEW = 2; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * A detached document is an instance with a persistent identity that is not |
||
| 61 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 62 | */ |
||
| 63 | const STATE_DETACHED = 3; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * A removed document instance is an instance with a persistent identity, |
||
| 67 | * associated with a DocumentManager, whose persistent state has been |
||
| 68 | * deleted (or is scheduled for deletion). |
||
| 69 | */ |
||
| 70 | const STATE_REMOVED = 4; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The identity map holds references to all managed documents. |
||
| 74 | * |
||
| 75 | * Documents are grouped by their class name, and then indexed by the |
||
| 76 | * serialized string of their database identifier field or, if the class |
||
| 77 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 78 | * differentiation of values that may be equal (via type juggling) but not |
||
| 79 | * identical. |
||
| 80 | * |
||
| 81 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 82 | * we always take the root class name of the hierarchy. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private $identityMap = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Map of all identifiers of managed documents. |
||
| 90 | * Keys are object ids (spl_object_hash). |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private $documentIdentifiers = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Map of the original document data of managed documents. |
||
| 98 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 99 | * at commit time. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 103 | * A value will only really be copied if the value in the document is modified |
||
| 104 | * by the user. |
||
| 105 | */ |
||
| 106 | private $originalDocumentData = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 110 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | private $documentChangeSets = array(); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The (cached) states of any known documents. |
||
| 118 | * Keys are object ids (spl_object_hash). |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $documentStates = array(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 126 | * |
||
| 127 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 128 | * object hash. This is only used for documents with a change tracking |
||
| 129 | * policy of DEFERRED_EXPLICIT. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | * @todo rename: scheduledForSynchronization |
||
| 133 | */ |
||
| 134 | private $scheduledForDirtyCheck = array(); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * A list of all pending document insertions. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | private $documentInsertions = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * A list of all pending document updates. |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | private $documentUpdates = array(); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * A list of all pending document upserts. |
||
| 152 | * |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | private $documentUpserts = array(); |
||
| 156 | |||
| 157 | /** |
||
| 158 | * A list of all pending document deletions. |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | private $documentDeletions = array(); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * All pending collection deletions. |
||
| 166 | * |
||
| 167 | * @var array |
||
| 168 | */ |
||
| 169 | private $collectionDeletions = array(); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * All pending collection updates. |
||
| 173 | * |
||
| 174 | * @var array |
||
| 175 | */ |
||
| 176 | private $collectionUpdates = array(); |
||
| 177 | |||
| 178 | /** |
||
| 179 | * A list of documents related to collections scheduled for update or deletion |
||
| 180 | * |
||
| 181 | * @var array |
||
| 182 | */ |
||
| 183 | private $hasScheduledCollections = array(); |
||
| 184 | |||
| 185 | /** |
||
| 186 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 187 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 188 | * of their data. |
||
| 189 | * |
||
| 190 | * @var array |
||
| 191 | */ |
||
| 192 | private $visitedCollections = array(); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 196 | * |
||
| 197 | * @var DocumentManager |
||
| 198 | */ |
||
| 199 | private $dm; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The EventManager used for dispatching events. |
||
| 203 | * |
||
| 204 | * @var EventManager |
||
| 205 | */ |
||
| 206 | private $evm; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Additional documents that are scheduled for removal. |
||
| 210 | * |
||
| 211 | * @var array |
||
| 212 | */ |
||
| 213 | private $orphanRemovals = array(); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 217 | * |
||
| 218 | * @var HydratorFactory |
||
| 219 | */ |
||
| 220 | private $hydratorFactory; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * The document persister instances used to persist document instances. |
||
| 224 | * |
||
| 225 | * @var array |
||
| 226 | */ |
||
| 227 | private $persisters = array(); |
||
| 228 | |||
| 229 | /** |
||
| 230 | * The collection persister instance used to persist changes to collections. |
||
| 231 | * |
||
| 232 | * @var Persisters\CollectionPersister |
||
| 233 | */ |
||
| 234 | private $collectionPersister; |
||
| 235 | |||
| 236 | /** |
||
| 237 | * The persistence builder instance used in DocumentPersisters. |
||
| 238 | * |
||
| 239 | * @var PersistenceBuilder |
||
| 240 | */ |
||
| 241 | private $persistenceBuilder; |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Array of parent associations between embedded documents |
||
| 245 | * |
||
| 246 | * @todo We might need to clean up this array in clear(), doDetach(), etc. |
||
| 247 | * @var array |
||
| 248 | */ |
||
| 249 | private $parentAssociations = array(); |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @var LifecycleEventManager |
||
| 253 | */ |
||
| 254 | private $lifecycleEventManager; |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 258 | * |
||
| 259 | * @param DocumentManager $dm |
||
| 260 | * @param EventManager $evm |
||
| 261 | * @param HydratorFactory $hydratorFactory |
||
| 262 | */ |
||
| 263 | 940 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 264 | { |
||
| 265 | 940 | $this->dm = $dm; |
|
| 266 | 940 | $this->evm = $evm; |
|
| 267 | 940 | $this->hydratorFactory = $hydratorFactory; |
|
| 268 | 940 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 269 | 940 | } |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 273 | * queries for insert persistence. |
||
| 274 | * |
||
| 275 | * @return PersistenceBuilder $pb |
||
| 276 | */ |
||
| 277 | 680 | public function getPersistenceBuilder() |
|
| 278 | { |
||
| 279 | 680 | if ( ! $this->persistenceBuilder) { |
|
| 280 | 680 | $this->persistenceBuilder = new PersistenceBuilder($this->dm, $this); |
|
| 281 | 680 | } |
|
| 282 | 680 | return $this->persistenceBuilder; |
|
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Sets the parent association for a given embedded document. |
||
| 287 | * |
||
| 288 | * @param object $document |
||
| 289 | * @param array $mapping |
||
| 290 | * @param object $parent |
||
| 291 | * @param string $propertyPath |
||
| 292 | */ |
||
| 293 | 182 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 294 | { |
||
| 295 | 182 | $oid = spl_object_hash($document); |
|
| 296 | 182 | $this->parentAssociations[$oid] = array($mapping, $parent, $propertyPath); |
|
| 297 | 182 | } |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Gets the parent association for a given embedded document. |
||
| 301 | * |
||
| 302 | * <code> |
||
| 303 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 304 | * </code> |
||
| 305 | * |
||
| 306 | * @param object $document |
||
| 307 | * @return array $association |
||
| 308 | */ |
||
| 309 | 208 | public function getParentAssociation($document) |
|
| 310 | { |
||
| 311 | 208 | $oid = spl_object_hash($document); |
|
| 312 | |||
| 313 | 208 | return isset($this->parentAssociations[$oid]) |
|
| 314 | 208 | ? $this->parentAssociations[$oid] |
|
| 315 | 208 | : null; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get the document persister instance for the given document name |
||
| 320 | * |
||
| 321 | * @param string $documentName |
||
| 322 | * @return Persisters\DocumentPersister |
||
| 323 | */ |
||
| 324 | 678 | public function getDocumentPersister($documentName) |
|
| 325 | { |
||
| 326 | 678 | if ( ! isset($this->persisters[$documentName])) { |
|
| 327 | 664 | $class = $this->dm->getClassMetadata($documentName); |
|
| 328 | 664 | $pb = $this->getPersistenceBuilder(); |
|
| 329 | 664 | $this->persisters[$documentName] = new Persisters\DocumentPersister($pb, $this->dm, $this->evm, $this, $this->hydratorFactory, $class); |
|
| 330 | 664 | } |
|
| 331 | 678 | return $this->persisters[$documentName]; |
|
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get the collection persister instance. |
||
| 336 | * |
||
| 337 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 338 | */ |
||
| 339 | 678 | public function getCollectionPersister() |
|
| 340 | { |
||
| 341 | 678 | if ( ! isset($this->collectionPersister)) { |
|
| 342 | 678 | $pb = $this->getPersistenceBuilder(); |
|
| 343 | 678 | $this->collectionPersister = new Persisters\CollectionPersister($this->dm, $pb, $this); |
|
| 344 | 678 | } |
|
| 345 | 678 | return $this->collectionPersister; |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set the document persister instance to use for the given document name |
||
| 350 | * |
||
| 351 | * @param string $documentName |
||
| 352 | * @param Persisters\DocumentPersister $persister |
||
| 353 | */ |
||
| 354 | 14 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 361 | * up to this point. The state of all managed documents will be synchronized with |
||
| 362 | * the database. |
||
| 363 | * |
||
| 364 | * The operations are executed in the following order: |
||
| 365 | * |
||
| 366 | * 1) All document insertions |
||
| 367 | * 2) All document updates |
||
| 368 | * 3) All document deletions |
||
| 369 | * |
||
| 370 | * @param object $document |
||
| 371 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 372 | */ |
||
| 373 | 562 | public function commit($document = null, array $options = array()) |
|
| 374 | { |
||
| 375 | // Raise preFlush |
||
| 376 | 562 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 377 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
||
| 378 | } |
||
| 379 | |||
| 380 | 562 | $defaultOptions = $this->dm->getConfiguration()->getDefaultCommitOptions(); |
|
| 381 | 562 | if ($options) { |
|
|
|
|||
| 382 | $options = array_merge($defaultOptions, $options); |
||
| 383 | } else { |
||
| 384 | 562 | $options = $defaultOptions; |
|
| 385 | } |
||
| 386 | // Compute changes done since last commit. |
||
| 387 | 562 | if ($document === null) { |
|
| 388 | 556 | $this->computeChangeSets(); |
|
| 389 | 561 | } elseif (is_object($document)) { |
|
| 390 | 12 | $this->computeSingleDocumentChangeSet($document); |
|
| 391 | 12 | } elseif (is_array($document)) { |
|
| 392 | 1 | foreach ($document as $object) { |
|
| 393 | 1 | $this->computeSingleDocumentChangeSet($object); |
|
| 394 | 1 | } |
|
| 395 | 1 | } |
|
| 396 | |||
| 397 | 560 | if ( ! ($this->documentInsertions || |
|
| 398 | 240 | $this->documentUpserts || |
|
| 399 | 203 | $this->documentDeletions || |
|
| 400 | 193 | $this->documentUpdates || |
|
| 401 | 24 | $this->collectionUpdates || |
|
| 402 | 23 | $this->collectionDeletions || |
|
| 403 | 23 | $this->orphanRemovals) |
|
| 404 | 560 | ) { |
|
| 405 | 23 | return; // Nothing to do. |
|
| 406 | } |
||
| 407 | |||
| 408 | 557 | if ($this->orphanRemovals) { |
|
| 409 | 47 | foreach ($this->orphanRemovals as $removal) { |
|
| 410 | 47 | $this->remove($removal); |
|
| 411 | 47 | } |
|
| 412 | 47 | } |
|
| 413 | |||
| 414 | // Raise onFlush |
||
| 415 | 557 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 416 | 7 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 417 | 7 | } |
|
| 418 | |||
| 419 | 557 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 420 | 78 | list($class, $documents) = $classAndDocuments; |
|
| 421 | 78 | $this->executeUpserts($class, $documents, $options); |
|
| 422 | 557 | } |
|
| 423 | |||
| 424 | 557 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 425 | 490 | list($class, $documents) = $classAndDocuments; |
|
| 426 | 490 | $this->executeInserts($class, $documents, $options); |
|
| 427 | 556 | } |
|
| 428 | |||
| 429 | 556 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 430 | 220 | list($class, $documents) = $classAndDocuments; |
|
| 431 | 220 | $this->executeUpdates($class, $documents, $options); |
|
| 432 | 556 | } |
|
| 433 | |||
| 434 | 556 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 435 | 64 | list($class, $documents) = $classAndDocuments; |
|
| 436 | 64 | $this->executeDeletions($class, $documents, $options); |
|
| 437 | 556 | } |
|
| 438 | |||
| 439 | // Raise postFlush |
||
| 440 | 556 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 441 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 442 | } |
||
| 443 | |||
| 444 | // Clear up |
||
| 445 | 556 | $this->documentInsertions = |
|
| 446 | 556 | $this->documentUpserts = |
|
| 447 | 556 | $this->documentUpdates = |
|
| 448 | 556 | $this->documentDeletions = |
|
| 449 | 556 | $this->documentChangeSets = |
|
| 450 | 556 | $this->collectionUpdates = |
|
| 451 | 556 | $this->collectionDeletions = |
|
| 452 | 556 | $this->visitedCollections = |
|
| 453 | 556 | $this->scheduledForDirtyCheck = |
|
| 454 | 556 | $this->orphanRemovals = |
|
| 455 | 556 | $this->hasScheduledCollections = array(); |
|
| 456 | 556 | } |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Groups a list of scheduled documents by their class. |
||
| 460 | * |
||
| 461 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 462 | * @param bool $includeEmbedded |
||
| 463 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 464 | */ |
||
| 465 | 557 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 466 | { |
||
| 467 | 557 | if (empty($documents)) { |
|
| 468 | 557 | return array(); |
|
| 469 | } |
||
| 470 | 556 | $divided = array(); |
|
| 471 | 556 | $embeds = array(); |
|
| 472 | 556 | foreach ($documents as $oid => $d) { |
|
| 473 | 556 | $className = get_class($d); |
|
| 474 | 556 | if (isset($embeds[$className])) { |
|
| 475 | 69 | continue; |
|
| 476 | } |
||
| 477 | 556 | if (isset($divided[$className])) { |
|
| 478 | 136 | $divided[$className][1][$oid] = $d; |
|
| 479 | 136 | continue; |
|
| 480 | } |
||
| 481 | 556 | $class = $this->dm->getClassMetadata($className); |
|
| 482 | 556 | if ($class->isEmbeddedDocument && ! $includeEmbedded) { |
|
| 483 | 167 | $embeds[$className] = true; |
|
| 484 | 167 | continue; |
|
| 485 | } |
||
| 486 | 556 | if (empty($divided[$class->name])) { |
|
| 487 | 556 | $divided[$class->name] = array($class, array($oid => $d)); |
|
| 488 | 556 | } else { |
|
| 489 | 4 | $divided[$class->name][1][$oid] = $d; |
|
| 490 | } |
||
| 491 | 556 | } |
|
| 492 | 556 | return $divided; |
|
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Compute changesets of all documents scheduled for insertion. |
||
| 497 | * |
||
| 498 | * Embedded documents will not be processed. |
||
| 499 | */ |
||
| 500 | 564 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 501 | { |
||
| 502 | 564 | foreach ($this->documentInsertions as $document) { |
|
| 503 | 498 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 504 | 498 | if ( ! $class->isEmbeddedDocument) { |
|
| 505 | 495 | $this->computeChangeSet($class, $document); |
|
| 506 | 494 | } |
|
| 507 | 563 | } |
|
| 508 | 563 | } |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Compute changesets of all documents scheduled for upsert. |
||
| 512 | * |
||
| 513 | * Embedded documents will not be processed. |
||
| 514 | */ |
||
| 515 | 563 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 516 | { |
||
| 517 | 563 | foreach ($this->documentUpserts as $document) { |
|
| 518 | 77 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 519 | 77 | if ( ! $class->isEmbeddedDocument) { |
|
| 520 | 77 | $this->computeChangeSet($class, $document); |
|
| 521 | 77 | } |
|
| 522 | 563 | } |
|
| 523 | 563 | } |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Only flush the given document according to a ruleset that keeps the UoW consistent. |
||
| 527 | * |
||
| 528 | * 1. All documents scheduled for insertion and (orphan) removals are processed as well! |
||
| 529 | * 2. Proxies are skipped. |
||
| 530 | * 3. Only if document is properly managed. |
||
| 531 | * |
||
| 532 | * @param object $document |
||
| 533 | * @throws \InvalidArgumentException If the document is not STATE_MANAGED |
||
| 534 | * @return void |
||
| 535 | */ |
||
| 536 | 13 | private function computeSingleDocumentChangeSet($document) |
|
| 537 | { |
||
| 538 | 13 | $state = $this->getDocumentState($document); |
|
| 539 | |||
| 540 | 13 | if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { |
|
| 541 | 1 | throw new \InvalidArgumentException("Document has to be managed or scheduled for removal for single computation " . $this->objToStr($document)); |
|
| 542 | } |
||
| 543 | |||
| 544 | 12 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 545 | |||
| 546 | 12 | if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) { |
|
| 547 | 9 | $this->persist($document); |
|
| 548 | 9 | } |
|
| 549 | |||
| 550 | // Compute changes for INSERTed and UPSERTed documents first. This must always happen even in this case. |
||
| 551 | 12 | $this->computeScheduleInsertsChangeSets(); |
|
| 552 | 12 | $this->computeScheduleUpsertsChangeSets(); |
|
| 553 | |||
| 554 | // Ignore uninitialized proxy objects |
||
| 555 | 12 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 556 | return; |
||
| 557 | } |
||
| 558 | |||
| 559 | // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. |
||
| 560 | 12 | $oid = spl_object_hash($document); |
|
| 561 | |||
| 562 | 12 | View Code Duplication | if ( ! isset($this->documentInsertions[$oid]) |
| 563 | 12 | && ! isset($this->documentUpserts[$oid]) |
|
| 564 | 12 | && ! isset($this->documentDeletions[$oid]) |
|
| 565 | 12 | && isset($this->documentStates[$oid]) |
|
| 566 | 12 | ) { |
|
| 567 | 8 | $this->computeChangeSet($class, $document); |
|
| 568 | 8 | } |
|
| 569 | 12 | } |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Gets the changeset for a document. |
||
| 573 | * |
||
| 574 | * @param object $document |
||
| 575 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 576 | */ |
||
| 577 | 554 | public function getDocumentChangeSet($document) |
|
| 578 | { |
||
| 579 | 554 | $oid = spl_object_hash($document); |
|
| 580 | |||
| 581 | 554 | return isset($this->documentChangeSets[$oid]) |
|
| 582 | 554 | ? $this->documentChangeSets[$oid] |
|
| 583 | 554 | : []; |
|
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * INTERNAL: |
||
| 588 | * Sets the changeset for a document. |
||
| 589 | * |
||
| 590 | * @param object $document |
||
| 591 | * @param array $changeset |
||
| 592 | */ |
||
| 593 | 1 | public function setDocumentChangeSet($document, $changeset) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 600 | * |
||
| 601 | * @param object $document |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | 561 | public function getDocumentActualData($document) |
|
| 605 | { |
||
| 606 | 561 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 607 | 561 | $actualData = array(); |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Computes the changes that happened to a single document. |
||
| 641 | * |
||
| 642 | * Modifies/populates the following properties: |
||
| 643 | * |
||
| 644 | * {@link originalDocumentData} |
||
| 645 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 646 | * then it was not fetched from the database and therefore we have no original |
||
| 647 | * document data yet. All of the current document data is stored as the original document data. |
||
| 648 | * |
||
| 649 | * {@link documentChangeSets} |
||
| 650 | * The changes detected on all properties of the document are stored there. |
||
| 651 | * A change is a tuple array where the first entry is the old value and the second |
||
| 652 | * entry is the new value of the property. Changesets are used by persisters |
||
| 653 | * to INSERT/UPDATE the persistent document state. |
||
| 654 | * |
||
| 655 | * {@link documentUpdates} |
||
| 656 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 657 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 658 | * there to mark it for an update. |
||
| 659 | * |
||
| 660 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 661 | * @param object $document The document for which to compute the changes. |
||
| 662 | */ |
||
| 663 | 561 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 676 | |||
| 677 | /** |
||
| 678 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 679 | * |
||
| 680 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class |
||
| 681 | * @param object $document |
||
| 682 | * @param boolean $recompute |
||
| 683 | */ |
||
| 684 | 561 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 852 | |||
| 853 | /** |
||
| 854 | * Computes all the changes that have been done to documents and collections |
||
| 855 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 856 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 857 | */ |
||
| 858 | 559 | public function computeChangeSets() |
|
| 908 | |||
| 909 | /** |
||
| 910 | * Computes the changes of an association. |
||
| 911 | * |
||
| 912 | * @param object $parentDocument |
||
| 913 | * @param array $assoc |
||
| 914 | * @param mixed $value The value of the association. |
||
| 915 | * @throws \InvalidArgumentException |
||
| 916 | */ |
||
| 917 | 423 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 1018 | |||
| 1019 | /** |
||
| 1020 | * INTERNAL: |
||
| 1021 | * Computes the changeset of an individual document, independently of the |
||
| 1022 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1023 | * |
||
| 1024 | * The passed document must be a managed document. If the document already has a change set |
||
| 1025 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1026 | * whereby changes detected in this method prevail. |
||
| 1027 | * |
||
| 1028 | * @ignore |
||
| 1029 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1030 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1031 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1032 | */ |
||
| 1033 | 20 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1052 | |||
| 1053 | /** |
||
| 1054 | * @param ClassMetadata $class |
||
| 1055 | * @param object $document |
||
| 1056 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1057 | */ |
||
| 1058 | 585 | private function persistNew(ClassMetadata $class, $document) |
|
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Executes all document insertions for documents of the specified type. |
||
| 1104 | * |
||
| 1105 | * @param ClassMetadata $class |
||
| 1106 | * @param array $documents Array of documents to insert |
||
| 1107 | * @param array $options Array of options to be used with batchInsert() |
||
| 1108 | */ |
||
| 1109 | 490 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Executes all document upserts for documents of the specified type. |
||
| 1127 | * |
||
| 1128 | * @param ClassMetadata $class |
||
| 1129 | * @param array $documents Array of documents to upsert |
||
| 1130 | * @param array $options Array of options to be used with batchInsert() |
||
| 1131 | */ |
||
| 1132 | 78 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Executes all document updates for documents of the specified type. |
||
| 1151 | * |
||
| 1152 | * @param Mapping\ClassMetadata $class |
||
| 1153 | * @param array $documents Array of documents to update |
||
| 1154 | * @param array $options Array of options to be used with update() |
||
| 1155 | */ |
||
| 1156 | 220 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Executes all document deletions for documents of the specified type. |
||
| 1176 | * |
||
| 1177 | * @param ClassMetadata $class |
||
| 1178 | * @param array $documents Array of documents to delete |
||
| 1179 | * @param array $options Array of options to be used with remove() |
||
| 1180 | */ |
||
| 1181 | 64 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Schedules a document for insertion into the database. |
||
| 1216 | * If the document already has an identifier, it will be added to the |
||
| 1217 | * identity map. |
||
| 1218 | * |
||
| 1219 | * @param ClassMetadata $class |
||
| 1220 | * @param object $document The document to schedule for insertion. |
||
| 1221 | * @throws \InvalidArgumentException |
||
| 1222 | */ |
||
| 1223 | 520 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Schedules a document for upsert into the database and adds it to the |
||
| 1246 | * identity map |
||
| 1247 | * |
||
| 1248 | * @param ClassMetadata $class |
||
| 1249 | * @param object $document The document to schedule for upsert. |
||
| 1250 | * @throws \InvalidArgumentException |
||
| 1251 | */ |
||
| 1252 | 84 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Checks whether a document is scheduled for insertion. |
||
| 1276 | * |
||
| 1277 | * @param object $document |
||
| 1278 | * @return boolean |
||
| 1279 | */ |
||
| 1280 | 101 | public function isScheduledForInsert($document) |
|
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Checks whether a document is scheduled for upsert. |
||
| 1287 | * |
||
| 1288 | * @param object $document |
||
| 1289 | * @return boolean |
||
| 1290 | */ |
||
| 1291 | 5 | public function isScheduledForUpsert($document) |
|
| 1295 | |||
| 1296 | /** |
||
| 1297 | * Schedules a document for being updated. |
||
| 1298 | * |
||
| 1299 | * @param object $document The document to schedule for being updated. |
||
| 1300 | * @throws \InvalidArgumentException |
||
| 1301 | */ |
||
| 1302 | 229 | public function scheduleForUpdate($document) |
|
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1322 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1323 | * at commit time. |
||
| 1324 | * |
||
| 1325 | * @param object $document |
||
| 1326 | * @return boolean |
||
| 1327 | */ |
||
| 1328 | 13 | public function isScheduledForUpdate($document) |
|
| 1332 | |||
| 1333 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1338 | |||
| 1339 | /** |
||
| 1340 | * INTERNAL: |
||
| 1341 | * Schedules a document for deletion. |
||
| 1342 | * |
||
| 1343 | * @param object $document |
||
| 1344 | */ |
||
| 1345 | 69 | public function scheduleForDelete($document) |
|
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1374 | * of work. |
||
| 1375 | * |
||
| 1376 | * @param object $document |
||
| 1377 | * @return boolean |
||
| 1378 | */ |
||
| 1379 | 8 | public function isScheduledForDelete($document) |
|
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1386 | * |
||
| 1387 | * @param $document |
||
| 1388 | * @return boolean |
||
| 1389 | */ |
||
| 1390 | 226 | public function isDocumentScheduled($document) |
|
| 1398 | |||
| 1399 | /** |
||
| 1400 | * INTERNAL: |
||
| 1401 | * Registers a document in the identity map. |
||
| 1402 | * |
||
| 1403 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1404 | * the root document. Identifiers are serialized before being used as array |
||
| 1405 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1406 | * |
||
| 1407 | * @ignore |
||
| 1408 | * @param object $document The document to register. |
||
| 1409 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1410 | * the document in question is already managed. |
||
| 1411 | */ |
||
| 1412 | 614 | public function addToIdentityMap($document) |
|
| 1430 | |||
| 1431 | /** |
||
| 1432 | * Gets the state of a document with regard to the current unit of work. |
||
| 1433 | * |
||
| 1434 | * @param object $document |
||
| 1435 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1436 | * This parameter can be set to improve performance of document state detection |
||
| 1437 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1438 | * is either known or does not matter for the caller of the method. |
||
| 1439 | * @return int The document state. |
||
| 1440 | */ |
||
| 1441 | 588 | public function getDocumentState($document, $assume = null) |
|
| 1491 | |||
| 1492 | /** |
||
| 1493 | * INTERNAL: |
||
| 1494 | * Removes a document from the identity map. This effectively detaches the |
||
| 1495 | * document from the persistence management of Doctrine. |
||
| 1496 | * |
||
| 1497 | * @ignore |
||
| 1498 | * @param object $document |
||
| 1499 | * @throws \InvalidArgumentException |
||
| 1500 | * @return boolean |
||
| 1501 | */ |
||
| 1502 | 78 | public function removeFromIdentityMap($document) |
|
| 1522 | |||
| 1523 | /** |
||
| 1524 | * INTERNAL: |
||
| 1525 | * Gets a document in the identity map by its identifier hash. |
||
| 1526 | * |
||
| 1527 | * @ignore |
||
| 1528 | * @param mixed $id Document identifier |
||
| 1529 | * @param ClassMetadata $class Document class |
||
| 1530 | * @return object |
||
| 1531 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1532 | */ |
||
| 1533 | 31 | public function getById($id, ClassMetadata $class) |
|
| 1543 | |||
| 1544 | /** |
||
| 1545 | * INTERNAL: |
||
| 1546 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1547 | * for the given hash, FALSE is returned. |
||
| 1548 | * |
||
| 1549 | * @ignore |
||
| 1550 | * @param mixed $id Document identifier |
||
| 1551 | * @param ClassMetadata $class Document class |
||
| 1552 | * @return mixed The found document or FALSE. |
||
| 1553 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1554 | */ |
||
| 1555 | 292 | public function tryGetById($id, ClassMetadata $class) |
|
| 1566 | |||
| 1567 | /** |
||
| 1568 | * Schedules a document for dirty-checking at commit-time. |
||
| 1569 | * |
||
| 1570 | * @param object $document The document to schedule for dirty-checking. |
||
| 1571 | * @todo Rename: scheduleForSynchronization |
||
| 1572 | */ |
||
| 1573 | 2 | public function scheduleForDirtyCheck($document) |
|
| 1578 | |||
| 1579 | /** |
||
| 1580 | * Checks whether a document is registered in the identity map. |
||
| 1581 | * |
||
| 1582 | * @param object $document |
||
| 1583 | * @return boolean |
||
| 1584 | */ |
||
| 1585 | 78 | public function isInIdentityMap($document) |
|
| 1598 | |||
| 1599 | /** |
||
| 1600 | * @param object $document |
||
| 1601 | * @return string |
||
| 1602 | */ |
||
| 1603 | 614 | private function getIdForIdentityMap($document) |
|
| 1616 | |||
| 1617 | /** |
||
| 1618 | * INTERNAL: |
||
| 1619 | * Checks whether an identifier exists in the identity map. |
||
| 1620 | * |
||
| 1621 | * @ignore |
||
| 1622 | * @param string $id |
||
| 1623 | * @param string $rootClassName |
||
| 1624 | * @return boolean |
||
| 1625 | */ |
||
| 1626 | public function containsId($id, $rootClassName) |
||
| 1630 | |||
| 1631 | /** |
||
| 1632 | * Persists a document as part of the current unit of work. |
||
| 1633 | * |
||
| 1634 | * @param object $document The document to persist. |
||
| 1635 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1636 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1637 | */ |
||
| 1638 | 583 | public function persist($document) |
|
| 1647 | |||
| 1648 | /** |
||
| 1649 | * Saves a document as part of the current unit of work. |
||
| 1650 | * This method is internally called during save() cascades as it tracks |
||
| 1651 | * the already visited documents to prevent infinite recursions. |
||
| 1652 | * |
||
| 1653 | * NOTE: This method always considers documents that are not yet known to |
||
| 1654 | * this UnitOfWork as NEW. |
||
| 1655 | * |
||
| 1656 | * @param object $document The document to persist. |
||
| 1657 | * @param array $visited The already visited documents. |
||
| 1658 | * @throws \InvalidArgumentException |
||
| 1659 | * @throws MongoDBException |
||
| 1660 | */ |
||
| 1661 | 582 | private function doPersist($document, array &$visited) |
|
| 1701 | |||
| 1702 | /** |
||
| 1703 | * Deletes a document as part of the current unit of work. |
||
| 1704 | * |
||
| 1705 | * @param object $document The document to remove. |
||
| 1706 | */ |
||
| 1707 | 68 | public function remove($document) |
|
| 1712 | |||
| 1713 | /** |
||
| 1714 | * Deletes a document as part of the current unit of work. |
||
| 1715 | * |
||
| 1716 | * This method is internally called during delete() cascades as it tracks |
||
| 1717 | * the already visited documents to prevent infinite recursions. |
||
| 1718 | * |
||
| 1719 | * @param object $document The document to delete. |
||
| 1720 | * @param array $visited The map of the already visited documents. |
||
| 1721 | * @throws MongoDBException |
||
| 1722 | */ |
||
| 1723 | 68 | private function doRemove($document, array &$visited) |
|
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1758 | * |
||
| 1759 | * @param object $document |
||
| 1760 | * @return object The managed copy of the document. |
||
| 1761 | */ |
||
| 1762 | 13 | public function merge($document) |
|
| 1768 | |||
| 1769 | /** |
||
| 1770 | * Executes a merge operation on a document. |
||
| 1771 | * |
||
| 1772 | * @param object $document |
||
| 1773 | * @param array $visited |
||
| 1774 | * @param object|null $prevManagedCopy |
||
| 1775 | * @param array|null $assoc |
||
| 1776 | * |
||
| 1777 | * @return object The managed copy of the document. |
||
| 1778 | * |
||
| 1779 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1780 | * @throws LockException If the document uses optimistic locking through a |
||
| 1781 | * version attribute and the version check against the |
||
| 1782 | * managed copy fails. |
||
| 1783 | */ |
||
| 1784 | 13 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1962 | |||
| 1963 | /** |
||
| 1964 | * Detaches a document from the persistence management. It's persistence will |
||
| 1965 | * no longer be managed by Doctrine. |
||
| 1966 | * |
||
| 1967 | * @param object $document The document to detach. |
||
| 1968 | */ |
||
| 1969 | 9 | public function detach($document) |
|
| 1974 | |||
| 1975 | /** |
||
| 1976 | * Executes a detach operation on the given document. |
||
| 1977 | * |
||
| 1978 | * @param object $document |
||
| 1979 | * @param array $visited |
||
| 1980 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1981 | */ |
||
| 1982 | 12 | private function doDetach($document, array &$visited) |
|
| 2007 | |||
| 2008 | /** |
||
| 2009 | * Refreshes the state of the given document from the database, overwriting |
||
| 2010 | * any local, unpersisted changes. |
||
| 2011 | * |
||
| 2012 | * @param object $document The document to refresh. |
||
| 2013 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2014 | */ |
||
| 2015 | 21 | public function refresh($document) |
|
| 2020 | |||
| 2021 | /** |
||
| 2022 | * Executes a refresh operation on a document. |
||
| 2023 | * |
||
| 2024 | * @param object $document The document to refresh. |
||
| 2025 | * @param array $visited The already visited documents during cascades. |
||
| 2026 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2027 | */ |
||
| 2028 | 21 | private function doRefresh($document, array &$visited) |
|
| 2050 | |||
| 2051 | /** |
||
| 2052 | * Cascades a refresh operation to associated documents. |
||
| 2053 | * |
||
| 2054 | * @param object $document |
||
| 2055 | * @param array $visited |
||
| 2056 | */ |
||
| 2057 | 20 | private function cascadeRefresh($document, array &$visited) |
|
| 2081 | |||
| 2082 | /** |
||
| 2083 | * Cascades a detach operation to associated documents. |
||
| 2084 | * |
||
| 2085 | * @param object $document |
||
| 2086 | * @param array $visited |
||
| 2087 | */ |
||
| 2088 | 12 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2109 | /** |
||
| 2110 | * Cascades a merge operation to associated documents. |
||
| 2111 | * |
||
| 2112 | * @param object $document |
||
| 2113 | * @param object $managedCopy |
||
| 2114 | * @param array $visited |
||
| 2115 | */ |
||
| 2116 | 13 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2147 | |||
| 2148 | /** |
||
| 2149 | * Cascades the save operation to associated documents. |
||
| 2150 | * |
||
| 2151 | * @param object $document |
||
| 2152 | * @param array $visited |
||
| 2153 | */ |
||
| 2154 | 580 | private function cascadePersist($document, array &$visited) |
|
| 2201 | |||
| 2202 | /** |
||
| 2203 | * Cascades the delete operation to associated documents. |
||
| 2204 | * |
||
| 2205 | * @param object $document |
||
| 2206 | * @param array $visited |
||
| 2207 | */ |
||
| 2208 | 68 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2230 | |||
| 2231 | /** |
||
| 2232 | * Acquire a lock on the given document. |
||
| 2233 | * |
||
| 2234 | * @param object $document |
||
| 2235 | * @param int $lockMode |
||
| 2236 | * @param int $lockVersion |
||
| 2237 | * @throws LockException |
||
| 2238 | * @throws \InvalidArgumentException |
||
| 2239 | */ |
||
| 2240 | 9 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2264 | |||
| 2265 | /** |
||
| 2266 | * Releases a lock on the given document. |
||
| 2267 | * |
||
| 2268 | * @param object $document |
||
| 2269 | * @throws \InvalidArgumentException |
||
| 2270 | */ |
||
| 2271 | 1 | public function unlock($document) |
|
| 2279 | |||
| 2280 | /** |
||
| 2281 | * Clears the UnitOfWork. |
||
| 2282 | * |
||
| 2283 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2284 | */ |
||
| 2285 | 391 | public function clear($documentName = null) |
|
| 2318 | |||
| 2319 | /** |
||
| 2320 | * INTERNAL: |
||
| 2321 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2322 | * invoked on that document at the beginning of the next commit of this |
||
| 2323 | * UnitOfWork. |
||
| 2324 | * |
||
| 2325 | * @ignore |
||
| 2326 | * @param object $document |
||
| 2327 | */ |
||
| 2328 | 49 | public function scheduleOrphanRemoval($document) |
|
| 2332 | |||
| 2333 | /** |
||
| 2334 | * INTERNAL: |
||
| 2335 | * Unschedules an embedded or referenced object for removal. |
||
| 2336 | * |
||
| 2337 | * @ignore |
||
| 2338 | * @param object $document |
||
| 2339 | */ |
||
| 2340 | 104 | public function unscheduleOrphanRemoval($document) |
|
| 2347 | |||
| 2348 | /** |
||
| 2349 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2350 | * 1) sets owner if it was cloned |
||
| 2351 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2352 | * 3) NOP if state is OK |
||
| 2353 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2354 | * |
||
| 2355 | * @param PersistentCollection $coll |
||
| 2356 | * @param object $document |
||
| 2357 | * @param ClassMetadata $class |
||
| 2358 | * @param string $propName |
||
| 2359 | * @return PersistentCollection |
||
| 2360 | */ |
||
| 2361 | 8 | private function fixPersistentCollectionOwnership(PersistentCollection $coll, $document, ClassMetadata $class, $propName) |
|
| 2381 | |||
| 2382 | /** |
||
| 2383 | * INTERNAL: |
||
| 2384 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2385 | * |
||
| 2386 | * @param PersistentCollection $coll |
||
| 2387 | */ |
||
| 2388 | 42 | public function scheduleCollectionDeletion(PersistentCollection $coll) |
|
| 2397 | |||
| 2398 | /** |
||
| 2399 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2400 | * |
||
| 2401 | * @param PersistentCollection $coll |
||
| 2402 | * @return boolean |
||
| 2403 | */ |
||
| 2404 | 207 | public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
|
| 2408 | |||
| 2409 | /** |
||
| 2410 | * INTERNAL: |
||
| 2411 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2412 | * |
||
| 2413 | * @param \Doctrine\ODM\MongoDB\PersistentCollection $coll |
||
| 2414 | */ |
||
| 2415 | 207 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollection $coll) |
| 2424 | |||
| 2425 | /** |
||
| 2426 | * INTERNAL: |
||
| 2427 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2428 | * |
||
| 2429 | * @param PersistentCollection $coll |
||
| 2430 | */ |
||
| 2431 | 223 | public function scheduleCollectionUpdate(PersistentCollection $coll) |
|
| 2446 | |||
| 2447 | /** |
||
| 2448 | * INTERNAL: |
||
| 2449 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2450 | * |
||
| 2451 | * @param \Doctrine\ODM\MongoDB\PersistentCollection $coll |
||
| 2452 | */ |
||
| 2453 | 207 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollection $coll) |
| 2462 | |||
| 2463 | /** |
||
| 2464 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2465 | * |
||
| 2466 | * @param PersistentCollection $coll |
||
| 2467 | * @return boolean |
||
| 2468 | */ |
||
| 2469 | 123 | public function isCollectionScheduledForUpdate(PersistentCollection $coll) |
|
| 2473 | |||
| 2474 | /** |
||
| 2475 | * INTERNAL: |
||
| 2476 | * Gets PersistentCollections that have been visited during computing change |
||
| 2477 | * set of $document |
||
| 2478 | * |
||
| 2479 | * @param object $document |
||
| 2480 | * @return PersistentCollection[] |
||
| 2481 | */ |
||
| 2482 | 542 | public function getVisitedCollections($document) |
|
| 2489 | |||
| 2490 | /** |
||
| 2491 | * INTERNAL: |
||
| 2492 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2493 | * |
||
| 2494 | * @param object $document |
||
| 2495 | * @return array |
||
| 2496 | */ |
||
| 2497 | 542 | public function getScheduledCollections($document) |
|
| 2504 | |||
| 2505 | /** |
||
| 2506 | * Checks whether the document is related to a PersistentCollection |
||
| 2507 | * scheduled for update or deletion. |
||
| 2508 | * |
||
| 2509 | * @param object $document |
||
| 2510 | * @return boolean |
||
| 2511 | */ |
||
| 2512 | 49 | public function hasScheduledCollections($document) |
|
| 2516 | |||
| 2517 | /** |
||
| 2518 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2519 | * a collection scheduled for update or deletion. |
||
| 2520 | * |
||
| 2521 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2522 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2523 | * |
||
| 2524 | * If the collection is nested within atomic collection, it is immediately |
||
| 2525 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2526 | * calculating update data way easier. |
||
| 2527 | * |
||
| 2528 | * @param PersistentCollection $coll |
||
| 2529 | */ |
||
| 2530 | 225 | private function scheduleCollectionOwner(PersistentCollection $coll) |
|
| 2553 | |||
| 2554 | /** |
||
| 2555 | * Get the top-most owning document of a given document |
||
| 2556 | * |
||
| 2557 | * If a top-level document is provided, that same document will be returned. |
||
| 2558 | * For an embedded document, we will walk through parent associations until |
||
| 2559 | * we find a top-level document. |
||
| 2560 | * |
||
| 2561 | * @param object $document |
||
| 2562 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2563 | * @return object |
||
| 2564 | */ |
||
| 2565 | 227 | public function getOwningDocument($document) |
|
| 2581 | |||
| 2582 | /** |
||
| 2583 | * Gets the class name for an association (embed or reference) with respect |
||
| 2584 | * to any discriminator value. |
||
| 2585 | * |
||
| 2586 | * @param array $mapping Field mapping for the association |
||
| 2587 | * @param array|null $data Data for the embedded document or reference |
||
| 2588 | */ |
||
| 2589 | 208 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2622 | |||
| 2623 | /** |
||
| 2624 | * INTERNAL: |
||
| 2625 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2626 | * |
||
| 2627 | * @ignore |
||
| 2628 | * @param string $className The name of the document class. |
||
| 2629 | * @param array $data The data for the document. |
||
| 2630 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2631 | * @param object The document to be hydrated into in case of creation |
||
| 2632 | * @return object The document instance. |
||
| 2633 | * @internal Highly performance-sensitive method. |
||
| 2634 | */ |
||
| 2635 | 386 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2689 | |||
| 2690 | /** |
||
| 2691 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2692 | * |
||
| 2693 | * @param PersistentCollection $collection The collection to initialize. |
||
| 2694 | */ |
||
| 2695 | 157 | public function loadCollection(PersistentCollection $collection) |
|
| 2699 | |||
| 2700 | /** |
||
| 2701 | * Gets the identity map of the UnitOfWork. |
||
| 2702 | * |
||
| 2703 | * @return array |
||
| 2704 | */ |
||
| 2705 | public function getIdentityMap() |
||
| 2709 | |||
| 2710 | /** |
||
| 2711 | * Gets the original data of a document. The original data is the data that was |
||
| 2712 | * present at the time the document was reconstituted from the database. |
||
| 2713 | * |
||
| 2714 | * @param object $document |
||
| 2715 | * @return array |
||
| 2716 | */ |
||
| 2717 | 1 | public function getOriginalDocumentData($document) |
|
| 2718 | { |
||
| 2719 | 1 | $oid = spl_object_hash($document); |
|
| 2720 | |||
| 2721 | 1 | return isset($this->originalDocumentData[$oid]) |
|
| 2722 | 1 | ? $this->originalDocumentData[$oid] |
|
| 2723 | 1 | : []; |
|
| 2724 | } |
||
| 2725 | |||
| 2726 | /** |
||
| 2727 | * @ignore |
||
| 2728 | */ |
||
| 2729 | 51 | public function setOriginalDocumentData($document, array $data) |
|
| 2735 | |||
| 2736 | /** |
||
| 2737 | * INTERNAL: |
||
| 2738 | * Sets a property value of the original data array of a document. |
||
| 2739 | * |
||
| 2740 | * @ignore |
||
| 2741 | * @param string $oid |
||
| 2742 | * @param string $property |
||
| 2743 | * @param mixed $value |
||
| 2744 | */ |
||
| 2745 | 3 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2749 | |||
| 2750 | /** |
||
| 2751 | * Gets the identifier of a document. |
||
| 2752 | * |
||
| 2753 | * @param object $document |
||
| 2754 | * @return mixed The identifier value |
||
| 2755 | */ |
||
| 2756 | 362 | public function getDocumentIdentifier($document) |
|
| 2763 | |||
| 2764 | /** |
||
| 2765 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2766 | * |
||
| 2767 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2768 | */ |
||
| 2769 | public function hasPendingInsertions() |
||
| 2773 | |||
| 2774 | /** |
||
| 2775 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2776 | * number of documents in the identity map. |
||
| 2777 | * |
||
| 2778 | * @return integer |
||
| 2779 | */ |
||
| 2780 | 2 | public function size() |
|
| 2788 | |||
| 2789 | /** |
||
| 2790 | * INTERNAL: |
||
| 2791 | * Registers a document as managed. |
||
| 2792 | * |
||
| 2793 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2794 | * document class. If the class expects its database identifier to be a |
||
| 2795 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2796 | * document identifiers map will become inconsistent with the identity map. |
||
| 2797 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2798 | * conversion and throw an exception if it's inconsistent. |
||
| 2799 | * |
||
| 2800 | * @param object $document The document. |
||
| 2801 | * @param array $id The identifier values. |
||
| 2802 | * @param array $data The original document data. |
||
| 2803 | */ |
||
| 2804 | 380 | public function registerManaged($document, $id, array $data) |
|
| 2819 | |||
| 2820 | /** |
||
| 2821 | * INTERNAL: |
||
| 2822 | * Clears the property changeset of the document with the given OID. |
||
| 2823 | * |
||
| 2824 | * @param string $oid The document's OID. |
||
| 2825 | */ |
||
| 2826 | 1 | public function clearDocumentChangeSet($oid) |
|
| 2830 | |||
| 2831 | /* PropertyChangedListener implementation */ |
||
| 2832 | |||
| 2833 | /** |
||
| 2834 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2835 | * |
||
| 2836 | * @param object $document The document that owns the property. |
||
| 2837 | * @param string $propertyName The name of the property that changed. |
||
| 2838 | * @param mixed $oldValue The old value of the property. |
||
| 2839 | * @param mixed $newValue The new value of the property. |
||
| 2840 | */ |
||
| 2841 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2856 | |||
| 2857 | /** |
||
| 2858 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2859 | * |
||
| 2860 | * @return array |
||
| 2861 | */ |
||
| 2862 | 5 | public function getScheduledDocumentInsertions() |
|
| 2866 | |||
| 2867 | /** |
||
| 2868 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2869 | * |
||
| 2870 | * @return array |
||
| 2871 | */ |
||
| 2872 | 3 | public function getScheduledDocumentUpserts() |
|
| 2876 | |||
| 2877 | /** |
||
| 2878 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2879 | * |
||
| 2880 | * @return array |
||
| 2881 | */ |
||
| 2882 | 3 | public function getScheduledDocumentUpdates() |
|
| 2886 | |||
| 2887 | /** |
||
| 2888 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2889 | * |
||
| 2890 | * @return array |
||
| 2891 | */ |
||
| 2892 | public function getScheduledDocumentDeletions() |
||
| 2896 | |||
| 2897 | /** |
||
| 2898 | * Get the currently scheduled complete collection deletions |
||
| 2899 | * |
||
| 2900 | * @return array |
||
| 2901 | */ |
||
| 2902 | public function getScheduledCollectionDeletions() |
||
| 2906 | |||
| 2907 | /** |
||
| 2908 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2909 | * |
||
| 2910 | * @return array |
||
| 2911 | */ |
||
| 2912 | public function getScheduledCollectionUpdates() |
||
| 2916 | |||
| 2917 | /** |
||
| 2918 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2919 | * |
||
| 2920 | * @param object |
||
| 2921 | * @return void |
||
| 2922 | */ |
||
| 2923 | public function initializeObject($obj) |
||
| 2931 | |||
| 2932 | 1 | private function objToStr($obj) |
|
| 2936 | } |
||
| 2937 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.