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 |
||
| 45 | class UnitOfWork implements PropertyChangedListener |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 49 | */ |
||
| 50 | public const STATE_MANAGED = 1; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 54 | * and is not (yet) managed by a DocumentManager. |
||
| 55 | */ |
||
| 56 | public const STATE_NEW = 2; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * A detached document is an instance with a persistent identity that is not |
||
| 60 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 61 | */ |
||
| 62 | public const STATE_DETACHED = 3; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * A removed document instance is an instance with a persistent identity, |
||
| 66 | * associated with a DocumentManager, whose persistent state has been |
||
| 67 | * deleted (or is scheduled for deletion). |
||
| 68 | */ |
||
| 69 | public const STATE_REMOVED = 4; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The identity map holds references to all managed documents. |
||
| 73 | * |
||
| 74 | * Documents are grouped by their class name, and then indexed by the |
||
| 75 | * serialized string of their database identifier field or, if the class |
||
| 76 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 77 | * differentiation of values that may be equal (via type juggling) but not |
||
| 78 | * identical. |
||
| 79 | * |
||
| 80 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 81 | * we always take the root class name of the hierarchy. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $identityMap = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Map of all identifiers of managed documents. |
||
| 89 | * Keys are object ids (spl_object_hash). |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $documentIdentifiers = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Map of the original document data of managed documents. |
||
| 97 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 98 | * at commit time. |
||
| 99 | * |
||
| 100 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 101 | * A value will only really be copied if the value in the document is modified |
||
| 102 | * by the user. |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private $originalDocumentData = []; |
||
| 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 = []; |
||
| 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 = []; |
||
| 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 | */ |
||
| 133 | private $scheduledForSynchronization = []; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * A list of all pending document insertions. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private $documentInsertions = []; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * A list of all pending document updates. |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | private $documentUpdates = []; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * A list of all pending document upserts. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | private $documentUpserts = []; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * A list of all pending document deletions. |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | private $documentDeletions = []; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * All pending collection deletions. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | private $collectionDeletions = []; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * All pending collection updates. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | private $collectionUpdates = []; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * A list of documents related to collections scheduled for update or deletion |
||
| 179 | * |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | private $hasScheduledCollections = []; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 186 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 187 | * of their data. |
||
| 188 | * |
||
| 189 | * @var array |
||
| 190 | */ |
||
| 191 | private $visitedCollections = []; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 195 | * |
||
| 196 | * @var DocumentManager |
||
| 197 | */ |
||
| 198 | private $dm; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * The EventManager used for dispatching events. |
||
| 202 | * |
||
| 203 | * @var EventManager |
||
| 204 | */ |
||
| 205 | private $evm; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Additional documents that are scheduled for removal. |
||
| 209 | * |
||
| 210 | * @var array |
||
| 211 | */ |
||
| 212 | private $orphanRemovals = []; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 216 | * |
||
| 217 | * @var HydratorFactory |
||
| 218 | */ |
||
| 219 | private $hydratorFactory; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * The document persister instances used to persist document instances. |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | private $persisters = []; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * The collection persister instance used to persist changes to collections. |
||
| 230 | * |
||
| 231 | * @var Persisters\CollectionPersister |
||
| 232 | */ |
||
| 233 | private $collectionPersister; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * The persistence builder instance used in DocumentPersisters. |
||
| 237 | * |
||
| 238 | * @var PersistenceBuilder|null |
||
| 239 | */ |
||
| 240 | private $persistenceBuilder; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Array of parent associations between embedded documents. |
||
| 244 | * |
||
| 245 | * @var array |
||
| 246 | */ |
||
| 247 | private $parentAssociations = []; |
||
| 248 | |||
| 249 | /** @var LifecycleEventManager */ |
||
| 250 | private $lifecycleEventManager; |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Array of embedded documents known to UnitOfWork. We need to hold them to prevent spl_object_hash |
||
| 254 | * collisions in case already managed object is lost due to GC (so now it won't). Embedded documents |
||
| 255 | * found during doDetach are removed from the registry, to empty it altogether clear() can be utilized. |
||
| 256 | * |
||
| 257 | * @var array |
||
| 258 | */ |
||
| 259 | private $embeddedDocumentsRegistry = []; |
||
| 260 | |||
| 261 | /** @var int */ |
||
| 262 | private $commitsInProgress = 0; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 266 | */ |
||
| 267 | 1651 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 268 | { |
||
| 269 | 1651 | $this->dm = $dm; |
|
| 270 | 1651 | $this->evm = $evm; |
|
| 271 | 1651 | $this->hydratorFactory = $hydratorFactory; |
|
| 272 | 1651 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 273 | 1651 | } |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 277 | * queries for insert persistence. |
||
| 278 | */ |
||
| 279 | 1130 | public function getPersistenceBuilder() : PersistenceBuilder |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Sets the parent association for a given embedded document. |
||
| 289 | */ |
||
| 290 | 209 | public function setParentAssociation(object $document, array $mapping, ?object $parent, string $propertyPath) : void |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Gets the parent association for a given embedded document. |
||
| 299 | * |
||
| 300 | * <code> |
||
| 301 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 302 | * </code> |
||
| 303 | */ |
||
| 304 | 233 | public function getParentAssociation(object $document) : ?array |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get the document persister instance for the given document name |
||
| 313 | */ |
||
| 314 | 1128 | public function getDocumentPersister(string $documentName) : Persisters\DocumentPersister |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Get the collection persister instance. |
||
| 326 | */ |
||
| 327 | 1128 | public function getCollectionPersister() : CollectionPersister |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Set the document persister instance to use for the given document name |
||
| 338 | */ |
||
| 339 | 13 | public function setDocumentPersister(string $documentName, Persisters\DocumentPersister $persister) : void |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 346 | * up to this point. The state of all managed documents will be synchronized with |
||
| 347 | * the database. |
||
| 348 | * |
||
| 349 | * The operations are executed in the following order: |
||
| 350 | * |
||
| 351 | * 1) All document insertions |
||
| 352 | * 2) All document updates |
||
| 353 | * 3) All document deletions |
||
| 354 | * |
||
| 355 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 356 | */ |
||
| 357 | 611 | public function commit(array $options = []) : void |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Groups a list of scheduled documents by their class. |
||
| 438 | */ |
||
| 439 | 606 | private function getClassesForCommitAction(array $documents, bool $includeEmbedded = false) : array |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Compute changesets of all documents scheduled for insertion. |
||
| 471 | * |
||
| 472 | * Embedded documents will not be processed. |
||
| 473 | */ |
||
| 474 | 616 | private function computeScheduleInsertsChangeSets() : void |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Compute changesets of all documents scheduled for upsert. |
||
| 488 | * |
||
| 489 | * Embedded documents will not be processed. |
||
| 490 | */ |
||
| 491 | 615 | private function computeScheduleUpsertsChangeSets() : void |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Gets the changeset for a document. |
||
| 505 | * |
||
| 506 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 507 | */ |
||
| 508 | 611 | public function getDocumentChangeSet(object $document) : array |
|
| 514 | |||
| 515 | /** |
||
| 516 | * INTERNAL: |
||
| 517 | * Sets the changeset for a document. |
||
| 518 | */ |
||
| 519 | 1 | public function setDocumentChangeSet(object $document, array $changeset) : void |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | 616 | public function getDocumentActualData(object $document) : array |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Computes the changes that happened to a single document. |
||
| 562 | * |
||
| 563 | * Modifies/populates the following properties: |
||
| 564 | * |
||
| 565 | * {@link originalDocumentData} |
||
| 566 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 567 | * then it was not fetched from the database and therefore we have no original |
||
| 568 | * document data yet. All of the current document data is stored as the original document data. |
||
| 569 | * |
||
| 570 | * {@link documentChangeSets} |
||
| 571 | * The changes detected on all properties of the document are stored there. |
||
| 572 | * A change is a tuple array where the first entry is the old value and the second |
||
| 573 | * entry is the new value of the property. Changesets are used by persisters |
||
| 574 | * to INSERT/UPDATE the persistent document state. |
||
| 575 | * |
||
| 576 | * {@link documentUpdates} |
||
| 577 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 578 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 579 | * there to mark it for an update. |
||
| 580 | */ |
||
| 581 | 612 | public function computeChangeSet(ClassMetadata $class, object $document) : void |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 597 | */ |
||
| 598 | 612 | private function computeOrRecomputeChangeSet(ClassMetadata $class, object $document, bool $recompute = false) : void |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Computes all the changes that have been done to documents and collections |
||
| 791 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 792 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 793 | */ |
||
| 794 | 616 | public function computeChangeSets() : void |
|
| 845 | |||
| 846 | /** |
||
| 847 | * Computes the changes of an association. |
||
| 848 | * |
||
| 849 | * @param mixed $value The value of the association. |
||
| 850 | * |
||
| 851 | * @throws InvalidArgumentException |
||
| 852 | */ |
||
| 853 | 452 | private function computeAssociationChanges(object $parentDocument, array $assoc, $value) : void |
|
| 854 | { |
||
| 855 | 452 | $isNewParentDocument = isset($this->documentInsertions[spl_object_hash($parentDocument)]); |
|
| 856 | 452 | $class = $this->dm->getClassMetadata(get_class($parentDocument)); |
|
| 857 | 452 | $topOrExistingDocument = ( ! $isNewParentDocument || ! $class->isEmbeddedDocument); |
|
| 858 | |||
| 859 | 452 | if ($value instanceof GhostObjectInterface && ! $value->isProxyInitialized()) { |
|
| 860 | 7 | return; |
|
| 861 | } |
||
| 862 | |||
| 863 | 451 | if ($value instanceof PersistentCollectionInterface && $value->isDirty() && $value->getOwner() !== null && ($assoc['isOwningSide'] || isset($assoc['embedded']))) { |
|
| 864 | 258 | if ($topOrExistingDocument || CollectionHelper::usesSet($assoc['strategy'])) { |
|
| 865 | 254 | $this->scheduleCollectionUpdate($value); |
|
| 866 | } |
||
| 867 | |||
| 868 | 258 | $topmostOwner = $this->getOwningDocument($value->getOwner()); |
|
| 869 | 258 | $this->visitedCollections[spl_object_hash($topmostOwner)][] = $value; |
|
| 870 | 258 | if (! empty($assoc['orphanRemoval']) || isset($assoc['embedded'])) { |
|
| 871 | 151 | $value->initialize(); |
|
| 872 | 151 | foreach ($value->getDeletedDocuments() as $orphan) { |
|
| 873 | 25 | $this->scheduleOrphanRemoval($orphan); |
|
| 874 | } |
||
| 875 | } |
||
| 876 | } |
||
| 877 | |||
| 878 | // Look through the documents, and in any of their associations, |
||
| 879 | // for transient (new) documents, recursively. ("Persistence by reachability") |
||
| 880 | // Unwrap. Uninitialized collections will simply be empty. |
||
| 881 | 451 | $unwrappedValue = $assoc['type'] === ClassMetadata::ONE ? [$value] : $value->unwrap(); |
|
| 882 | |||
| 883 | 451 | $count = 0; |
|
| 884 | 451 | foreach ($unwrappedValue as $key => $entry) { |
|
| 885 | 367 | if (! is_object($entry)) { |
|
| 886 | 1 | throw new InvalidArgumentException( |
|
| 887 | 1 | sprintf('Expected object, found "%s" in %s::%s', $entry, get_class($parentDocument), $assoc['name']) |
|
| 888 | ); |
||
| 889 | } |
||
| 890 | |||
| 891 | 366 | $targetClass = $this->dm->getClassMetadata(get_class($entry)); |
|
| 892 | |||
| 893 | 366 | $state = $this->getDocumentState($entry, self::STATE_NEW); |
|
| 894 | |||
| 895 | // Handle "set" strategy for multi-level hierarchy |
||
| 896 | 366 | $pathKey = ! isset($assoc['strategy']) || CollectionHelper::isList($assoc['strategy']) ? $count : $key; |
|
| 897 | 366 | $path = $assoc['type'] === 'many' ? $assoc['name'] . '.' . $pathKey : $assoc['name']; |
|
| 898 | |||
| 899 | 366 | $count++; |
|
| 900 | |||
| 901 | switch ($state) { |
||
| 902 | 366 | case self::STATE_NEW: |
|
| 903 | 70 | if (! $assoc['isCascadePersist']) { |
|
| 904 | throw new InvalidArgumentException('A new document was found through a relationship that was not' |
||
| 905 | . ' configured to cascade persist operations: ' . $this->objToStr($entry) . '.' |
||
| 906 | . ' Explicitly persist the new document or configure cascading persist operations' |
||
| 907 | . ' on the relationship.'); |
||
| 908 | } |
||
| 909 | |||
| 910 | 70 | $this->persistNew($targetClass, $entry); |
|
| 911 | 70 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 912 | 70 | $this->computeChangeSet($targetClass, $entry); |
|
| 913 | 70 | break; |
|
| 914 | |||
| 915 | 362 | case self::STATE_MANAGED: |
|
| 916 | 362 | if ($targetClass->isEmbeddedDocument) { |
|
| 917 | 179 | [, $knownParent ] = $this->getParentAssociation($entry); |
|
| 918 | 179 | if ($knownParent && $knownParent !== $parentDocument) { |
|
| 919 | 6 | $entry = clone $entry; |
|
| 920 | 6 | if ($assoc['type'] === ClassMetadata::ONE) { |
|
| 921 | 3 | $class->setFieldValue($parentDocument, $assoc['fieldName'], $entry); |
|
| 922 | 3 | $this->setOriginalDocumentProperty(spl_object_hash($parentDocument), $assoc['fieldName'], $entry); |
|
| 923 | 3 | $poid = spl_object_hash($parentDocument); |
|
| 924 | 3 | if (isset($this->documentChangeSets[$poid][$assoc['fieldName']])) { |
|
| 925 | 3 | $this->documentChangeSets[$poid][$assoc['fieldName']][1] = $entry; |
|
| 926 | } |
||
| 927 | } else { |
||
| 928 | // must use unwrapped value to not trigger orphan removal |
||
| 929 | 4 | $unwrappedValue[$key] = $entry; |
|
| 930 | } |
||
| 931 | 6 | $this->persistNew($targetClass, $entry); |
|
| 932 | } |
||
| 933 | 179 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 934 | 179 | $this->computeChangeSet($targetClass, $entry); |
|
| 935 | } |
||
| 936 | 362 | break; |
|
| 937 | |||
| 938 | 1 | case self::STATE_REMOVED: |
|
| 939 | // Consume the $value as array (it's either an array or an ArrayAccess) |
||
| 940 | // and remove the element from Collection. |
||
| 941 | 1 | if ($assoc['type'] === ClassMetadata::MANY) { |
|
| 942 | unset($value[$key]); |
||
| 943 | } |
||
| 944 | 1 | break; |
|
| 945 | |||
| 946 | case self::STATE_DETACHED: |
||
| 947 | // Can actually not happen right now as we assume STATE_NEW, |
||
| 948 | // so the exception will be raised from the DBAL layer (constraint violation). |
||
| 949 | throw new InvalidArgumentException('A detached document was found through a ' |
||
| 950 | . 'relationship during cascading a persist operation.'); |
||
| 951 | |||
| 952 | default: |
||
| 953 | // MANAGED associated documents are already taken into account |
||
| 954 | // during changeset calculation anyway, since they are in the identity map. |
||
| 955 | } |
||
| 956 | } |
||
| 957 | 450 | } |
|
| 958 | |||
| 959 | /** |
||
| 960 | * INTERNAL: |
||
| 961 | * Computes the changeset of an individual document, independently of the |
||
| 962 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 963 | * |
||
| 964 | * The passed document must be a managed document. If the document already has a change set |
||
| 965 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 966 | * whereby changes detected in this method prevail. |
||
| 967 | * |
||
| 968 | * @throws InvalidArgumentException If the passed document is not MANAGED. |
||
| 969 | * |
||
| 970 | * @ignore |
||
| 971 | */ |
||
| 972 | 19 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, object $document) : void |
|
| 973 | { |
||
| 974 | // Ignore uninitialized proxy objects |
||
| 975 | 19 | if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { |
|
| 976 | 1 | return; |
|
| 977 | } |
||
| 978 | |||
| 979 | 18 | $oid = spl_object_hash($document); |
|
| 980 | |||
| 981 | 18 | if (! isset($this->documentStates[$oid]) || $this->documentStates[$oid] !== self::STATE_MANAGED) { |
|
| 982 | throw new InvalidArgumentException('Document must be managed.'); |
||
| 983 | } |
||
| 984 | |||
| 985 | 18 | if (! $class->isInheritanceTypeNone()) { |
|
| 986 | 2 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 987 | } |
||
| 988 | |||
| 989 | 18 | $this->computeOrRecomputeChangeSet($class, $document, true); |
|
| 990 | 18 | } |
|
| 991 | |||
| 992 | /** |
||
| 993 | * @throws InvalidArgumentException If there is something wrong with document's identifier. |
||
| 994 | */ |
||
| 995 | 641 | private function persistNew(ClassMetadata $class, object $document) : void |
|
| 996 | { |
||
| 997 | 641 | $this->lifecycleEventManager->prePersist($class, $document); |
|
| 998 | 641 | $oid = spl_object_hash($document); |
|
| 999 | 641 | $upsert = false; |
|
| 1000 | 641 | if ($class->identifier) { |
|
| 1001 | 641 | $idValue = $class->getIdentifierValue($document); |
|
| 1002 | 641 | $upsert = ! $class->isEmbeddedDocument && $idValue !== null; |
|
| 1003 | |||
| 1004 | 641 | if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) { |
|
| 1005 | 3 | throw new InvalidArgumentException(sprintf( |
|
| 1006 | 3 | '%s uses NONE identifier generation strategy but no identifier was provided when persisting.', |
|
| 1007 | 3 | get_class($document) |
|
| 1008 | )); |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | 640 | if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_AUTO && $idValue !== null && ! preg_match('#^[0-9a-f]{24}$#', (string) $idValue)) { |
|
| 1012 | 1 | throw new InvalidArgumentException(sprintf( |
|
| 1013 | 1 | '%s uses AUTO identifier generation strategy but provided identifier is not a valid ObjectId.', |
|
| 1014 | 1 | get_class($document) |
|
| 1015 | )); |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | 639 | if ($class->generatorType !== ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null && $class->idGenerator !== null) { |
|
| 1019 | 560 | $idValue = $class->idGenerator->generate($this->dm, $document); |
|
| 1020 | 560 | $idValue = $class->getPHPIdentifierValue($class->getDatabaseIdentifierValue($idValue)); |
|
| 1021 | 560 | $class->setIdentifierValue($document, $idValue); |
|
| 1022 | } |
||
| 1023 | |||
| 1024 | 639 | $this->documentIdentifiers[$oid] = $idValue; |
|
| 1025 | } else { |
||
| 1026 | // this is for embedded documents without identifiers |
||
| 1027 | 152 | $this->documentIdentifiers[$oid] = $oid; |
|
| 1028 | } |
||
| 1029 | |||
| 1030 | 639 | $this->documentStates[$oid] = self::STATE_MANAGED; |
|
| 1031 | |||
| 1032 | 639 | if ($upsert) { |
|
| 1033 | 89 | $this->scheduleForUpsert($class, $document); |
|
| 1034 | } else { |
||
| 1035 | 569 | $this->scheduleForInsert($class, $document); |
|
| 1036 | } |
||
| 1037 | 639 | } |
|
| 1038 | |||
| 1039 | /** |
||
| 1040 | * Executes all document insertions for documents of the specified type. |
||
| 1041 | */ |
||
| 1042 | 530 | private function executeInserts(ClassMetadata $class, array $documents, array $options = []) : void |
|
| 1043 | { |
||
| 1044 | 530 | $persister = $this->getDocumentPersister($class->name); |
|
| 1045 | |||
| 1046 | 530 | foreach ($documents as $oid => $document) { |
|
| 1047 | 530 | $persister->addInsert($document); |
|
| 1048 | 530 | unset($this->documentInsertions[$oid]); |
|
| 1049 | } |
||
| 1050 | |||
| 1051 | 530 | $persister->executeInserts($options); |
|
| 1052 | |||
| 1053 | 519 | foreach ($documents as $document) { |
|
| 1054 | 519 | $this->lifecycleEventManager->postPersist($class, $document); |
|
| 1055 | } |
||
| 1056 | 519 | } |
|
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Executes all document upserts for documents of the specified type. |
||
| 1060 | */ |
||
| 1061 | 86 | private function executeUpserts(ClassMetadata $class, array $documents, array $options = []) : void |
|
| 1062 | { |
||
| 1063 | 86 | $persister = $this->getDocumentPersister($class->name); |
|
| 1064 | |||
| 1065 | 86 | foreach ($documents as $oid => $document) { |
|
| 1066 | 86 | $persister->addUpsert($document); |
|
| 1067 | 86 | unset($this->documentUpserts[$oid]); |
|
| 1068 | } |
||
| 1069 | |||
| 1070 | 86 | $persister->executeUpserts($options); |
|
| 1071 | |||
| 1072 | 86 | foreach ($documents as $document) { |
|
| 1073 | 86 | $this->lifecycleEventManager->postPersist($class, $document); |
|
| 1074 | } |
||
| 1075 | 86 | } |
|
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Executes all document updates for documents of the specified type. |
||
| 1079 | */ |
||
| 1080 | 236 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = []) : void |
|
| 1081 | { |
||
| 1082 | 236 | if ($class->isReadOnly) { |
|
| 1083 | return; |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | 236 | $className = $class->name; |
|
| 1087 | 236 | $persister = $this->getDocumentPersister($className); |
|
| 1088 | |||
| 1089 | 236 | foreach ($documents as $oid => $document) { |
|
| 1090 | 236 | $this->lifecycleEventManager->preUpdate($class, $document); |
|
| 1091 | |||
| 1092 | 236 | if (! empty($this->documentChangeSets[$oid]) || $this->hasScheduledCollections($document)) { |
|
| 1093 | 235 | $persister->update($document, $options); |
|
| 1094 | } |
||
| 1095 | |||
| 1096 | 229 | unset($this->documentUpdates[$oid]); |
|
| 1097 | |||
| 1098 | 229 | $this->lifecycleEventManager->postUpdate($class, $document); |
|
| 1099 | } |
||
| 1100 | 228 | } |
|
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Executes all document deletions for documents of the specified type. |
||
| 1104 | */ |
||
| 1105 | 79 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = []) : void |
|
| 1106 | { |
||
| 1107 | 79 | $persister = $this->getDocumentPersister($class->name); |
|
| 1108 | |||
| 1109 | 79 | foreach ($documents as $oid => $document) { |
|
| 1110 | 79 | if (! $class->isEmbeddedDocument) { |
|
| 1111 | 36 | $persister->delete($document, $options); |
|
| 1112 | } |
||
| 1113 | unset( |
||
| 1114 | 77 | $this->documentDeletions[$oid], |
|
| 1115 | 77 | $this->documentIdentifiers[$oid], |
|
| 1116 | 77 | $this->originalDocumentData[$oid] |
|
| 1117 | ); |
||
| 1118 | |||
| 1119 | // Clear snapshot information for any referenced PersistentCollection |
||
| 1120 | // http://www.doctrine-project.org/jira/browse/MODM-95 |
||
| 1121 | 77 | foreach ($class->associationMappings as $fieldMapping) { |
|
| 1122 | 53 | if (! isset($fieldMapping['type']) || $fieldMapping['type'] !== ClassMetadata::MANY) { |
|
| 1123 | 38 | continue; |
|
| 1124 | } |
||
| 1125 | |||
| 1126 | 33 | $value = $class->reflFields[$fieldMapping['fieldName']]->getValue($document); |
|
| 1127 | 33 | if (! ($value instanceof PersistentCollectionInterface)) { |
|
| 1128 | 7 | continue; |
|
| 1129 | } |
||
| 1130 | |||
| 1131 | 29 | $value->clearSnapshot(); |
|
| 1132 | } |
||
| 1133 | |||
| 1134 | // Document with this $oid after deletion treated as NEW, even if the $oid |
||
| 1135 | // is obtained by a new document because the old one went out of scope. |
||
| 1136 | 77 | $this->documentStates[$oid] = self::STATE_NEW; |
|
| 1137 | |||
| 1138 | 77 | $this->lifecycleEventManager->postRemove($class, $document); |
|
| 1139 | } |
||
| 1140 | 77 | } |
|
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Schedules a document for insertion into the database. |
||
| 1144 | * If the document already has an identifier, it will be added to the |
||
| 1145 | * identity map. |
||
| 1146 | * |
||
| 1147 | * @throws InvalidArgumentException |
||
| 1148 | */ |
||
| 1149 | 572 | public function scheduleForInsert(ClassMetadata $class, object $document) : void |
|
| 1150 | { |
||
| 1151 | 572 | $oid = spl_object_hash($document); |
|
| 1152 | |||
| 1153 | 572 | if (isset($this->documentUpdates[$oid])) { |
|
| 1154 | throw new InvalidArgumentException('Dirty document can not be scheduled for insertion.'); |
||
| 1155 | } |
||
| 1156 | 572 | if (isset($this->documentDeletions[$oid])) { |
|
| 1157 | throw new InvalidArgumentException('Removed document can not be scheduled for insertion.'); |
||
| 1158 | } |
||
| 1159 | 572 | if (isset($this->documentInsertions[$oid])) { |
|
| 1160 | throw new InvalidArgumentException('Document can not be scheduled for insertion twice.'); |
||
| 1161 | } |
||
| 1162 | |||
| 1163 | 572 | $this->documentInsertions[$oid] = $document; |
|
| 1164 | |||
| 1165 | 572 | if (! isset($this->documentIdentifiers[$oid])) { |
|
| 1166 | 3 | return; |
|
| 1167 | } |
||
| 1168 | |||
| 1169 | 569 | $this->addToIdentityMap($document); |
|
| 1170 | 569 | } |
|
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Schedules a document for upsert into the database and adds it to the |
||
| 1174 | * identity map |
||
| 1175 | * |
||
| 1176 | * @throws InvalidArgumentException |
||
| 1177 | */ |
||
| 1178 | 92 | public function scheduleForUpsert(ClassMetadata $class, object $document) : void |
|
| 1179 | { |
||
| 1180 | 92 | $oid = spl_object_hash($document); |
|
| 1181 | |||
| 1182 | 92 | if ($class->isEmbeddedDocument) { |
|
| 1183 | throw new InvalidArgumentException('Embedded document can not be scheduled for upsert.'); |
||
| 1184 | } |
||
| 1185 | 92 | if (isset($this->documentUpdates[$oid])) { |
|
| 1186 | throw new InvalidArgumentException('Dirty document can not be scheduled for upsert.'); |
||
| 1187 | } |
||
| 1188 | 92 | if (isset($this->documentDeletions[$oid])) { |
|
| 1189 | throw new InvalidArgumentException('Removed document can not be scheduled for upsert.'); |
||
| 1190 | } |
||
| 1191 | 92 | if (isset($this->documentUpserts[$oid])) { |
|
| 1192 | throw new InvalidArgumentException('Document can not be scheduled for upsert twice.'); |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | 92 | $this->documentUpserts[$oid] = $document; |
|
| 1196 | 92 | $this->documentIdentifiers[$oid] = $class->getIdentifierValue($document); |
|
| 1197 | 92 | $this->addToIdentityMap($document); |
|
| 1198 | 92 | } |
|
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Checks whether a document is scheduled for insertion. |
||
| 1202 | */ |
||
| 1203 | 110 | public function isScheduledForInsert(object $document) : bool |
|
| 1204 | { |
||
| 1205 | 110 | return isset($this->documentInsertions[spl_object_hash($document)]); |
|
| 1206 | } |
||
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Checks whether a document is scheduled for upsert. |
||
| 1210 | */ |
||
| 1211 | 5 | public function isScheduledForUpsert(object $document) : bool |
|
| 1212 | { |
||
| 1213 | 5 | return isset($this->documentUpserts[spl_object_hash($document)]); |
|
| 1214 | } |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Schedules a document for being updated. |
||
| 1218 | * |
||
| 1219 | * @throws InvalidArgumentException |
||
| 1220 | */ |
||
| 1221 | 245 | public function scheduleForUpdate(object $document) : void |
|
| 1222 | { |
||
| 1223 | 245 | $oid = spl_object_hash($document); |
|
| 1224 | 245 | if (! isset($this->documentIdentifiers[$oid])) { |
|
| 1225 | throw new InvalidArgumentException('Document has no identity.'); |
||
| 1226 | } |
||
| 1227 | |||
| 1228 | 245 | if (isset($this->documentDeletions[$oid])) { |
|
| 1229 | throw new InvalidArgumentException('Document is removed.'); |
||
| 1230 | } |
||
| 1231 | |||
| 1232 | 245 | if (isset($this->documentUpdates[$oid]) |
|
| 1233 | 245 | || isset($this->documentInsertions[$oid]) |
|
| 1234 | 245 | || isset($this->documentUpserts[$oid])) { |
|
| 1235 | 104 | return; |
|
| 1236 | } |
||
| 1237 | |||
| 1238 | 243 | $this->documentUpdates[$oid] = $document; |
|
| 1239 | 243 | } |
|
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1243 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1244 | * at commit time. |
||
| 1245 | */ |
||
| 1246 | 21 | public function isScheduledForUpdate(object $document) : bool |
|
| 1247 | { |
||
| 1248 | 21 | return isset($this->documentUpdates[spl_object_hash($document)]); |
|
| 1249 | } |
||
| 1250 | |||
| 1251 | /** |
||
| 1252 | * Checks whether a document is registered to be checked in the unit of work. |
||
| 1253 | */ |
||
| 1254 | 1 | public function isScheduledForSynchronization(object $document) : bool |
|
| 1255 | { |
||
| 1256 | 1 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1257 | 1 | return isset($this->scheduledForSynchronization[$class->name][spl_object_hash($document)]); |
|
| 1258 | } |
||
| 1259 | |||
| 1260 | /** |
||
| 1261 | * INTERNAL: |
||
| 1262 | * Schedules a document for deletion. |
||
| 1263 | */ |
||
| 1264 | 84 | public function scheduleForDelete(object $document) : void |
|
| 1265 | { |
||
| 1266 | 84 | $oid = spl_object_hash($document); |
|
| 1267 | |||
| 1268 | 84 | if (isset($this->documentInsertions[$oid])) { |
|
| 1269 | 2 | if ($this->isInIdentityMap($document)) { |
|
| 1270 | 2 | $this->removeFromIdentityMap($document); |
|
| 1271 | } |
||
| 1272 | 2 | unset($this->documentInsertions[$oid]); |
|
| 1273 | 2 | return; // document has not been persisted yet, so nothing more to do. |
|
| 1274 | } |
||
| 1275 | |||
| 1276 | 83 | if (! $this->isInIdentityMap($document)) { |
|
| 1277 | 2 | return; // ignore |
|
| 1278 | } |
||
| 1279 | |||
| 1280 | 82 | $this->removeFromIdentityMap($document); |
|
| 1281 | 82 | $this->documentStates[$oid] = self::STATE_REMOVED; |
|
| 1282 | |||
| 1283 | 82 | if (isset($this->documentUpdates[$oid])) { |
|
| 1284 | unset($this->documentUpdates[$oid]); |
||
| 1285 | } |
||
| 1286 | 82 | if (isset($this->documentDeletions[$oid])) { |
|
| 1287 | return; |
||
| 1288 | } |
||
| 1289 | |||
| 1290 | 82 | $this->documentDeletions[$oid] = $document; |
|
| 1291 | 82 | } |
|
| 1292 | |||
| 1293 | /** |
||
| 1294 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1295 | * of work. |
||
| 1296 | */ |
||
| 1297 | 5 | public function isScheduledForDelete(object $document) : bool |
|
| 1298 | { |
||
| 1299 | 5 | return isset($this->documentDeletions[spl_object_hash($document)]); |
|
| 1300 | } |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1304 | */ |
||
| 1305 | 257 | public function isDocumentScheduled(object $document) : bool |
|
| 1306 | { |
||
| 1307 | 257 | $oid = spl_object_hash($document); |
|
| 1308 | 257 | return isset($this->documentInsertions[$oid]) || |
|
| 1309 | 139 | isset($this->documentUpserts[$oid]) || |
|
| 1310 | 129 | isset($this->documentUpdates[$oid]) || |
|
| 1311 | 257 | isset($this->documentDeletions[$oid]); |
|
| 1312 | } |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * INTERNAL: |
||
| 1316 | * Registers a document in the identity map. |
||
| 1317 | * |
||
| 1318 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1319 | * the root document. Identifiers are serialized before being used as array |
||
| 1320 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1321 | * |
||
| 1322 | * @ignore |
||
| 1323 | */ |
||
| 1324 | 680 | public function addToIdentityMap(object $document) : bool |
|
| 1325 | { |
||
| 1326 | 680 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1327 | 680 | $id = $this->getIdForIdentityMap($document); |
|
| 1328 | |||
| 1329 | 680 | if (isset($this->identityMap[$class->name][$id])) { |
|
| 1330 | 44 | return false; |
|
| 1331 | } |
||
| 1332 | |||
| 1333 | 680 | $this->identityMap[$class->name][$id] = $document; |
|
| 1334 | |||
| 1335 | 680 | if ($document instanceof NotifyPropertyChanged && |
|
| 1336 | 680 | ( ! $document instanceof GhostObjectInterface || $document->isProxyInitialized())) { |
|
| 1337 | 3 | $document->addPropertyChangedListener($this); |
|
| 1338 | } |
||
| 1339 | |||
| 1340 | 680 | return true; |
|
| 1341 | } |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Gets the state of a document with regard to the current unit of work. |
||
| 1345 | * |
||
| 1346 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1347 | * This parameter can be set to improve performance of document state detection |
||
| 1348 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1349 | * is either known or does not matter for the caller of the method. |
||
| 1350 | */ |
||
| 1351 | 645 | public function getDocumentState(object $document, ?int $assume = null) : int |
|
| 1352 | { |
||
| 1353 | 645 | $oid = spl_object_hash($document); |
|
| 1354 | |||
| 1355 | 645 | if (isset($this->documentStates[$oid])) { |
|
| 1356 | 406 | return $this->documentStates[$oid]; |
|
| 1357 | } |
||
| 1358 | |||
| 1359 | 644 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1360 | |||
| 1361 | 644 | if ($class->isEmbeddedDocument) { |
|
| 1362 | 192 | return self::STATE_NEW; |
|
| 1363 | } |
||
| 1364 | |||
| 1365 | 641 | if ($assume !== null) { |
|
| 1366 | 639 | return $assume; |
|
| 1367 | } |
||
| 1368 | |||
| 1369 | /* State can only be NEW or DETACHED, because MANAGED/REMOVED states are |
||
| 1370 | * known. Note that you cannot remember the NEW or DETACHED state in |
||
| 1371 | * _documentStates since the UoW does not hold references to such |
||
| 1372 | * objects and the object hash can be reused. More generally, because |
||
| 1373 | * the state may "change" between NEW/DETACHED without the UoW being |
||
| 1374 | * aware of it. |
||
| 1375 | */ |
||
| 1376 | 3 | $id = $class->getIdentifierObject($document); |
|
| 1377 | |||
| 1378 | 3 | if ($id === null) { |
|
| 1379 | 2 | return self::STATE_NEW; |
|
| 1380 | } |
||
| 1381 | |||
| 1382 | // Check for a version field, if available, to avoid a DB lookup. |
||
| 1383 | 2 | if ($class->isVersioned && $class->versionField !== null) { |
|
| 1384 | return $class->getFieldValue($document, $class->versionField) |
||
| 1385 | ? self::STATE_DETACHED |
||
| 1386 | : self::STATE_NEW; |
||
| 1387 | } |
||
| 1388 | |||
| 1389 | // Last try before DB lookup: check the identity map. |
||
| 1390 | 2 | if ($this->tryGetById($id, $class)) { |
|
| 1391 | 1 | return self::STATE_DETACHED; |
|
| 1392 | } |
||
| 1393 | |||
| 1394 | // DB lookup |
||
| 1395 | 2 | if ($this->getDocumentPersister($class->name)->exists($document)) { |
|
| 1396 | 1 | return self::STATE_DETACHED; |
|
| 1397 | } |
||
| 1398 | |||
| 1399 | 1 | return self::STATE_NEW; |
|
| 1400 | } |
||
| 1401 | |||
| 1402 | /** |
||
| 1403 | * INTERNAL: |
||
| 1404 | * Removes a document from the identity map. This effectively detaches the |
||
| 1405 | * document from the persistence management of Doctrine. |
||
| 1406 | * |
||
| 1407 | * @throws InvalidArgumentException |
||
| 1408 | * |
||
| 1409 | * @ignore |
||
| 1410 | */ |
||
| 1411 | 96 | public function removeFromIdentityMap(object $document) : bool |
|
| 1412 | { |
||
| 1413 | 96 | $oid = spl_object_hash($document); |
|
| 1414 | |||
| 1415 | // Check if id is registered first |
||
| 1416 | 96 | if (! isset($this->documentIdentifiers[$oid])) { |
|
| 1417 | return false; |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | 96 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1421 | 96 | $id = $this->getIdForIdentityMap($document); |
|
| 1422 | |||
| 1423 | 96 | if (isset($this->identityMap[$class->name][$id])) { |
|
| 1424 | 96 | unset($this->identityMap[$class->name][$id]); |
|
| 1425 | 96 | $this->documentStates[$oid] = self::STATE_DETACHED; |
|
| 1426 | 96 | return true; |
|
| 1427 | } |
||
| 1428 | |||
| 1429 | return false; |
||
| 1430 | } |
||
| 1431 | |||
| 1432 | /** |
||
| 1433 | * INTERNAL: |
||
| 1434 | * Gets a document in the identity map by its identifier hash. |
||
| 1435 | * |
||
| 1436 | * @param mixed $id Document identifier |
||
| 1437 | * |
||
| 1438 | * @throws InvalidArgumentException If the class does not have an identifier. |
||
| 1439 | * |
||
| 1440 | * @ignore |
||
| 1441 | */ |
||
| 1442 | 37 | public function getById($id, ClassMetadata $class) : object |
|
| 1443 | { |
||
| 1444 | 37 | if (! $class->identifier) { |
|
| 1445 | throw new InvalidArgumentException(sprintf('Class "%s" does not have an identifier', $class->name)); |
||
| 1446 | } |
||
| 1447 | |||
| 1448 | 37 | $serializedId = serialize($class->getDatabaseIdentifierValue($id)); |
|
| 1449 | |||
| 1450 | 37 | return $this->identityMap[$class->name][$serializedId]; |
|
| 1451 | } |
||
| 1452 | |||
| 1453 | /** |
||
| 1454 | * INTERNAL: |
||
| 1455 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1456 | * for the given hash, FALSE is returned. |
||
| 1457 | * |
||
| 1458 | * @param mixed $id Document identifier |
||
| 1459 | * |
||
| 1460 | * @return mixed The found document or FALSE. |
||
| 1461 | * |
||
| 1462 | * @throws InvalidArgumentException If the class does not have an identifier. |
||
| 1463 | * |
||
| 1464 | * @ignore |
||
| 1465 | */ |
||
| 1466 | 306 | public function tryGetById($id, ClassMetadata $class) |
|
| 1467 | { |
||
| 1468 | 306 | if (! $class->identifier) { |
|
| 1469 | throw new InvalidArgumentException(sprintf('Class "%s" does not have an identifier', $class->name)); |
||
| 1470 | } |
||
| 1471 | |||
| 1472 | 306 | $serializedId = serialize($class->getDatabaseIdentifierValue($id)); |
|
| 1473 | |||
| 1474 | 306 | return $this->identityMap[$class->name][$serializedId] ?? false; |
|
| 1475 | } |
||
| 1476 | |||
| 1477 | /** |
||
| 1478 | * Schedules a document for dirty-checking at commit-time. |
||
| 1479 | */ |
||
| 1480 | 3 | public function scheduleForSynchronization(object $document) : void |
|
| 1481 | { |
||
| 1482 | 3 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1483 | 3 | $this->scheduledForSynchronization[$class->name][spl_object_hash($document)] = $document; |
|
| 1484 | 3 | } |
|
| 1485 | |||
| 1486 | /** |
||
| 1487 | * Checks whether a document is registered in the identity map. |
||
| 1488 | */ |
||
| 1489 | 92 | public function isInIdentityMap(object $document) : bool |
|
| 1502 | |||
| 1503 | 680 | private function getIdForIdentityMap(object $document) : string |
|
| 1516 | |||
| 1517 | /** |
||
| 1518 | * INTERNAL: |
||
| 1519 | * Checks whether an identifier exists in the identity map. |
||
| 1520 | * |
||
| 1521 | * @ignore |
||
| 1522 | */ |
||
| 1523 | public function containsId($id, string $rootClassName) : bool |
||
| 1527 | |||
| 1528 | /** |
||
| 1529 | * Persists a document as part of the current unit of work. |
||
| 1530 | * |
||
| 1531 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1532 | * @throws InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1533 | */ |
||
| 1534 | 642 | public function persist(object $document) : void |
|
| 1543 | |||
| 1544 | /** |
||
| 1545 | * Saves a document as part of the current unit of work. |
||
| 1546 | * This method is internally called during save() cascades as it tracks |
||
| 1547 | * the already visited documents to prevent infinite recursions. |
||
| 1548 | * |
||
| 1549 | * NOTE: This method always considers documents that are not yet known to |
||
| 1550 | * this UnitOfWork as NEW. |
||
| 1551 | * |
||
| 1552 | * @throws InvalidArgumentException |
||
| 1553 | * @throws MongoDBException |
||
| 1554 | */ |
||
| 1555 | 641 | private function doPersist(object $document, array &$visited) : void |
|
| 1600 | |||
| 1601 | /** |
||
| 1602 | * Deletes a document as part of the current unit of work. |
||
| 1603 | */ |
||
| 1604 | 83 | public function remove(object $document) |
|
| 1609 | |||
| 1610 | /** |
||
| 1611 | * Deletes a document as part of the current unit of work. |
||
| 1612 | * |
||
| 1613 | * This method is internally called during delete() cascades as it tracks |
||
| 1614 | * the already visited documents to prevent infinite recursions. |
||
| 1615 | * |
||
| 1616 | * @throws MongoDBException |
||
| 1617 | */ |
||
| 1618 | 83 | private function doRemove(object $document, array &$visited) : void |
|
| 1650 | |||
| 1651 | /** |
||
| 1652 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1653 | */ |
||
| 1654 | 11 | public function merge(object $document) : object |
|
| 1660 | |||
| 1661 | /** |
||
| 1662 | * Executes a merge operation on a document. |
||
| 1663 | * |
||
| 1664 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1665 | * @throws LockException If the document uses optimistic locking through a |
||
| 1666 | * version attribute and the version check against the |
||
| 1667 | * managed copy fails. |
||
| 1668 | */ |
||
| 1669 | 11 | private function doMerge(object $document, array &$visited, ?object $prevManagedCopy = null, ?array $assoc = null) : object |
|
| 1845 | |||
| 1846 | /** |
||
| 1847 | * Detaches a document from the persistence management. It's persistence will |
||
| 1848 | * no longer be managed by Doctrine. |
||
| 1849 | */ |
||
| 1850 | 11 | public function detach(object $document) : void |
|
| 1855 | |||
| 1856 | /** |
||
| 1857 | * Executes a detach operation on the given document. |
||
| 1858 | * |
||
| 1859 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1860 | */ |
||
| 1861 | 17 | private function doDetach(object $document, array &$visited) : void |
|
| 1893 | |||
| 1894 | /** |
||
| 1895 | * Refreshes the state of the given document from the database, overwriting |
||
| 1896 | * any local, unpersisted changes. |
||
| 1897 | * |
||
| 1898 | * @throws InvalidArgumentException If the document is not MANAGED. |
||
| 1899 | */ |
||
| 1900 | 24 | public function refresh(object $document) : void |
|
| 1905 | |||
| 1906 | /** |
||
| 1907 | * Executes a refresh operation on a document. |
||
| 1908 | * |
||
| 1909 | * @throws InvalidArgumentException If the document is not MANAGED. |
||
| 1910 | */ |
||
| 1911 | 24 | private function doRefresh(object $document, array &$visited) : void |
|
| 1932 | |||
| 1933 | /** |
||
| 1934 | * Cascades a refresh operation to associated documents. |
||
| 1935 | */ |
||
| 1936 | 23 | private function cascadeRefresh(object $document, array &$visited) : void |
|
| 1937 | { |
||
| 1938 | 23 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1939 | |||
| 1940 | 23 | $associationMappings = array_filter( |
|
| 1941 | 23 | $class->associationMappings, |
|
| 1942 | static function ($assoc) { |
||
| 1943 | 18 | return $assoc['isCascadeRefresh']; |
|
| 1944 | 23 | } |
|
| 1945 | ); |
||
| 1946 | |||
| 1947 | 23 | foreach ($associationMappings as $mapping) { |
|
| 1948 | 15 | $relatedDocuments = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 1949 | 15 | if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) { |
|
| 1950 | 15 | if ($relatedDocuments instanceof PersistentCollectionInterface) { |
|
| 1951 | // Unwrap so that foreach() does not initialize |
||
| 1952 | 15 | $relatedDocuments = $relatedDocuments->unwrap(); |
|
| 1953 | } |
||
| 1954 | 15 | foreach ($relatedDocuments as $relatedDocument) { |
|
| 1955 | $this->doRefresh($relatedDocument, $visited); |
||
| 1956 | } |
||
| 1957 | 10 | } elseif ($relatedDocuments !== null) { |
|
| 1958 | 2 | $this->doRefresh($relatedDocuments, $visited); |
|
| 1959 | } |
||
| 1960 | } |
||
| 1961 | 23 | } |
|
| 1962 | |||
| 1963 | /** |
||
| 1964 | * Cascades a detach operation to associated documents. |
||
| 1965 | */ |
||
| 1966 | 17 | private function cascadeDetach(object $document, array &$visited) : void |
|
| 1987 | /** |
||
| 1988 | * Cascades a merge operation to associated documents. |
||
| 1989 | */ |
||
| 1990 | 11 | private function cascadeMerge(object $document, object $managedCopy, array &$visited) : void |
|
| 2018 | |||
| 2019 | /** |
||
| 2020 | * Cascades the save operation to associated documents. |
||
| 2021 | */ |
||
| 2022 | 638 | private function cascadePersist(object $document, array &$visited) : void |
|
| 2071 | |||
| 2072 | /** |
||
| 2073 | * Cascades the delete operation to associated documents. |
||
| 2074 | */ |
||
| 2075 | 83 | private function cascadeRemove(object $document, array &$visited) : void |
|
| 2097 | |||
| 2098 | /** |
||
| 2099 | * Acquire a lock on the given document. |
||
| 2100 | * |
||
| 2101 | * @throws LockException |
||
| 2102 | * @throws InvalidArgumentException |
||
| 2103 | */ |
||
| 2104 | 8 | public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
|
| 2128 | |||
| 2129 | /** |
||
| 2130 | * Releases a lock on the given document. |
||
| 2131 | * |
||
| 2132 | * @throws InvalidArgumentException |
||
| 2133 | */ |
||
| 2134 | 1 | public function unlock(object $document) : void |
|
| 2142 | |||
| 2143 | /** |
||
| 2144 | * Clears the UnitOfWork. |
||
| 2145 | */ |
||
| 2146 | 378 | public function clear(?string $documentName = null) : void |
|
| 2184 | |||
| 2185 | /** |
||
| 2186 | * INTERNAL: |
||
| 2187 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2188 | * invoked on that document at the beginning of the next commit of this |
||
| 2189 | * UnitOfWork. |
||
| 2190 | * |
||
| 2191 | * @ignore |
||
| 2192 | */ |
||
| 2193 | 58 | public function scheduleOrphanRemoval(object $document) : void |
|
| 2197 | |||
| 2198 | /** |
||
| 2199 | * INTERNAL: |
||
| 2200 | * Unschedules an embedded or referenced object for removal. |
||
| 2201 | * |
||
| 2202 | * @ignore |
||
| 2203 | */ |
||
| 2204 | 123 | public function unscheduleOrphanRemoval(object $document) : void |
|
| 2209 | |||
| 2210 | /** |
||
| 2211 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2212 | * 1) sets owner if it was cloned |
||
| 2213 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2214 | * 3) NOP if state is OK |
||
| 2215 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2216 | */ |
||
| 2217 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, object $document, ClassMetadata $class, string $propName) : PersistentCollectionInterface |
|
| 2237 | |||
| 2238 | /** |
||
| 2239 | * INTERNAL: |
||
| 2240 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2241 | */ |
||
| 2242 | 47 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) : void |
|
| 2253 | |||
| 2254 | /** |
||
| 2255 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2256 | */ |
||
| 2257 | 220 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) : bool |
|
| 2261 | |||
| 2262 | /** |
||
| 2263 | * INTERNAL: |
||
| 2264 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2265 | */ |
||
| 2266 | 227 | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) : void |
|
| 2281 | |||
| 2282 | /** |
||
| 2283 | * INTERNAL: |
||
| 2284 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2285 | */ |
||
| 2286 | 254 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) : void |
|
| 2303 | |||
| 2304 | /** |
||
| 2305 | * INTERNAL: |
||
| 2306 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2307 | */ |
||
| 2308 | 227 | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) : void |
|
| 2323 | |||
| 2324 | /** |
||
| 2325 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2326 | */ |
||
| 2327 | 140 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) : bool |
|
| 2331 | |||
| 2332 | /** |
||
| 2333 | * INTERNAL: |
||
| 2334 | * Gets PersistentCollections that have been visited during computing change |
||
| 2335 | * set of $document |
||
| 2336 | * |
||
| 2337 | * @return PersistentCollectionInterface[] |
||
| 2338 | */ |
||
| 2339 | 581 | public function getVisitedCollections(object $document) : array |
|
| 2345 | |||
| 2346 | /** |
||
| 2347 | * INTERNAL: |
||
| 2348 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2349 | * |
||
| 2350 | * @return PersistentCollectionInterface[] |
||
| 2351 | */ |
||
| 2352 | 581 | public function getScheduledCollections(object $document) : array |
|
| 2358 | |||
| 2359 | /** |
||
| 2360 | * Checks whether the document is related to a PersistentCollection |
||
| 2361 | * scheduled for update or deletion. |
||
| 2362 | */ |
||
| 2363 | 56 | public function hasScheduledCollections(object $document) : bool |
|
| 2367 | |||
| 2368 | /** |
||
| 2369 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2370 | * a collection scheduled for update or deletion. |
||
| 2371 | * |
||
| 2372 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2373 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2374 | * |
||
| 2375 | * If the collection is nested within atomic collection, it is immediately |
||
| 2376 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2377 | * calculating update data way easier. |
||
| 2378 | */ |
||
| 2379 | 256 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) : void |
|
| 2409 | |||
| 2410 | /** |
||
| 2411 | * Get the top-most owning document of a given document |
||
| 2412 | * |
||
| 2413 | * If a top-level document is provided, that same document will be returned. |
||
| 2414 | * For an embedded document, we will walk through parent associations until |
||
| 2415 | * we find a top-level document. |
||
| 2416 | * |
||
| 2417 | * @throws UnexpectedValueException When a top-level document could not be found. |
||
| 2418 | */ |
||
| 2419 | 258 | public function getOwningDocument(object $document) : object |
|
| 2435 | |||
| 2436 | /** |
||
| 2437 | * Gets the class name for an association (embed or reference) with respect |
||
| 2438 | * to any discriminator value. |
||
| 2439 | * |
||
| 2440 | * @param array|null $data |
||
| 2441 | */ |
||
| 2442 | 227 | public function getClassNameForAssociation(array $mapping, $data) : string |
|
| 2472 | |||
| 2473 | /** |
||
| 2474 | * INTERNAL: |
||
| 2475 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2476 | * |
||
| 2477 | * @internal Highly performance-sensitive method. |
||
| 2478 | * |
||
| 2479 | * @ignore |
||
| 2480 | */ |
||
| 2481 | 402 | public function getOrCreateDocument(string $className, array $data, array &$hints = [], ?object $document = null) : object |
|
| 2554 | |||
| 2555 | /** |
||
| 2556 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2557 | */ |
||
| 2558 | 178 | public function loadCollection(PersistentCollectionInterface $collection) : void |
|
| 2567 | |||
| 2568 | /** |
||
| 2569 | * Gets the identity map of the UnitOfWork. |
||
| 2570 | */ |
||
| 2571 | public function getIdentityMap() : array |
||
| 2575 | |||
| 2576 | /** |
||
| 2577 | * Gets the original data of a document. The original data is the data that was |
||
| 2578 | * present at the time the document was reconstituted from the database. |
||
| 2579 | * |
||
| 2580 | * @return array |
||
| 2581 | */ |
||
| 2582 | 1 | public function getOriginalDocumentData(object $document) : array |
|
| 2588 | |||
| 2589 | 60 | public function setOriginalDocumentData(object $document, array $data) : void |
|
| 2595 | |||
| 2596 | /** |
||
| 2597 | * INTERNAL: |
||
| 2598 | * Sets a property value of the original data array of a document. |
||
| 2599 | * |
||
| 2600 | * @param mixed $value |
||
| 2601 | * |
||
| 2602 | * @ignore |
||
| 2603 | */ |
||
| 2604 | 3 | public function setOriginalDocumentProperty(string $oid, string $property, $value) : void |
|
| 2608 | |||
| 2609 | /** |
||
| 2610 | * Gets the identifier of a document. |
||
| 2611 | * |
||
| 2612 | * @return mixed The identifier value |
||
| 2613 | */ |
||
| 2614 | 452 | public function getDocumentIdentifier(object $document) |
|
| 2618 | |||
| 2619 | /** |
||
| 2620 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2621 | * |
||
| 2622 | * @return bool TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2623 | */ |
||
| 2624 | public function hasPendingInsertions() : bool |
||
| 2628 | |||
| 2629 | /** |
||
| 2630 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2631 | * number of documents in the identity map. |
||
| 2632 | */ |
||
| 2633 | 2 | public function size() : int |
|
| 2641 | |||
| 2642 | /** |
||
| 2643 | * INTERNAL: |
||
| 2644 | * Registers a document as managed. |
||
| 2645 | * |
||
| 2646 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2647 | * document class. If the class expects its database identifier to be an |
||
| 2648 | * ObjectId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2649 | * document identifiers map will become inconsistent with the identity map. |
||
| 2650 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2651 | * conversion and throw an exception if it's inconsistent. |
||
| 2652 | * |
||
| 2653 | * @param mixed $id The identifier values. |
||
| 2654 | */ |
||
| 2655 | 381 | public function registerManaged(object $document, $id, array $data) : void |
|
| 2670 | |||
| 2671 | /** |
||
| 2672 | * INTERNAL: |
||
| 2673 | * Clears the property changeset of the document with the given OID. |
||
| 2674 | */ |
||
| 2675 | public function clearDocumentChangeSet(string $oid) |
||
| 2679 | |||
| 2680 | /* PropertyChangedListener implementation */ |
||
| 2681 | |||
| 2682 | /** |
||
| 2683 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2684 | * |
||
| 2685 | * @param object $document The document that owns the property. |
||
| 2686 | * @param string $propertyName The name of the property that changed. |
||
| 2687 | * @param mixed $oldValue The old value of the property. |
||
| 2688 | * @param mixed $newValue The new value of the property. |
||
| 2689 | */ |
||
| 2690 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2707 | |||
| 2708 | /** |
||
| 2709 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2710 | */ |
||
| 2711 | 3 | public function getScheduledDocumentInsertions() : array |
|
| 2715 | |||
| 2716 | /** |
||
| 2717 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2718 | */ |
||
| 2719 | 1 | public function getScheduledDocumentUpserts() : array |
|
| 2723 | |||
| 2724 | /** |
||
| 2725 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2726 | */ |
||
| 2727 | 2 | public function getScheduledDocumentUpdates() : array |
|
| 2731 | |||
| 2732 | /** |
||
| 2733 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2734 | */ |
||
| 2735 | public function getScheduledDocumentDeletions() : array |
||
| 2739 | |||
| 2740 | /** |
||
| 2741 | * Get the currently scheduled complete collection deletions |
||
| 2742 | */ |
||
| 2743 | public function getScheduledCollectionDeletions() : array |
||
| 2747 | |||
| 2748 | /** |
||
| 2749 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2750 | */ |
||
| 2751 | public function getScheduledCollectionUpdates() : array |
||
| 2755 | |||
| 2756 | /** |
||
| 2757 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2758 | */ |
||
| 2759 | public function initializeObject(object $obj) : void |
||
| 2767 | |||
| 2768 | private function objToStr(object $obj) : string |
||
| 2772 | } |
||
| 2773 |
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.