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 |
||
| 30 | class UnitOfWork implements PropertyChangedListener |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * An entity is in MANAGED state when its persistence is managed by an EntityManager. |
||
| 34 | */ |
||
| 35 | const STATE_MANAGED = 1; |
||
| 36 | /** |
||
| 37 | * An entity is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 38 | * and is not (yet) managed by an EntityManager. |
||
| 39 | */ |
||
| 40 | const STATE_NEW = 2; |
||
| 41 | /** |
||
| 42 | * A detached entity is an instance with persistent state and identity that is not |
||
| 43 | * (or no longer) associated with an EntityManager (and a UnitOfWork). |
||
| 44 | */ |
||
| 45 | const STATE_DETACHED = 3; |
||
| 46 | /** |
||
| 47 | * A removed entity instance is an instance with a persistent identity, |
||
| 48 | * associated with an EntityManager, whose persistent state will be deleted |
||
| 49 | * on commit. |
||
| 50 | */ |
||
| 51 | const STATE_REMOVED = 4; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The (cached) states of any known entities. |
||
| 55 | * Keys are object ids (spl_object_hash). |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $entityStates = []; |
||
| 60 | |||
| 61 | /** @var EntityManager */ |
||
| 62 | private $manager; |
||
| 63 | /** @var EntityPersister[] */ |
||
| 64 | private $persisters = []; |
||
| 65 | /** @var CollectionPersister[] */ |
||
| 66 | private $collectionPersisters = []; |
||
| 67 | /** @var array */ |
||
| 68 | private $entityIdentifiers = []; |
||
| 69 | /** @var object[][] */ |
||
| 70 | private $identityMap = []; |
||
| 71 | /** @var IdentifierFlattener */ |
||
| 72 | private $identifierFlattener; |
||
| 73 | /** @var array */ |
||
| 74 | private $originalEntityData = []; |
||
| 75 | /** @var array */ |
||
| 76 | private $entityDeletions = []; |
||
| 77 | /** @var array */ |
||
| 78 | private $entityChangeSets = []; |
||
| 79 | /** @var array */ |
||
| 80 | private $entityInsertions = []; |
||
| 81 | /** @var array */ |
||
| 82 | private $entityUpdates = []; |
||
| 83 | /** @var array */ |
||
| 84 | private $readOnlyObjects = []; |
||
| 85 | /** @var array */ |
||
| 86 | private $scheduledForSynchronization = []; |
||
| 87 | /** @var array */ |
||
| 88 | private $orphanRemovals = []; |
||
| 89 | /** @var ApiCollection[] */ |
||
| 90 | private $collectionDeletions = []; |
||
| 91 | /** @var array */ |
||
| 92 | private $extraUpdates = []; |
||
| 93 | /** @var ApiCollection[] */ |
||
| 94 | private $collectionUpdates = []; |
||
| 95 | /** @var ApiCollection[] */ |
||
| 96 | private $visitedCollections = []; |
||
| 97 | /** @var ReflectionPropertiesGetter */ |
||
| 98 | private $reflectionPropertiesGetter; |
||
| 99 | /** @var Hydrator[] */ |
||
| 100 | private $hydrators = []; |
||
| 101 | /** @var CrudsApiInterface[] */ |
||
| 102 | private $apis = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * UnitOfWork constructor. |
||
| 106 | * |
||
| 107 | * @param EntityManager $manager |
||
| 108 | */ |
||
| 109 | 28 | public function __construct(EntityManager $manager) |
|
| 110 | { |
||
| 111 | 28 | $this->manager = $manager; |
|
| 112 | 28 | $this->identifierFlattener = new IdentifierFlattener($this->manager); |
|
| 113 | 28 | $this->reflectionPropertiesGetter = new ReflectionPropertiesGetter(new RuntimeReflectionService()); |
|
| 114 | 28 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param $className |
||
| 118 | * |
||
| 119 | * @return EntityPersister |
||
| 120 | */ |
||
| 121 | 26 | public function getEntityPersister($className) |
|
| 122 | { |
||
| 123 | 26 | if (!array_key_exists($className, $this->persisters)) { |
|
| 124 | /** @var ApiMetadata $classMetadata */ |
||
| 125 | 26 | $classMetadata = $this->manager->getClassMetadata($className); |
|
| 126 | |||
| 127 | 26 | $api = $this->getCrudsApi($classMetadata); |
|
| 128 | |||
| 129 | 26 | if ($api instanceof EntityCacheAwareInterface) { |
|
| 130 | 26 | $api->setEntityCache($this->createEntityCache($classMetadata)); |
|
| 131 | 26 | } |
|
| 132 | |||
| 133 | 26 | $this->persisters[$className] = new ApiPersister($this->manager, $api); |
|
| 134 | 26 | } |
|
| 135 | |||
| 136 | 26 | return $this->persisters[$className]; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Checks whether an entity is registered in the identity map of this UnitOfWork. |
||
| 141 | * |
||
| 142 | * @param object $entity |
||
| 143 | * |
||
| 144 | * @return boolean |
||
| 145 | */ |
||
| 146 | public function isInIdentityMap($entity) |
||
| 147 | { |
||
| 148 | $oid = spl_object_hash($entity); |
||
| 149 | |||
| 150 | if (!isset($this->entityIdentifiers[$oid])) { |
||
| 151 | return false; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** @var EntityMetadata $classMetadata */ |
||
| 155 | $classMetadata = $this->manager->getClassMetadata(get_class($entity)); |
||
| 156 | $idHash = implode(' ', $this->entityIdentifiers[$oid]); |
||
| 157 | |||
| 158 | if ($idHash === '') { |
||
| 159 | return false; |
||
| 160 | } |
||
| 161 | |||
| 162 | return isset($this->identityMap[$classMetadata->rootEntityName][$idHash]); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Gets the identifier of an entity. |
||
| 167 | * The returned value is always an array of identifier values. If the entity |
||
| 168 | * has a composite identifier then the identifier values are in the same |
||
| 169 | * order as the identifier field names as returned by ClassMetadata#getIdentifierFieldNames(). |
||
| 170 | * |
||
| 171 | * @param object $entity |
||
| 172 | * |
||
| 173 | * @return array The identifier values. |
||
| 174 | */ |
||
| 175 | 1 | public function getEntityIdentifier($entity) |
|
| 176 | { |
||
| 177 | 1 | return $this->entityIdentifiers[spl_object_hash($entity)]; |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $className |
||
| 182 | * @param \stdClass $data |
||
| 183 | * |
||
| 184 | * @return ObjectManagerAware|object |
||
| 185 | * @throws MappingException |
||
| 186 | */ |
||
| 187 | 18 | public function getOrCreateEntity($className, \stdClass $data) |
|
| 188 | { |
||
| 189 | /** @var EntityMetadata $class */ |
||
| 190 | 18 | $class = $this->resolveSourceMetadataForClass($data, $className); |
|
| 191 | 18 | $tmpEntity = $this->getHydratorForClass($class)->hydarate($data); |
|
| 192 | |||
| 193 | 18 | $id = $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($tmpEntity)); |
|
| 194 | 18 | $idHash = implode(' ', $id); |
|
| 195 | |||
| 196 | 18 | $overrideLocalValues = false; |
|
| 197 | 18 | if (isset($this->identityMap[$class->rootEntityName][$idHash])) { |
|
| 198 | 2 | $entity = $this->identityMap[$class->rootEntityName][$idHash]; |
|
| 199 | 2 | $oid = spl_object_hash($entity); |
|
| 200 | |||
| 201 | 2 | if ($entity instanceof Proxy && !$entity->__isInitialized()) { |
|
| 202 | 2 | $entity->__setInitialized(true); |
|
| 203 | |||
| 204 | 2 | $overrideLocalValues = true; |
|
| 205 | 2 | $this->originalEntityData[$oid] = $data; |
|
| 206 | |||
| 207 | 2 | if ($entity instanceof NotifyPropertyChanged) { |
|
| 208 | $entity->addPropertyChangedListener($this); |
||
| 209 | } |
||
| 210 | 2 | } |
|
| 211 | 2 | } else { |
|
| 212 | 17 | $entity = $this->newInstance($class); |
|
| 213 | 17 | $oid = spl_object_hash($entity); |
|
| 214 | 17 | $this->entityIdentifiers[$oid] = $id; |
|
| 215 | 17 | $this->entityStates[$oid] = self::STATE_MANAGED; |
|
| 216 | 17 | $this->originalEntityData[$oid] = $data; |
|
| 217 | 17 | $this->identityMap[$class->rootEntityName][$idHash] = $entity; |
|
| 218 | 17 | if ($entity instanceof NotifyPropertyChanged) { |
|
| 219 | $entity->addPropertyChangedListener($this); |
||
| 220 | } |
||
| 221 | 17 | $overrideLocalValues = true; |
|
| 222 | } |
||
| 223 | |||
| 224 | 18 | if (!$overrideLocalValues) { |
|
| 225 | return $entity; |
||
| 226 | } |
||
| 227 | |||
| 228 | 18 | $entity = $this->getHydratorForClass($class)->hydarate($data, $entity); |
|
| 229 | |||
| 230 | 18 | return $entity; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * INTERNAL: |
||
| 235 | * Registers an entity as managed. |
||
| 236 | * |
||
| 237 | * @param object $entity The entity. |
||
| 238 | * @param array $id The identifier values. |
||
| 239 | * @param \stdClass|null $data The original entity data. |
||
| 240 | * |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | 4 | public function registerManaged($entity, array $id, \stdClass $data = null) |
|
| 244 | { |
||
| 245 | 4 | $oid = spl_object_hash($entity); |
|
| 246 | |||
| 247 | 4 | $this->entityIdentifiers[$oid] = $id; |
|
| 248 | 4 | $this->entityStates[$oid] = self::STATE_MANAGED; |
|
| 249 | 4 | $this->originalEntityData[$oid] = $data; |
|
| 250 | |||
| 251 | 4 | $this->addToIdentityMap($entity); |
|
| 252 | |||
| 253 | 4 | if ($entity instanceof NotifyPropertyChanged && (!$entity instanceof Proxy || $entity->__isInitialized())) { |
|
| 254 | $entity->addPropertyChangedListener($this); |
||
| 255 | } |
||
| 256 | 4 | } |
|
| 257 | |||
| 258 | /** |
||
| 259 | * INTERNAL: |
||
| 260 | * Registers an entity in the identity map. |
||
| 261 | * Note that entities in a hierarchy are registered with the class name of |
||
| 262 | * the root entity. |
||
| 263 | * |
||
| 264 | * @ignore |
||
| 265 | * |
||
| 266 | * @param object $entity The entity to register. |
||
| 267 | * |
||
| 268 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 269 | * the entity in question is already managed. |
||
| 270 | * |
||
| 271 | */ |
||
| 272 | 11 | public function addToIdentityMap($entity) |
|
| 273 | { |
||
| 274 | /** @var EntityMetadata $classMetadata */ |
||
| 275 | 11 | $classMetadata = $this->manager->getClassMetadata(get_class($entity)); |
|
| 276 | 11 | $idHash = implode(' ', $this->entityIdentifiers[spl_object_hash($entity)]); |
|
| 277 | |||
| 278 | 11 | if ($idHash === '') { |
|
| 279 | throw new \InvalidArgumentException('Entitty does not have valid identifiers to be stored at identity map'); |
||
| 280 | } |
||
| 281 | |||
| 282 | 11 | $className = $classMetadata->rootEntityName; |
|
| 283 | |||
| 284 | 11 | if (isset($this->identityMap[$className][$idHash])) { |
|
| 285 | return false; |
||
| 286 | } |
||
| 287 | |||
| 288 | 11 | $this->identityMap[$className][$idHash] = $entity; |
|
| 289 | |||
| 290 | 11 | return true; |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Gets the identity map of the UnitOfWork. |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | public function getIdentityMap() |
||
| 299 | { |
||
| 300 | return $this->identityMap; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Gets the original data of an entity. The original data is the data that was |
||
| 305 | * present at the time the entity was reconstituted from the database. |
||
| 306 | * |
||
| 307 | * @param object $entity |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public function getOriginalEntityData($entity) |
||
| 312 | { |
||
| 313 | $oid = spl_object_hash($entity); |
||
| 314 | |||
| 315 | if (isset($this->originalEntityData[$oid])) { |
||
| 316 | return $this->originalEntityData[$oid]; |
||
| 317 | } |
||
| 318 | |||
| 319 | return []; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * INTERNAL: |
||
| 324 | * Checks whether an identifier hash exists in the identity map. |
||
| 325 | * |
||
| 326 | * @ignore |
||
| 327 | * |
||
| 328 | * @param string $idHash |
||
| 329 | * @param string $rootClassName |
||
| 330 | * |
||
| 331 | * @return boolean |
||
| 332 | */ |
||
| 333 | public function containsIdHash($idHash, $rootClassName) |
||
| 334 | { |
||
| 335 | return isset($this->identityMap[$rootClassName][$idHash]); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * INTERNAL: |
||
| 340 | * Gets an entity in the identity map by its identifier hash. |
||
| 341 | * |
||
| 342 | * @ignore |
||
| 343 | * |
||
| 344 | * @param string $idHash |
||
| 345 | * @param string $rootClassName |
||
| 346 | * |
||
| 347 | * @return object |
||
| 348 | */ |
||
| 349 | public function getByIdHash($idHash, $rootClassName) |
||
| 350 | { |
||
| 351 | return $this->identityMap[$rootClassName][$idHash]; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * INTERNAL: |
||
| 356 | * Tries to get an entity by its identifier hash. If no entity is found for |
||
| 357 | * the given hash, FALSE is returned. |
||
| 358 | * |
||
| 359 | * @ignore |
||
| 360 | * |
||
| 361 | * @param mixed $idHash (must be possible to cast it to string) |
||
| 362 | * @param string $rootClassName |
||
| 363 | * |
||
| 364 | * @return object|bool The found entity or FALSE. |
||
| 365 | */ |
||
| 366 | public function tryGetByIdHash($idHash, $rootClassName) |
||
| 367 | { |
||
| 368 | $stringIdHash = (string)$idHash; |
||
| 369 | |||
| 370 | if (isset($this->identityMap[$rootClassName][$stringIdHash])) { |
||
| 371 | return $this->identityMap[$rootClassName][$stringIdHash]; |
||
| 372 | } |
||
| 373 | |||
| 374 | return false; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Gets the state of an entity with regard to the current unit of work. |
||
| 379 | * |
||
| 380 | * @param object $entity |
||
| 381 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 382 | * This parameter can be set to improve performance of entity state detection |
||
| 383 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 384 | * is either known or does not matter for the caller of the method. |
||
| 385 | * |
||
| 386 | * @return int The entity state. |
||
| 387 | */ |
||
| 388 | 7 | public function getEntityState($entity, $assume = null) |
|
| 389 | { |
||
| 390 | 7 | $oid = spl_object_hash($entity); |
|
| 391 | 7 | if (isset($this->entityStates[$oid])) { |
|
| 392 | 2 | return $this->entityStates[$oid]; |
|
| 393 | } |
||
| 394 | 7 | if ($assume !== null) { |
|
| 395 | 7 | return $assume; |
|
| 396 | } |
||
| 397 | // State can only be NEW or DETACHED, because MANAGED/REMOVED states are known. |
||
| 398 | // Note that you can not remember the NEW or DETACHED state in _entityStates since |
||
| 399 | // the UoW does not hold references to such objects and the object hash can be reused. |
||
| 400 | // More generally because the state may "change" between NEW/DETACHED without the UoW being aware of it. |
||
| 401 | $class = $this->manager->getClassMetadata(get_class($entity)); |
||
| 402 | $id = $class->getIdentifierValues($entity); |
||
| 403 | if (!$id) { |
||
|
|
|||
| 404 | return self::STATE_NEW; |
||
| 405 | } |
||
| 406 | |||
| 407 | return self::STATE_DETACHED; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Tries to find an entity with the given identifier in the identity map of |
||
| 412 | * this UnitOfWork. |
||
| 413 | * |
||
| 414 | * @param mixed $id The entity identifier to look for. |
||
| 415 | * @param string $rootClassName The name of the root class of the mapped entity hierarchy. |
||
| 416 | * |
||
| 417 | * @return object|bool Returns the entity with the specified identifier if it exists in |
||
| 418 | * this UnitOfWork, FALSE otherwise. |
||
| 419 | */ |
||
| 420 | 15 | public function tryGetById($id, $rootClassName) |
|
| 421 | { |
||
| 422 | /** @var EntityMetadata $metadata */ |
||
| 423 | 15 | $metadata = $this->manager->getClassMetadata($rootClassName); |
|
| 424 | 15 | $idHash = implode(' ', (array)$this->identifierFlattener->flattenIdentifier($metadata, $id)); |
|
| 425 | |||
| 426 | 15 | if (isset($this->identityMap[$rootClassName][$idHash])) { |
|
| 427 | 5 | return $this->identityMap[$rootClassName][$idHash]; |
|
| 428 | } |
||
| 429 | |||
| 430 | 15 | return false; |
|
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Notifies this UnitOfWork of a property change in an entity. |
||
| 435 | * |
||
| 436 | * @param object $entity The entity that owns the property. |
||
| 437 | * @param string $propertyName The name of the property that changed. |
||
| 438 | * @param mixed $oldValue The old value of the property. |
||
| 439 | * @param mixed $newValue The new value of the property. |
||
| 440 | * |
||
| 441 | * @return void |
||
| 442 | */ |
||
| 443 | public function propertyChanged($entity, $propertyName, $oldValue, $newValue) |
||
| 444 | { |
||
| 445 | $oid = spl_object_hash($entity); |
||
| 446 | $class = $this->manager->getClassMetadata(get_class($entity)); |
||
| 447 | $isAssocField = $class->hasAssociation($propertyName); |
||
| 448 | if (!$isAssocField && !$class->hasField($propertyName)) { |
||
| 449 | return; // ignore non-persistent fields |
||
| 450 | } |
||
| 451 | // Update changeset and mark entity for synchronization |
||
| 452 | $this->entityChangeSets[$oid][$propertyName] = [$oldValue, $newValue]; |
||
| 453 | if (!isset($this->scheduledForSynchronization[$class->getRootEntityName()][$oid])) { |
||
| 454 | $this->scheduleForDirtyCheck($entity); |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Persists an entity as part of the current unit of work. |
||
| 460 | * |
||
| 461 | * @param object $entity The entity to persist. |
||
| 462 | * |
||
| 463 | * @return void |
||
| 464 | */ |
||
| 465 | 7 | public function persist($entity) |
|
| 466 | { |
||
| 467 | 7 | $visited = []; |
|
| 468 | 7 | $this->doPersist($entity, $visited); |
|
| 469 | 7 | } |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @param ApiMetadata $class |
||
| 473 | * @param $entity |
||
| 474 | * |
||
| 475 | * @throws \InvalidArgumentException |
||
| 476 | * @throws \RuntimeException |
||
| 477 | */ |
||
| 478 | 2 | public function recomputeSingleEntityChangeSet(ApiMetadata $class, $entity) |
|
| 479 | { |
||
| 480 | 2 | $oid = spl_object_hash($entity); |
|
| 481 | 2 | if (!isset($this->entityStates[$oid]) || $this->entityStates[$oid] != self::STATE_MANAGED) { |
|
| 482 | throw new \InvalidArgumentException('Entity is not managed'); |
||
| 483 | } |
||
| 484 | |||
| 485 | 2 | $actualData = []; |
|
| 486 | 2 | foreach ($class->getReflectionProperties() as $name => $refProp) { |
|
| 487 | 2 | if (!$class->isIdentifier($name) && !$class->isCollectionValuedAssociation($name)) { |
|
| 488 | 2 | $actualData[$name] = $refProp->getValue($entity); |
|
| 489 | 2 | } |
|
| 490 | 2 | } |
|
| 491 | 2 | if (!isset($this->originalEntityData[$oid])) { |
|
| 492 | throw new \RuntimeException( |
||
| 493 | 'Cannot call recomputeSingleEntityChangeSet before computeChangeSet on an entity.' |
||
| 494 | ); |
||
| 495 | } |
||
| 496 | 2 | $originalData = $this->originalEntityData[$oid]; |
|
| 497 | 2 | $changeSet = []; |
|
| 498 | 2 | foreach ($actualData as $propName => $actualValue) { |
|
| 499 | 2 | $orgValue = isset($originalData->$propName) ? $originalData->$propName : null; |
|
| 500 | 2 | if ($orgValue !== $actualValue) { |
|
| 501 | $changeSet[$propName] = [$orgValue, $actualValue]; |
||
| 502 | } |
||
| 503 | 2 | } |
|
| 504 | 2 | if ($changeSet) { |
|
| 505 | if (isset($this->entityChangeSets[$oid])) { |
||
| 506 | $this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet); |
||
| 507 | } else { |
||
| 508 | if (!isset($this->entityInsertions[$oid])) { |
||
| 509 | $this->entityChangeSets[$oid] = $changeSet; |
||
| 510 | $this->entityUpdates[$oid] = $entity; |
||
| 511 | } |
||
| 512 | } |
||
| 513 | $this->originalEntityData[$oid] = (object)$actualData; |
||
| 514 | } |
||
| 515 | 2 | } |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Schedules an entity for insertion into the database. |
||
| 519 | * If the entity already has an identifier, it will be added to the identity map. |
||
| 520 | * |
||
| 521 | * @param object $entity The entity to schedule for insertion. |
||
| 522 | * |
||
| 523 | * @return void |
||
| 524 | * |
||
| 525 | * @throws \InvalidArgumentException |
||
| 526 | */ |
||
| 527 | 7 | public function scheduleForInsert($entity) |
|
| 528 | { |
||
| 529 | 7 | $oid = spl_object_hash($entity); |
|
| 530 | 7 | if (isset($this->entityUpdates[$oid])) { |
|
| 531 | throw new \InvalidArgumentException('Dirty entity cannot be scheduled for insertion'); |
||
| 532 | } |
||
| 533 | 7 | if (isset($this->entityDeletions[$oid])) { |
|
| 534 | throw new \InvalidArgumentException('Removed entity scheduled for insertion'); |
||
| 535 | } |
||
| 536 | 7 | if (isset($this->originalEntityData[$oid]) && !isset($this->entityInsertions[$oid])) { |
|
| 537 | throw new \InvalidArgumentException('Managed entity scheduled for insertion'); |
||
| 538 | } |
||
| 539 | 7 | if (isset($this->entityInsertions[$oid])) { |
|
| 540 | throw new \InvalidArgumentException('Entity scheduled for insertion twice'); |
||
| 541 | } |
||
| 542 | 7 | $this->entityInsertions[$oid] = $entity; |
|
| 543 | 7 | if (isset($this->entityIdentifiers[$oid])) { |
|
| 544 | $this->addToIdentityMap($entity); |
||
| 545 | } |
||
| 546 | 7 | if ($entity instanceof NotifyPropertyChanged) { |
|
| 547 | $entity->addPropertyChangedListener($this); |
||
| 548 | } |
||
| 549 | 7 | } |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Checks whether an entity is scheduled for insertion. |
||
| 553 | * |
||
| 554 | * @param object $entity |
||
| 555 | * |
||
| 556 | * @return boolean |
||
| 557 | */ |
||
| 558 | 1 | public function isScheduledForInsert($entity) |
|
| 559 | { |
||
| 560 | 1 | return isset($this->entityInsertions[spl_object_hash($entity)]); |
|
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Schedules an entity for being updated. |
||
| 565 | * |
||
| 566 | * @param object $entity The entity to schedule for being updated. |
||
| 567 | * |
||
| 568 | * @return void |
||
| 569 | * |
||
| 570 | * @throws \InvalidArgumentException |
||
| 571 | */ |
||
| 572 | public function scheduleForUpdate($entity) |
||
| 573 | { |
||
| 574 | $oid = spl_object_hash($entity); |
||
| 575 | if (!isset($this->entityIdentifiers[$oid])) { |
||
| 576 | throw new \InvalidArgumentException('Entity has no identity'); |
||
| 577 | } |
||
| 578 | if (isset($this->entityDeletions[$oid])) { |
||
| 579 | throw new \InvalidArgumentException('Entity is removed'); |
||
| 580 | } |
||
| 581 | if (!isset($this->entityUpdates[$oid]) && !isset($this->entityInsertions[$oid])) { |
||
| 582 | $this->entityUpdates[$oid] = $entity; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Checks whether an entity is registered as dirty in the unit of work. |
||
| 588 | * Note: Is not very useful currently as dirty entities are only registered |
||
| 589 | * at commit time. |
||
| 590 | * |
||
| 591 | * @param object $entity |
||
| 592 | * |
||
| 593 | * @return boolean |
||
| 594 | */ |
||
| 595 | public function isScheduledForUpdate($entity) |
||
| 596 | { |
||
| 597 | return isset($this->entityUpdates[spl_object_hash($entity)]); |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Checks whether an entity is registered to be checked in the unit of work. |
||
| 602 | * |
||
| 603 | * @param object $entity |
||
| 604 | * |
||
| 605 | * @return boolean |
||
| 606 | */ |
||
| 607 | public function isScheduledForDirtyCheck($entity) |
||
| 608 | { |
||
| 609 | $rootEntityName = $this->manager->getClassMetadata(get_class($entity))->getRootEntityName(); |
||
| 610 | |||
| 611 | return isset($this->scheduledForSynchronization[$rootEntityName][spl_object_hash($entity)]); |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * INTERNAL: |
||
| 616 | * Schedules an entity for deletion. |
||
| 617 | * |
||
| 618 | * @param object $entity |
||
| 619 | * |
||
| 620 | * @return void |
||
| 621 | */ |
||
| 622 | public function scheduleForDelete($entity) |
||
| 623 | { |
||
| 624 | $oid = spl_object_hash($entity); |
||
| 625 | if (isset($this->entityInsertions[$oid])) { |
||
| 626 | if ($this->isInIdentityMap($entity)) { |
||
| 627 | $this->removeFromIdentityMap($entity); |
||
| 628 | } |
||
| 629 | unset($this->entityInsertions[$oid], $this->entityStates[$oid]); |
||
| 630 | |||
| 631 | return; // entity has not been persisted yet, so nothing more to do. |
||
| 632 | } |
||
| 633 | if (!$this->isInIdentityMap($entity)) { |
||
| 634 | return; |
||
| 635 | } |
||
| 636 | $this->removeFromIdentityMap($entity); |
||
| 637 | unset($this->entityUpdates[$oid]); |
||
| 638 | if (!isset($this->entityDeletions[$oid])) { |
||
| 639 | $this->entityDeletions[$oid] = $entity; |
||
| 640 | $this->entityStates[$oid] = self::STATE_REMOVED; |
||
| 641 | } |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Checks whether an entity is registered as removed/deleted with the unit |
||
| 646 | * of work. |
||
| 647 | * |
||
| 648 | * @param object $entity |
||
| 649 | * |
||
| 650 | * @return boolean |
||
| 651 | */ |
||
| 652 | public function isScheduledForDelete($entity) |
||
| 653 | { |
||
| 654 | return isset($this->entityDeletions[spl_object_hash($entity)]); |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Checks whether an entity is scheduled for insertion, update or deletion. |
||
| 659 | * |
||
| 660 | * @param object $entity |
||
| 661 | * |
||
| 662 | * @return boolean |
||
| 663 | */ |
||
| 664 | public function isEntityScheduled($entity) |
||
| 665 | { |
||
| 666 | $oid = spl_object_hash($entity); |
||
| 667 | |||
| 668 | return isset($this->entityInsertions[$oid]) |
||
| 669 | || isset($this->entityUpdates[$oid]) |
||
| 670 | || isset($this->entityDeletions[$oid]); |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * INTERNAL: |
||
| 675 | * Removes an entity from the identity map. This effectively detaches the |
||
| 676 | * entity from the persistence management of Doctrine. |
||
| 677 | * |
||
| 678 | * @ignore |
||
| 679 | * |
||
| 680 | * @param object $entity |
||
| 681 | * |
||
| 682 | * @return boolean |
||
| 683 | * |
||
| 684 | * @throws \InvalidArgumentException |
||
| 685 | */ |
||
| 686 | public function removeFromIdentityMap($entity) |
||
| 687 | { |
||
| 688 | $oid = spl_object_hash($entity); |
||
| 689 | $classMetadata = $this->manager->getClassMetadata(get_class($entity)); |
||
| 690 | $idHash = implode(' ', $this->entityIdentifiers[$oid]); |
||
| 691 | if ($idHash === '') { |
||
| 692 | throw new \InvalidArgumentException('Entity has no identity'); |
||
| 693 | } |
||
| 694 | $className = $classMetadata->getRootEntityName(); |
||
| 695 | if (isset($this->identityMap[$className][$idHash])) { |
||
| 696 | unset($this->identityMap[$className][$idHash]); |
||
| 697 | unset($this->readOnlyObjects[$oid]); |
||
| 698 | |||
| 699 | //$this->entityStates[$oid] = self::STATE_DETACHED; |
||
| 700 | return true; |
||
| 701 | } |
||
| 702 | |||
| 703 | return false; |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 708 | * up to this point. The state of all managed entities will be synchronized with |
||
| 709 | * the database. |
||
| 710 | * |
||
| 711 | * The operations are executed in the following order: |
||
| 712 | * |
||
| 713 | * 1) All entity insertions |
||
| 714 | * 2) All entity updates |
||
| 715 | * 3) All collection deletions |
||
| 716 | * 4) All collection updates |
||
| 717 | * 5) All entity deletions |
||
| 718 | * |
||
| 719 | * @param null|object|array $entity |
||
| 720 | * |
||
| 721 | * @return void |
||
| 722 | * |
||
| 723 | * @throws \Exception |
||
| 724 | */ |
||
| 725 | 7 | public function commit($entity = null) |
|
| 726 | { |
||
| 727 | // Compute changes done since last commit. |
||
| 728 | 7 | if ($entity === null) { |
|
| 729 | 7 | $this->computeChangeSets(); |
|
| 730 | 7 | } elseif (is_object($entity)) { |
|
| 731 | $this->computeSingleEntityChangeSet($entity); |
||
| 732 | } elseif (is_array($entity)) { |
||
| 733 | foreach ((array)$entity as $object) { |
||
| 734 | $this->computeSingleEntityChangeSet($object); |
||
| 735 | } |
||
| 736 | } |
||
| 737 | 7 | if (!($this->entityInsertions || |
|
| 738 | 2 | $this->entityDeletions || |
|
| 739 | 2 | $this->entityUpdates || |
|
| 740 | 1 | $this->collectionUpdates || |
|
| 741 | 1 | $this->collectionDeletions || |
|
| 742 | 1 | $this->orphanRemovals) |
|
| 743 | 7 | ) { |
|
| 744 | 1 | return; // Nothing to do. |
|
| 745 | } |
||
| 746 | 7 | if ($this->orphanRemovals) { |
|
| 747 | foreach ($this->orphanRemovals as $orphan) { |
||
| 748 | $this->remove($orphan); |
||
| 749 | } |
||
| 750 | } |
||
| 751 | // Now we need a commit order to maintain referential integrity |
||
| 752 | 7 | $commitOrder = $this->getCommitOrder(); |
|
| 753 | |||
| 754 | // Collection deletions (deletions of complete collections) |
||
| 755 | // foreach ($this->collectionDeletions as $collectionToDelete) { |
||
| 756 | // //fixme: collection mutations |
||
| 757 | // $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); |
||
| 758 | // } |
||
| 759 | 7 | if ($this->entityInsertions) { |
|
| 760 | 7 | foreach ($commitOrder as $class) { |
|
| 761 | 7 | $this->executeInserts($class); |
|
| 762 | 7 | } |
|
| 763 | 7 | } |
|
| 764 | 7 | if ($this->entityUpdates) { |
|
| 765 | 2 | foreach ($commitOrder as $class) { |
|
| 766 | 2 | $this->executeUpdates($class); |
|
| 767 | 2 | } |
|
| 768 | 2 | } |
|
| 769 | // Extra updates that were requested by persisters. |
||
| 770 | 7 | if ($this->extraUpdates) { |
|
| 771 | $this->executeExtraUpdates(); |
||
| 772 | } |
||
| 773 | // Collection updates (deleteRows, updateRows, insertRows) |
||
| 774 | 7 | foreach ($this->collectionUpdates as $collectionToUpdate) { |
|
| 775 | //fixme: decide what to do with collection mutation if API does not support this |
||
| 776 | //$this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); |
||
| 777 | 7 | } |
|
| 778 | // Entity deletions come last and need to be in reverse commit order |
||
| 779 | 7 | if ($this->entityDeletions) { |
|
| 780 | for ($count = count($commitOrder), $i = $count - 1; $i >= 0 && $this->entityDeletions; --$i) { |
||
| 781 | $this->executeDeletions($commitOrder[$i]); |
||
| 782 | } |
||
| 783 | } |
||
| 784 | |||
| 785 | // Take new snapshots from visited collections |
||
| 786 | 7 | foreach ($this->visitedCollections as $coll) { |
|
| 787 | 2 | $coll->takeSnapshot(); |
|
| 788 | 7 | } |
|
| 789 | |||
| 790 | // Clear up |
||
| 791 | 7 | $this->entityInsertions = |
|
| 792 | 7 | $this->entityUpdates = |
|
| 793 | 7 | $this->entityDeletions = |
|
| 794 | 7 | $this->extraUpdates = |
|
| 795 | 7 | $this->entityChangeSets = |
|
| 796 | 7 | $this->collectionUpdates = |
|
| 797 | 7 | $this->collectionDeletions = |
|
| 798 | 7 | $this->visitedCollections = |
|
| 799 | 7 | $this->scheduledForSynchronization = |
|
| 800 | 7 | $this->orphanRemovals = []; |
|
| 801 | 7 | } |
|
| 802 | |||
| 803 | /** |
||
| 804 | * Gets the changeset for an entity. |
||
| 805 | * |
||
| 806 | * @param object $entity |
||
| 807 | * |
||
| 808 | * @return array |
||
| 809 | */ |
||
| 810 | 2 | public function & getEntityChangeSet($entity) |
|
| 811 | { |
||
| 812 | 2 | $oid = spl_object_hash($entity); |
|
| 813 | 2 | $data = []; |
|
| 814 | 2 | if (!isset($this->entityChangeSets[$oid])) { |
|
| 815 | return $data; |
||
| 816 | } |
||
| 817 | |||
| 818 | 2 | return $this->entityChangeSets[$oid]; |
|
| 819 | } |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Computes the changes that happened to a single entity. |
||
| 823 | * |
||
| 824 | * Modifies/populates the following properties: |
||
| 825 | * |
||
| 826 | * {@link _originalEntityData} |
||
| 827 | * If the entity is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 828 | * then it was not fetched from the database and therefore we have no original |
||
| 829 | * entity data yet. All of the current entity data is stored as the original entity data. |
||
| 830 | * |
||
| 831 | * {@link _entityChangeSets} |
||
| 832 | * The changes detected on all properties of the entity are stored there. |
||
| 833 | * A change is a tuple array where the first entry is the old value and the second |
||
| 834 | * entry is the new value of the property. Changesets are used by persisters |
||
| 835 | * to INSERT/UPDATE the persistent entity state. |
||
| 836 | * |
||
| 837 | * {@link _entityUpdates} |
||
| 838 | * If the entity is already fully MANAGED (has been fetched from the database before) |
||
| 839 | * and any changes to its properties are detected, then a reference to the entity is stored |
||
| 840 | * there to mark it for an update. |
||
| 841 | * |
||
| 842 | * {@link _collectionDeletions} |
||
| 843 | * If a PersistentCollection has been de-referenced in a fully MANAGED entity, |
||
| 844 | * then this collection is marked for deletion. |
||
| 845 | * |
||
| 846 | * @ignore |
||
| 847 | * |
||
| 848 | * @internal Don't call from the outside. |
||
| 849 | * |
||
| 850 | * @param ApiMetadata $class The class descriptor of the entity. |
||
| 851 | * @param object $entity The entity for which to compute the changes. |
||
| 852 | * |
||
| 853 | * @return void |
||
| 854 | */ |
||
| 855 | 7 | public function computeChangeSet(ApiMetadata $class, $entity) |
|
| 856 | { |
||
| 857 | 7 | $oid = spl_object_hash($entity); |
|
| 858 | 7 | if (isset($this->readOnlyObjects[$oid])) { |
|
| 859 | return; |
||
| 860 | } |
||
| 861 | |||
| 862 | 7 | $actualData = []; |
|
| 863 | 7 | foreach ($class->getReflectionProperties() as $name => $refProp) { |
|
| 864 | 7 | $value = $refProp->getValue($entity); |
|
| 865 | 7 | if (null !== $value && $class->isCollectionValuedAssociation($name)) { |
|
| 866 | 2 | if ($value instanceof ApiCollection) { |
|
| 867 | 1 | if ($value->getOwner() === $entity) { |
|
| 868 | 1 | continue; |
|
| 869 | } |
||
| 870 | $value = new ArrayCollection($value->getValues()); |
||
| 871 | } |
||
| 872 | // If $value is not a Collection then use an ArrayCollection. |
||
| 873 | 2 | if (!$value instanceof Collection) { |
|
| 874 | $value = new ArrayCollection($value); |
||
| 875 | } |
||
| 876 | 2 | $assoc = $class->getAssociationMapping($name); |
|
| 877 | // Inject PersistentCollection |
||
| 878 | 2 | $value = new ApiCollection( |
|
| 879 | 2 | $this->manager, |
|
| 880 | 2 | $this->manager->getClassMetadata($assoc['target']), |
|
| 881 | $value |
||
| 882 | 2 | ); |
|
| 883 | 2 | $value->setOwner($entity, $assoc); |
|
| 884 | 2 | $value->setDirty(!$value->isEmpty()); |
|
| 885 | 2 | $class->getReflectionProperty($name)->setValue($entity, $value); |
|
| 886 | 2 | $actualData[$name] = $value; |
|
| 887 | 2 | continue; |
|
| 888 | } |
||
| 889 | 7 | if (!$class->isIdentifier($name)) { |
|
| 890 | 7 | $actualData[$name] = $value; |
|
| 891 | 7 | } |
|
| 892 | 7 | } |
|
| 893 | 7 | if (!isset($this->originalEntityData[$oid])) { |
|
| 894 | // Entity is either NEW or MANAGED but not yet fully persisted (only has an id). |
||
| 895 | // These result in an INSERT. |
||
| 896 | 7 | $this->originalEntityData[$oid] = (object)$actualData; |
|
| 897 | 7 | $changeSet = []; |
|
| 898 | 7 | foreach ($actualData as $propName => $actualValue) { |
|
| 899 | 7 | View Code Duplication | if (!$class->hasAssociation($propName)) { |
| 900 | 7 | $changeSet[$propName] = [null, $actualValue]; |
|
| 901 | 7 | continue; |
|
| 902 | } |
||
| 903 | 5 | $assoc = $class->getAssociationMapping($propName); |
|
| 904 | 5 | if ($assoc['isOwningSide'] && $assoc['type'] & ApiMetadata::TO_ONE) { |
|
| 905 | 5 | $changeSet[$propName] = [null, $actualValue]; |
|
| 906 | 5 | } |
|
| 907 | 7 | } |
|
| 908 | 7 | $this->entityChangeSets[$oid] = $changeSet; |
|
| 909 | 7 | } else { |
|
| 910 | |||
| 911 | // Entity is "fully" MANAGED: it was already fully persisted before |
||
| 912 | // and we have a copy of the original data |
||
| 913 | 3 | $originalData = $this->originalEntityData[$oid]; |
|
| 914 | 3 | $isChangeTrackingNotify = false; |
|
| 915 | 3 | $changeSet = ($isChangeTrackingNotify && isset($this->entityChangeSets[$oid])) |
|
| 916 | 3 | ? $this->entityChangeSets[$oid] |
|
| 917 | 3 | : []; |
|
| 918 | |||
| 919 | 3 | foreach ($actualData as $propName => $actualValue) { |
|
| 920 | // skip field, its a partially omitted one! |
||
| 921 | 3 | if (!property_exists($originalData, $propName)) { |
|
| 922 | continue; |
||
| 923 | } |
||
| 924 | 3 | $orgValue = $originalData->$propName; |
|
| 925 | // skip if value haven't changed |
||
| 926 | 3 | if ($orgValue === $actualValue) { |
|
| 927 | |||
| 928 | 3 | continue; |
|
| 929 | } |
||
| 930 | // if regular field |
||
| 931 | 2 | View Code Duplication | if (!$class->hasAssociation($propName)) { |
| 932 | 1 | if ($isChangeTrackingNotify) { |
|
| 933 | continue; |
||
| 934 | } |
||
| 935 | 1 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 936 | 1 | continue; |
|
| 937 | } |
||
| 938 | |||
| 939 | 1 | $assoc = $class->getAssociationMapping($propName); |
|
| 940 | // Persistent collection was exchanged with the "originally" |
||
| 941 | // created one. This can only mean it was cloned and replaced |
||
| 942 | // on another entity. |
||
| 943 | 1 | if ($actualValue instanceof ApiCollection) { |
|
| 944 | $owner = $actualValue->getOwner(); |
||
| 945 | if ($owner === null) { // cloned |
||
| 946 | $actualValue->setOwner($entity, $assoc); |
||
| 947 | } else { |
||
| 948 | if ($owner !== $entity) { // no clone, we have to fix |
||
| 949 | if (!$actualValue->isInitialized()) { |
||
| 950 | $actualValue->initialize(); // we have to do this otherwise the cols share state |
||
| 951 | } |
||
| 952 | $newValue = clone $actualValue; |
||
| 953 | $newValue->setOwner($entity, $assoc); |
||
| 954 | $class->getReflectionProperty($propName)->setValue($entity, $newValue); |
||
| 955 | } |
||
| 956 | } |
||
| 957 | } |
||
| 958 | 1 | if ($orgValue instanceof ApiCollection) { |
|
| 959 | // A PersistentCollection was de-referenced, so delete it. |
||
| 960 | $coid = spl_object_hash($orgValue); |
||
| 961 | if (isset($this->collectionDeletions[$coid])) { |
||
| 962 | continue; |
||
| 963 | } |
||
| 964 | $this->collectionDeletions[$coid] = $orgValue; |
||
| 965 | $changeSet[$propName] = $orgValue; // Signal changeset, to-many assocs will be ignored. |
||
| 966 | continue; |
||
| 967 | } |
||
| 968 | 1 | if ($assoc['type'] & ApiMetadata::TO_ONE) { |
|
| 969 | 1 | if ($assoc['isOwningSide']) { |
|
| 970 | 1 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 971 | 1 | } |
|
| 972 | 1 | if ($orgValue !== null && $assoc['orphanRemoval']) { |
|
| 973 | $this->scheduleOrphanRemoval($orgValue); |
||
| 974 | } |
||
| 975 | 1 | } |
|
| 976 | 3 | } |
|
| 977 | 3 | if ($changeSet) { |
|
| 978 | 2 | $this->entityChangeSets[$oid] = $changeSet; |
|
| 979 | 2 | $this->originalEntityData[$oid] = (object)$actualData; |
|
| 980 | 2 | $this->entityUpdates[$oid] = $entity; |
|
| 981 | 2 | } |
|
| 982 | } |
||
| 983 | // Look for changes in associations of the entity |
||
| 984 | 7 | foreach ($class->getAssociationMappings() as $field => $assoc) { |
|
| 985 | 5 | if (($val = $class->getReflectionProperty($field)->getValue($entity)) === null) { |
|
| 986 | 5 | continue; |
|
| 987 | } |
||
| 988 | 2 | $this->computeAssociationChanges($assoc, $val); |
|
| 989 | 2 | if (!isset($this->entityChangeSets[$oid]) && |
|
| 990 | 2 | $assoc['isOwningSide'] && |
|
| 991 | 2 | $assoc['type'] == ApiMetadata::MANY_TO_MANY && |
|
| 992 | 2 | $val instanceof ApiCollection && |
|
| 993 | $val->isDirty() |
||
| 994 | 2 | ) { |
|
| 995 | $this->entityChangeSets[$oid] = []; |
||
| 996 | $this->originalEntityData[$oid] = (object)$actualData; |
||
| 997 | $this->entityUpdates[$oid] = $entity; |
||
| 998 | } |
||
| 999 | 7 | } |
|
| 1000 | 7 | } |
|
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Computes all the changes that have been done to entities and collections |
||
| 1004 | * since the last commit and stores these changes in the _entityChangeSet map |
||
| 1005 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 1006 | * |
||
| 1007 | * @return void |
||
| 1008 | */ |
||
| 1009 | 7 | public function computeChangeSets() |
|
| 1010 | { |
||
| 1011 | // Compute changes for INSERTed entities first. This must always happen. |
||
| 1012 | 7 | $this->computeScheduleInsertsChangeSets(); |
|
| 1013 | // Compute changes for other MANAGED entities. Change tracking policies take effect here. |
||
| 1014 | 7 | foreach ($this->identityMap as $className => $entities) { |
|
| 1015 | 3 | $class = $this->manager->getClassMetadata($className); |
|
| 1016 | // Skip class if instances are read-only |
||
| 1017 | 3 | if ($class->isReadOnly()) { |
|
| 1018 | continue; |
||
| 1019 | } |
||
| 1020 | // If change tracking is explicit or happens through notification, then only compute |
||
| 1021 | // changes on entities of that type that are explicitly marked for synchronization. |
||
| 1022 | 3 | switch (true) { |
|
| 1023 | 3 | case ($class->isChangeTrackingDeferredImplicit()): |
|
| 1024 | 3 | $entitiesToProcess = $entities; |
|
| 1025 | 3 | break; |
|
| 1026 | case (isset($this->scheduledForSynchronization[$className])): |
||
| 1027 | $entitiesToProcess = $this->scheduledForSynchronization[$className]; |
||
| 1028 | break; |
||
| 1029 | default: |
||
| 1030 | $entitiesToProcess = []; |
||
| 1031 | } |
||
| 1032 | 3 | foreach ($entitiesToProcess as $entity) { |
|
| 1033 | // Ignore uninitialized proxy objects |
||
| 1034 | 3 | if ($entity instanceof Proxy && !$entity->__isInitialized__) { |
|
| 1035 | continue; |
||
| 1036 | } |
||
| 1037 | // Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here. |
||
| 1038 | 3 | $oid = spl_object_hash($entity); |
|
| 1039 | 3 | View Code Duplication | if (!isset($this->entityInsertions[$oid]) && |
| 1040 | 3 | !isset($this->entityDeletions[$oid]) && |
|
| 1041 | 3 | isset($this->entityStates[$oid]) |
|
| 1042 | 3 | ) { |
|
| 1043 | 3 | $this->computeChangeSet($class, $entity); |
|
| 1044 | 3 | } |
|
| 1045 | 3 | } |
|
| 1046 | 7 | } |
|
| 1047 | 7 | } |
|
| 1048 | |||
| 1049 | /** |
||
| 1050 | * INTERNAL: |
||
| 1051 | * Schedules an orphaned entity for removal. The remove() operation will be |
||
| 1052 | * invoked on that entity at the beginning of the next commit of this |
||
| 1053 | * UnitOfWork. |
||
| 1054 | * |
||
| 1055 | * @ignore |
||
| 1056 | * |
||
| 1057 | * @param object $entity |
||
| 1058 | * |
||
| 1059 | * @return void |
||
| 1060 | */ |
||
| 1061 | public function scheduleOrphanRemoval($entity) |
||
| 1062 | { |
||
| 1063 | $this->orphanRemovals[spl_object_hash($entity)] = $entity; |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | 2 | public function loadCollection(ApiCollection $collection) |
|
| 1067 | { |
||
| 1068 | 2 | $assoc = $collection->getMapping(); |
|
| 1069 | 2 | $persister = $this->getEntityPersister($assoc['target']); |
|
| 1070 | 2 | switch ($assoc['type']) { |
|
| 1071 | 2 | case ApiMetadata::ONE_TO_MANY: |
|
| 1072 | 2 | $persister->loadOneToManyCollection($assoc, $collection->getOwner(), $collection); |
|
| 1073 | 2 | break; |
|
| 1074 | 2 | } |
|
| 1075 | 2 | $collection->setInitialized(true); |
|
| 1076 | 2 | } |
|
| 1077 | |||
| 1078 | 1 | public function getCollectionPersister($association) |
|
| 1079 | { |
||
| 1080 | 1 | $targetMetadata = $this->manager->getClassMetadata($association['target']); |
|
| 1081 | 1 | $role = $association['sourceEntity'] . '::' . $association['field']; |
|
| 1082 | |||
| 1083 | 1 | if (!array_key_exists($role, $this->collectionPersisters)) { |
|
| 1084 | 1 | $this->collectionPersisters[$role] = new CollectionPersister( |
|
| 1085 | 1 | $targetMetadata, |
|
| 1086 | 1 | new CollectionMatcher($this->manager, $this->getCrudsApi($targetMetadata)), |
|
| 1087 | $association |
||
| 1088 | 1 | ); |
|
| 1089 | 1 | } |
|
| 1090 | |||
| 1091 | 1 | return $this->collectionPersisters[$role]; |
|
| 1092 | } |
||
| 1093 | |||
| 1094 | public function scheduleCollectionDeletion(Collection $collection) |
||
| 1095 | { |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | 2 | public function cancelOrphanRemoval($value) |
|
| 1099 | { |
||
| 1100 | 2 | } |
|
| 1101 | |||
| 1102 | /** |
||
| 1103 | * INTERNAL: |
||
| 1104 | * Sets a property value of the original data array of an entity. |
||
| 1105 | * |
||
| 1106 | * @ignore |
||
| 1107 | * |
||
| 1108 | * @param string $oid |
||
| 1109 | * @param string $property |
||
| 1110 | * @param mixed $value |
||
| 1111 | * |
||
| 1112 | * @return void |
||
| 1113 | */ |
||
| 1114 | 11 | public function setOriginalEntityProperty($oid, $property, $value) |
|
| 1115 | { |
||
| 1116 | 11 | if (!array_key_exists($oid, $this->originalEntityData)) { |
|
| 1117 | 11 | $this->originalEntityData[$oid] = new \stdClass(); |
|
| 1118 | 11 | } |
|
| 1119 | |||
| 1120 | 11 | $this->originalEntityData[$oid]->$property = $value; |
|
| 1121 | 11 | } |
|
| 1122 | |||
| 1123 | public function scheduleExtraUpdate($entity, $changeset) |
||
| 1124 | { |
||
| 1125 | $oid = spl_object_hash($entity); |
||
| 1126 | $extraUpdate = [$entity, $changeset]; |
||
| 1127 | if (isset($this->extraUpdates[$oid])) { |
||
| 1128 | list(, $changeset2) = $this->extraUpdates[$oid]; |
||
| 1129 | $extraUpdate = [$entity, $changeset + $changeset2]; |
||
| 1130 | } |
||
| 1131 | $this->extraUpdates[$oid] = $extraUpdate; |
||
| 1132 | } |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Refreshes the state of the given entity from the database, overwriting |
||
| 1136 | * any local, unpersisted changes. |
||
| 1137 | * |
||
| 1138 | * @param object $entity The entity to refresh. |
||
| 1139 | * |
||
| 1140 | * @return void |
||
| 1141 | * |
||
| 1142 | * @throws InvalidArgumentException If the entity is not MANAGED. |
||
| 1143 | */ |
||
| 1144 | public function refresh($entity) |
||
| 1145 | { |
||
| 1146 | $visited = []; |
||
| 1147 | $this->doRefresh($entity, $visited); |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Clears the UnitOfWork. |
||
| 1152 | * |
||
| 1153 | * @param string|null $entityName if given, only entities of this type will get detached. |
||
| 1154 | * |
||
| 1155 | * @return void |
||
| 1156 | */ |
||
| 1157 | public function clear($entityName = null) |
||
| 1158 | { |
||
| 1159 | if ($entityName === null) { |
||
| 1160 | $this->identityMap = |
||
| 1161 | $this->entityIdentifiers = |
||
| 1162 | $this->originalEntityData = |
||
| 1163 | $this->entityChangeSets = |
||
| 1164 | $this->entityStates = |
||
| 1165 | $this->scheduledForSynchronization = |
||
| 1166 | $this->entityInsertions = |
||
| 1167 | $this->entityUpdates = |
||
| 1168 | $this->entityDeletions = |
||
| 1169 | $this->collectionDeletions = |
||
| 1170 | $this->collectionUpdates = |
||
| 1171 | $this->extraUpdates = |
||
| 1172 | $this->readOnlyObjects = |
||
| 1173 | $this->visitedCollections = |
||
| 1174 | $this->orphanRemovals = []; |
||
| 1175 | } else { |
||
| 1176 | $this->clearIdentityMapForEntityName($entityName); |
||
| 1177 | $this->clearEntityInsertionsForEntityName($entityName); |
||
| 1178 | } |
||
| 1179 | } |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * @param PersistentCollection $coll |
||
| 1183 | * |
||
| 1184 | * @return bool |
||
| 1185 | */ |
||
| 1186 | public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Schedules an entity for dirty-checking at commit-time. |
||
| 1193 | * |
||
| 1194 | * @param object $entity The entity to schedule for dirty-checking. |
||
| 1195 | * |
||
| 1196 | * @return void |
||
| 1197 | * |
||
| 1198 | * @todo Rename: scheduleForSynchronization |
||
| 1199 | */ |
||
| 1200 | public function scheduleForDirtyCheck($entity) |
||
| 1201 | { |
||
| 1202 | $rootClassName = |
||
| 1203 | $this->manager->getClassMetadata(get_class($entity))->getRootEntityName(); |
||
| 1204 | $this->scheduledForSynchronization[$rootClassName][spl_object_hash($entity)] = $entity; |
||
| 1205 | } |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Deletes an entity as part of the current unit of work. |
||
| 1209 | * |
||
| 1210 | * @param object $entity The entity to remove. |
||
| 1211 | * |
||
| 1212 | * @return void |
||
| 1213 | */ |
||
| 1214 | public function remove($entity) |
||
| 1215 | { |
||
| 1216 | $visited = []; |
||
| 1217 | $this->doRemove($entity, $visited); |
||
| 1218 | } |
||
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Merges the state of the given detached entity into this UnitOfWork. |
||
| 1222 | * |
||
| 1223 | * @param object $entity |
||
| 1224 | * |
||
| 1225 | * @return object The managed copy of the entity. |
||
| 1226 | */ |
||
| 1227 | public function merge($entity) |
||
| 1228 | { |
||
| 1229 | $visited = []; |
||
| 1230 | |||
| 1231 | return $this->doMerge($entity, $visited); |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Detaches an entity from the persistence management. It's persistence will |
||
| 1236 | * no longer be managed by Doctrine. |
||
| 1237 | * |
||
| 1238 | * @param object $entity The entity to detach. |
||
| 1239 | * |
||
| 1240 | * @return void |
||
| 1241 | */ |
||
| 1242 | public function detach($entity) |
||
| 1243 | { |
||
| 1244 | $visited = []; |
||
| 1245 | $this->doDetach($entity, $visited); |
||
| 1246 | } |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Resolve metadata against source data and root class |
||
| 1250 | * |
||
| 1251 | * @param \stdClass $data |
||
| 1252 | * @param string $class |
||
| 1253 | * |
||
| 1254 | * @return ApiMetadata |
||
| 1255 | * @throws MappingException |
||
| 1256 | */ |
||
| 1257 | 18 | private function resolveSourceMetadataForClass(\stdClass $data, $class) |
|
| 1258 | { |
||
| 1259 | 18 | $metadata = $this->manager->getClassMetadata($class); |
|
| 1260 | 18 | $discriminatorValue = $metadata->getDiscriminatorValue(); |
|
| 1261 | 18 | if ($metadata->getDiscriminatorField()) { |
|
| 1262 | 2 | $property = $metadata->getDiscriminatorField()['fieldName']; |
|
| 1263 | 2 | if (isset($data->$property)) { |
|
| 1264 | 2 | $discriminatorValue = $data->$property; |
|
| 1265 | 2 | } |
|
| 1266 | 2 | } |
|
| 1267 | |||
| 1268 | 18 | $map = $metadata->getDiscriminatorMap(); |
|
| 1269 | |||
| 1270 | 18 | if (!array_key_exists($discriminatorValue, $map)) { |
|
| 1271 | throw MappingException::unknownDiscriminatorValue($discriminatorValue, $class); |
||
| 1272 | } |
||
| 1273 | |||
| 1274 | 18 | $realClass = $map[$discriminatorValue]; |
|
| 1275 | |||
| 1276 | 18 | return $this->manager->getClassMetadata($realClass); |
|
| 1277 | } |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * Helper method to show an object as string. |
||
| 1281 | * |
||
| 1282 | * @param object $obj |
||
| 1283 | * |
||
| 1284 | * @return string |
||
| 1285 | */ |
||
| 1286 | private static function objToStr($obj) |
||
| 1287 | { |
||
| 1288 | return method_exists($obj, '__toString') ? (string)$obj : get_class($obj) . '@' . spl_object_hash($obj); |
||
| 1289 | } |
||
| 1290 | |||
| 1291 | /** |
||
| 1292 | * @param ApiMetadata $class |
||
| 1293 | * |
||
| 1294 | * @return \Doctrine\Common\Persistence\ObjectManagerAware|object |
||
| 1295 | */ |
||
| 1296 | 17 | private function newInstance(ApiMetadata $class) |
|
| 1297 | { |
||
| 1298 | 17 | $entity = $class->newInstance(); |
|
| 1299 | |||
| 1300 | 17 | if ($entity instanceof ObjectManagerAware) { |
|
| 1301 | $entity->injectObjectManager($this->manager, $class); |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | 17 | return $entity; |
|
| 1305 | } |
||
| 1306 | |||
| 1307 | /** |
||
| 1308 | * @param ApiMetadata $classMetadata |
||
| 1309 | * |
||
| 1310 | * @return EntityDataCacheInterface |
||
| 1311 | */ |
||
| 1312 | 26 | private function createEntityCache(ApiMetadata $classMetadata) |
|
| 1313 | { |
||
| 1314 | 26 | $configuration = $this->manager->getConfiguration()->getCacheConfiguration($classMetadata->getName()); |
|
| 1315 | 26 | $cache = new VoidEntityCache($classMetadata); |
|
| 1316 | 26 | if ($configuration->isEnabled() && $this->manager->getConfiguration()->getApiCache()) { |
|
| 1317 | $cache = |
||
| 1318 | 1 | new LoggingCache( |
|
| 1319 | 1 | new ApiEntityCache( |
|
| 1320 | 1 | $this->manager->getConfiguration()->getApiCache(), |
|
| 1321 | 1 | $classMetadata, |
|
| 1322 | $configuration |
||
| 1323 | 1 | ), |
|
| 1324 | 1 | $this->manager->getConfiguration()->getApiCacheLogger() |
|
| 1325 | 1 | ); |
|
| 1326 | |||
| 1327 | 1 | return $cache; |
|
| 1328 | } |
||
| 1329 | |||
| 1330 | 25 | return $cache; |
|
| 1331 | } |
||
| 1332 | |||
| 1333 | /** |
||
| 1334 | * @param ApiMetadata $classMetadata |
||
| 1335 | * |
||
| 1336 | * @return CrudsApiInterface |
||
| 1337 | */ |
||
| 1338 | 26 | private function getCrudsApi(ApiMetadata $classMetadata) |
|
| 1339 | { |
||
| 1340 | 26 | if (!array_key_exists($classMetadata->getName(), $this->apis)) { |
|
| 1341 | 26 | $client = $this->manager->getConfiguration()->getClientRegistry()->get($classMetadata->getClientName()); |
|
| 1342 | |||
| 1343 | 26 | $api = $this->manager |
|
| 1344 | 26 | ->getConfiguration() |
|
| 1345 | 26 | ->getFactoryRegistry() |
|
| 1346 | 26 | ->create( |
|
| 1347 | 26 | $classMetadata->getApiFactory(), |
|
| 1348 | 26 | $client, |
|
| 1349 | $classMetadata |
||
| 1350 | 26 | ); |
|
| 1351 | |||
| 1352 | 26 | $this->apis[$classMetadata->getName()] = $api; |
|
| 1353 | 26 | } |
|
| 1354 | |||
| 1355 | 26 | return $this->apis[$classMetadata->getName()]; |
|
| 1356 | } |
||
| 1357 | |||
| 1358 | 7 | private function doPersist($entity, $visited) |
|
| 1359 | { |
||
| 1360 | 7 | $oid = spl_object_hash($entity); |
|
| 1361 | 7 | if (isset($visited[$oid])) { |
|
| 1362 | return; // Prevent infinite recursion |
||
| 1363 | } |
||
| 1364 | 7 | $visited[$oid] = $entity; // Mark visited |
|
| 1365 | 7 | $class = $this->manager->getClassMetadata(get_class($entity)); |
|
| 1366 | // We assume NEW, so DETACHED entities result in an exception on flush (constraint violation). |
||
| 1367 | // If we would detect DETACHED here we would throw an exception anyway with the same |
||
| 1368 | // consequences (not recoverable/programming error), so just assuming NEW here |
||
| 1369 | // lets us avoid some database lookups for entities with natural identifiers. |
||
| 1370 | 7 | $entityState = $this->getEntityState($entity, self::STATE_NEW); |
|
| 1371 | switch ($entityState) { |
||
| 1372 | 7 | case self::STATE_MANAGED: |
|
| 1373 | $this->scheduleForDirtyCheck($entity); |
||
| 1374 | break; |
||
| 1375 | 7 | case self::STATE_NEW: |
|
| 1376 | 7 | $this->persistNew($class, $entity); |
|
| 1377 | 7 | break; |
|
| 1378 | case self::STATE_REMOVED: |
||
| 1379 | // Entity becomes managed again |
||
| 1380 | unset($this->entityDeletions[$oid]); |
||
| 1381 | $this->addToIdentityMap($entity); |
||
| 1382 | $this->entityStates[$oid] = self::STATE_MANAGED; |
||
| 1383 | break; |
||
| 1384 | case self::STATE_DETACHED: |
||
| 1385 | // Can actually not happen right now since we assume STATE_NEW. |
||
| 1386 | throw new \InvalidArgumentException('Detached entity cannot be persisted'); |
||
| 1387 | default: |
||
| 1388 | throw new \UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity)); |
||
| 1389 | } |
||
| 1390 | 7 | $this->cascadePersist($entity, $visited); |
|
| 1391 | 7 | } |
|
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Cascades the save operation to associated entities. |
||
| 1395 | * |
||
| 1396 | * @param object $entity |
||
| 1397 | * @param array $visited |
||
| 1398 | * |
||
| 1399 | * @return void |
||
| 1400 | * @throws \InvalidArgumentException |
||
| 1401 | * @throws MappingException |
||
| 1402 | */ |
||
| 1403 | 7 | private function cascadePersist($entity, array &$visited) |
|
| 1404 | { |
||
| 1405 | 7 | $class = $this->manager->getClassMetadata(get_class($entity)); |
|
| 1406 | 7 | $associationMappings = []; |
|
| 1407 | 7 | foreach ($class->getAssociationNames() as $name) { |
|
| 1408 | 5 | $assoc = $class->getAssociationMapping($name); |
|
| 1409 | 5 | if ($assoc['isCascadePersist']) { |
|
| 1410 | $associationMappings[$name] = $assoc; |
||
| 1411 | } |
||
| 1412 | 7 | } |
|
| 1413 | 7 | foreach ($associationMappings as $assoc) { |
|
| 1414 | $relatedEntities = $class->getReflectionProperty($assoc['field'])->getValue($entity); |
||
| 1415 | switch (true) { |
||
| 1416 | case ($relatedEntities instanceof ApiCollection): |
||
| 1417 | // Unwrap so that foreach() does not initialize |
||
| 1418 | $relatedEntities = $relatedEntities->unwrap(); |
||
| 1419 | // break; is commented intentionally! |
||
| 1420 | case ($relatedEntities instanceof Collection): |
||
| 1421 | case (is_array($relatedEntities)): |
||
| 1422 | if (($assoc['type'] & ApiMetadata::TO_MANY) <= 0) { |
||
| 1423 | throw new \InvalidArgumentException('Invalid association for cascade'); |
||
| 1424 | } |
||
| 1425 | foreach ($relatedEntities as $relatedEntity) { |
||
| 1426 | $this->doPersist($relatedEntity, $visited); |
||
| 1427 | } |
||
| 1428 | break; |
||
| 1429 | case ($relatedEntities !== null): |
||
| 1430 | if (!$relatedEntities instanceof $assoc['target']) { |
||
| 1431 | throw new \InvalidArgumentException('Invalid association for cascade'); |
||
| 1432 | } |
||
| 1433 | $this->doPersist($relatedEntities, $visited); |
||
| 1434 | break; |
||
| 1435 | default: |
||
| 1436 | // Do nothing |
||
| 1437 | } |
||
| 1438 | 7 | } |
|
| 1439 | 7 | } |
|
| 1440 | |||
| 1441 | /** |
||
| 1442 | * @param ApiMetadata $class |
||
| 1443 | * @param object $entity |
||
| 1444 | * |
||
| 1445 | * @return void |
||
| 1446 | */ |
||
| 1447 | 7 | private function persistNew($class, $entity) |
|
| 1448 | { |
||
| 1449 | 7 | $oid = spl_object_hash($entity); |
|
| 1450 | // $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::prePersist); |
||
| 1451 | // if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
||
| 1452 | // $this->listenersInvoker->invoke($class, Events::prePersist, $entity, new LifecycleEventArgs($entity, $this->em), $invoke); |
||
| 1453 | // } |
||
| 1454 | // $idGen = $class->idGenerator; |
||
| 1455 | // if ( ! $idGen->isPostInsertGenerator()) { |
||
| 1456 | // $idValue = $idGen->generate($this->em, $entity); |
||
| 1457 | // if ( ! $idGen instanceof \Doctrine\ORM\Id\AssignedGenerator) { |
||
| 1458 | // $idValue = array($class->identifier[0] => $idValue); |
||
| 1459 | // $class->setIdentifierValues($entity, $idValue); |
||
| 1460 | // } |
||
| 1461 | // $this->entityIdentifiers[$oid] = $idValue; |
||
| 1462 | // } |
||
| 1463 | 7 | $this->entityStates[$oid] = self::STATE_MANAGED; |
|
| 1464 | 7 | $this->scheduleForInsert($entity); |
|
| 1465 | 7 | } |
|
| 1466 | |||
| 1467 | /** |
||
| 1468 | * Gets the commit order. |
||
| 1469 | * |
||
| 1470 | * @param array|null $entityChangeSet |
||
| 1471 | * |
||
| 1472 | * @return array |
||
| 1473 | */ |
||
| 1474 | 7 | private function getCommitOrder(array $entityChangeSet = null) |
|
| 1475 | { |
||
| 1476 | 7 | if ($entityChangeSet === null) { |
|
| 1477 | 7 | $entityChangeSet = array_merge($this->entityInsertions, $this->entityUpdates, $this->entityDeletions); |
|
| 1478 | 7 | } |
|
| 1479 | 7 | $calc = $this->getCommitOrderCalculator(); |
|
| 1480 | // See if there are any new classes in the changeset, that are not in the |
||
| 1481 | // commit order graph yet (don't have a node). |
||
| 1482 | // We have to inspect changeSet to be able to correctly build dependencies. |
||
| 1483 | // It is not possible to use IdentityMap here because post inserted ids |
||
| 1484 | // are not yet available. |
||
| 1485 | /** @var ApiMetadata[] $newNodes */ |
||
| 1486 | 7 | $newNodes = []; |
|
| 1487 | 7 | foreach ((array)$entityChangeSet as $entity) { |
|
| 1488 | 7 | $class = $this->manager->getClassMetadata(get_class($entity)); |
|
| 1489 | 7 | if ($calc->hasNode($class->getName())) { |
|
| 1490 | continue; |
||
| 1491 | } |
||
| 1492 | 7 | $calc->addNode($class->getName(), $class); |
|
| 1493 | 7 | $newNodes[] = $class; |
|
| 1494 | 7 | } |
|
| 1495 | // Calculate dependencies for new nodes |
||
| 1496 | 7 | while ($class = array_pop($newNodes)) { |
|
| 1497 | 7 | foreach ($class->getAssociationMappings() as $assoc) { |
|
| 1498 | 5 | if (!($assoc['isOwningSide'] && $assoc['type'] & ApiMetadata::TO_ONE)) { |
|
| 1499 | 5 | continue; |
|
| 1500 | } |
||
| 1501 | 5 | $targetClass = $this->manager->getClassMetadata($assoc['target']); |
|
| 1502 | 5 | if (!$calc->hasNode($targetClass->getName())) { |
|
| 1503 | 3 | $calc->addNode($targetClass->getName(), $targetClass); |
|
| 1504 | 3 | $newNodes[] = $targetClass; |
|
| 1505 | 3 | } |
|
| 1506 | 5 | $calc->addDependency($targetClass->getName(), $class->name, (int)empty($assoc['nullable'])); |
|
| 1507 | // If the target class has mapped subclasses, these share the same dependency. |
||
| 1508 | 5 | if (!$targetClass->getSubclasses()) { |
|
| 1509 | continue; |
||
| 1510 | } |
||
| 1511 | 5 | foreach ($targetClass->getSubclasses() as $subClassName) { |
|
| 1512 | 5 | $targetSubClass = $this->manager->getClassMetadata($subClassName); |
|
| 1513 | 5 | if (!$calc->hasNode($subClassName)) { |
|
| 1514 | 5 | $calc->addNode($targetSubClass->name, $targetSubClass); |
|
| 1515 | 5 | $newNodes[] = $targetSubClass; |
|
| 1516 | 5 | } |
|
| 1517 | 5 | $calc->addDependency($targetSubClass->name, $class->name, 1); |
|
| 1518 | 5 | } |
|
| 1519 | 7 | } |
|
| 1520 | 7 | } |
|
| 1521 | |||
| 1522 | 7 | return $calc->sort(); |
|
| 1523 | } |
||
| 1524 | |||
| 1525 | 7 | private function getCommitOrderCalculator() |
|
| 1526 | { |
||
| 1527 | 7 | return new Utility\CommitOrderCalculator(); |
|
| 1528 | } |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Only flushes the given entity according to a ruleset that keeps the UoW consistent. |
||
| 1532 | * |
||
| 1533 | * 1. All entities scheduled for insertion, (orphan) removals and changes in collections are processed as well! |
||
| 1534 | * 2. Read Only entities are skipped. |
||
| 1535 | * 3. Proxies are skipped. |
||
| 1536 | * 4. Only if entity is properly managed. |
||
| 1537 | * |
||
| 1538 | * @param object $entity |
||
| 1539 | * |
||
| 1540 | * @return void |
||
| 1541 | * |
||
| 1542 | * @throws \InvalidArgumentException |
||
| 1543 | */ |
||
| 1544 | private function computeSingleEntityChangeSet($entity) |
||
| 1545 | { |
||
| 1546 | $state = $this->getEntityState($entity); |
||
| 1547 | if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { |
||
| 1548 | throw new \InvalidArgumentException( |
||
| 1549 | "Entity has to be managed or scheduled for removal for single computation " . self::objToStr($entity) |
||
| 1550 | ); |
||
| 1551 | } |
||
| 1552 | $class = $this->manager->getClassMetadata(get_class($entity)); |
||
| 1553 | // Compute changes for INSERTed entities first. This must always happen even in this case. |
||
| 1554 | $this->computeScheduleInsertsChangeSets(); |
||
| 1555 | if ($class->isReadOnly()) { |
||
| 1556 | return; |
||
| 1557 | } |
||
| 1558 | // Ignore uninitialized proxy objects |
||
| 1559 | if ($entity instanceof Proxy && !$entity->__isInitialized__) { |
||
| 1560 | return; |
||
| 1561 | } |
||
| 1562 | // Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here. |
||
| 1563 | $oid = spl_object_hash($entity); |
||
| 1564 | View Code Duplication | if (!isset($this->entityInsertions[$oid]) && |
|
| 1565 | !isset($this->entityDeletions[$oid]) && |
||
| 1566 | isset($this->entityStates[$oid]) |
||
| 1567 | ) { |
||
| 1568 | $this->computeChangeSet($class, $entity); |
||
| 1569 | } |
||
| 1570 | } |
||
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Computes the changesets of all entities scheduled for insertion. |
||
| 1574 | * |
||
| 1575 | * @return void |
||
| 1576 | */ |
||
| 1577 | 7 | private function computeScheduleInsertsChangeSets() |
|
| 1578 | { |
||
| 1579 | 7 | foreach ($this->entityInsertions as $entity) { |
|
| 1580 | 7 | $class = $this->manager->getClassMetadata(get_class($entity)); |
|
| 1581 | 7 | $this->computeChangeSet($class, $entity); |
|
| 1582 | 7 | } |
|
| 1583 | 7 | } |
|
| 1584 | |||
| 1585 | /** |
||
| 1586 | * Computes the changes of an association. |
||
| 1587 | * |
||
| 1588 | * @param array $assoc The association mapping. |
||
| 1589 | * @param mixed $value The value of the association. |
||
| 1590 | * |
||
| 1591 | * @throws \InvalidArgumentException |
||
| 1592 | * @throws \UnexpectedValueException |
||
| 1593 | * |
||
| 1594 | * @return void |
||
| 1595 | */ |
||
| 1596 | 2 | private function computeAssociationChanges($assoc, $value) |
|
| 1597 | { |
||
| 1598 | 2 | if ($value instanceof Proxy && !$value->__isInitialized__) { |
|
| 1599 | return; |
||
| 1600 | } |
||
| 1601 | 2 | if ($value instanceof ApiCollection && $value->isDirty()) { |
|
| 1602 | 2 | $coid = spl_object_hash($value); |
|
| 1603 | 2 | $this->collectionUpdates[$coid] = $value; |
|
| 1604 | 2 | $this->visitedCollections[$coid] = $value; |
|
| 1605 | 2 | } |
|
| 1606 | // Look through the entities, and in any of their associations, |
||
| 1607 | // for transient (new) entities, recursively. ("Persistence by reachability") |
||
| 1608 | // Unwrap. Uninitialized collections will simply be empty. |
||
| 1609 | 2 | $unwrappedValue = ($assoc['type'] & ApiMetadata::TO_ONE) ? [$value] : $value->unwrap(); |
|
| 1610 | 2 | $targetClass = $this->manager->getClassMetadata($assoc['target']); |
|
| 1611 | 2 | $targetClassName = $targetClass->getName(); |
|
| 1612 | 2 | foreach ($unwrappedValue as $key => $entry) { |
|
| 1613 | 2 | if (!($entry instanceof $targetClassName)) { |
|
| 1614 | throw new \InvalidArgumentException('Invalid association'); |
||
| 1615 | } |
||
| 1616 | 2 | $state = $this->getEntityState($entry, self::STATE_NEW); |
|
| 1617 | 2 | if (!($entry instanceof $assoc['target'])) { |
|
| 1618 | throw new \UnexpectedValueException('Unexpected association'); |
||
| 1619 | } |
||
| 1620 | switch ($state) { |
||
| 1621 | 2 | case self::STATE_NEW: |
|
| 1622 | if (!$assoc['isCascadePersist']) { |
||
| 1623 | throw new \InvalidArgumentException('New entity through relationship'); |
||
| 1624 | } |
||
| 1625 | $this->persistNew($targetClass, $entry); |
||
| 1626 | $this->computeChangeSet($targetClass, $entry); |
||
| 1627 | break; |
||
| 1628 | 2 | case self::STATE_REMOVED: |
|
| 1629 | // Consume the $value as array (it's either an array or an ArrayAccess) |
||
| 1630 | // and remove the element from Collection. |
||
| 1631 | if ($assoc['type'] & ApiMetadata::TO_MANY) { |
||
| 1632 | unset($value[$key]); |
||
| 1633 | } |
||
| 1634 | break; |
||
| 1635 | 2 | case self::STATE_DETACHED: |
|
| 1636 | // Can actually not happen right now as we assume STATE_NEW, |
||
| 1637 | // so the exception will be raised from the DBAL layer (constraint violation). |
||
| 1638 | throw new \InvalidArgumentException('Detached entity through relationship'); |
||
| 1639 | break; |
||
| 1640 | 2 | default: |
|
| 1641 | // MANAGED associated entities are already taken into account |
||
| 1642 | // during changeset calculation anyway, since they are in the identity map. |
||
| 1643 | 2 | } |
|
| 1644 | 2 | } |
|
| 1645 | 2 | } |
|
| 1646 | |||
| 1647 | 7 | private function executeInserts(ApiMetadata $class) |
|
| 1648 | { |
||
| 1649 | 7 | $className = $class->getName(); |
|
| 1650 | 7 | $persister = $this->getEntityPersister($className); |
|
| 1651 | 7 | foreach ($this->entityInsertions as $oid => $entity) { |
|
| 1652 | 7 | if ($this->manager->getClassMetadata(get_class($entity))->getName() !== $className) { |
|
| 1653 | 5 | continue; |
|
| 1654 | } |
||
| 1655 | 7 | $persister->pushNewEntity($entity); |
|
| 1656 | 7 | unset($this->entityInsertions[$oid]); |
|
| 1657 | 7 | } |
|
| 1658 | 7 | $postInsertIds = $persister->flushNewEntities(); |
|
| 1659 | 7 | if ($postInsertIds) { |
|
| 1660 | // Persister returned post-insert IDs |
||
| 1661 | 7 | foreach ($postInsertIds as $postInsertId) { |
|
| 1662 | 7 | $id = $postInsertId['generatedId']; |
|
| 1663 | 7 | $entity = $postInsertId['entity']; |
|
| 1664 | 7 | $oid = spl_object_hash($entity); |
|
| 1665 | |||
| 1666 | 7 | if ($id instanceof \stdClass) { |
|
| 1667 | 5 | $id = (array)$id; |
|
| 1668 | 5 | } |
|
| 1669 | 7 | if (!is_array($id)) { |
|
| 1670 | 2 | $id = [$class->getApiFieldName($class->getIdentifierFieldNames()[0]) => $id]; |
|
| 1671 | 2 | } |
|
| 1672 | |||
| 1673 | 7 | if (!array_key_exists($oid, $this->originalEntityData)) { |
|
| 1674 | $this->originalEntityData[$oid] = new \stdClass(); |
||
| 1675 | } |
||
| 1676 | |||
| 1677 | 7 | $idValues = []; |
|
| 1678 | 7 | foreach ((array)$id as $apiIdField => $idValue) { |
|
| 1679 | 7 | $idName = $class->getFieldName($apiIdField); |
|
| 1680 | 7 | $typeName = $class->getTypeOfField($idName); |
|
| 1681 | 7 | $type = $this->manager->getConfiguration()->getTypeRegistry()->get($typeName); |
|
| 1682 | 7 | $idValue = $type->toApiValue($idValue, $class->getFieldOptions($idName)); |
|
| 1683 | 7 | $class->getReflectionProperty($idName)->setValue($entity, $idValue); |
|
| 1684 | 7 | $idValues[$idName] = $idValue; |
|
| 1685 | 7 | $this->originalEntityData[$oid]->$idName = $idValue; |
|
| 1686 | 7 | } |
|
| 1687 | |||
| 1688 | 7 | $this->entityIdentifiers[$oid] = $idValues; |
|
| 1689 | 7 | $this->entityStates[$oid] = self::STATE_MANAGED; |
|
| 1690 | 7 | $this->addToIdentityMap($entity); |
|
| 1691 | 7 | } |
|
| 1692 | 7 | } |
|
| 1693 | 7 | } |
|
| 1694 | |||
| 1695 | 2 | private function executeUpdates($class) |
|
| 1711 | |||
| 1712 | /** |
||
| 1713 | * Executes a refresh operation on an entity. |
||
| 1714 | * |
||
| 1715 | * @param object $entity The entity to refresh. |
||
| 1716 | * @param array $visited The already visited entities during cascades. |
||
| 1717 | * |
||
| 1718 | * @return void |
||
| 1719 | * |
||
| 1720 | * @throws \InvalidArgumentException If the entity is not MANAGED. |
||
| 1721 | */ |
||
| 1722 | private function doRefresh($entity, array &$visited) |
||
| 1723 | { |
||
| 1724 | $oid = spl_object_hash($entity); |
||
| 1725 | if (isset($visited[$oid])) { |
||
| 1726 | return; // Prevent infinite recursion |
||
| 1727 | } |
||
| 1728 | $visited[$oid] = $entity; // mark visited |
||
| 1729 | $class = $this->manager->getClassMetadata(get_class($entity)); |
||
| 1730 | if ($this->getEntityState($entity) !== self::STATE_MANAGED) { |
||
| 1731 | throw new \InvalidArgumentException('Entity not managed'); |
||
| 1732 | } |
||
| 1733 | $this->getEntityPersister($class->getName())->refresh( |
||
| 1734 | array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]), |
||
| 1735 | $entity |
||
| 1736 | ); |
||
| 1737 | $this->cascadeRefresh($entity, $visited); |
||
| 1738 | } |
||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Cascades a refresh operation to associated entities. |
||
| 1742 | * |
||
| 1743 | * @param object $entity |
||
| 1744 | * @param array $visited |
||
| 1745 | * |
||
| 1746 | * @return void |
||
| 1747 | */ |
||
| 1748 | View Code Duplication | private function cascadeRefresh($entity, array &$visited) |
|
| 1749 | { |
||
| 1750 | $class = $this->manager->getClassMetadata(get_class($entity)); |
||
| 1751 | $associationMappings = array_filter( |
||
| 1752 | $class->getAssociationMappings(), |
||
| 1753 | function ($assoc) { |
||
| 1754 | return $assoc['isCascadeRefresh']; |
||
| 1755 | } |
||
| 1756 | ); |
||
| 1757 | foreach ($associationMappings as $assoc) { |
||
| 1758 | $relatedEntities = $class->getReflectionProperty($assoc['fieldName'])->getValue($entity); |
||
| 1759 | switch (true) { |
||
| 1760 | case ($relatedEntities instanceof ApiCollection): |
||
| 1761 | // Unwrap so that foreach() does not initialize |
||
| 1762 | $relatedEntities = $relatedEntities->unwrap(); |
||
| 1763 | // break; is commented intentionally! |
||
| 1764 | case ($relatedEntities instanceof Collection): |
||
| 1765 | case (is_array($relatedEntities)): |
||
| 1766 | foreach ($relatedEntities as $relatedEntity) { |
||
| 1767 | $this->doRefresh($relatedEntity, $visited); |
||
| 1768 | } |
||
| 1769 | break; |
||
| 1778 | |||
| 1779 | /** |
||
| 1780 | * Cascades a detach operation to associated entities. |
||
| 1781 | * |
||
| 1782 | * @param object $entity |
||
| 1783 | * @param array $visited |
||
| 1784 | * |
||
| 1785 | * @return void |
||
| 1786 | */ |
||
| 1787 | View Code Duplication | private function cascadeDetach($entity, array &$visited) |
|
| 1817 | |||
| 1818 | /** |
||
| 1819 | * Cascades a merge operation to associated entities. |
||
| 1820 | * |
||
| 1821 | * @param object $entity |
||
| 1822 | * @param object $managedCopy |
||
| 1823 | * @param array $visited |
||
| 1824 | * |
||
| 1825 | * @return void |
||
| 1826 | */ |
||
| 1827 | private function cascadeMerge($entity, $managedCopy, array &$visited) |
||
| 1856 | |||
| 1857 | /** |
||
| 1858 | * Cascades the delete operation to associated entities. |
||
| 1859 | * |
||
| 1860 | * @param object $entity |
||
| 1861 | * @param array $visited |
||
| 1862 | * |
||
| 1863 | * @return void |
||
| 1864 | */ |
||
| 1865 | private function cascadeRemove($entity, array &$visited) |
||
| 1899 | |||
| 1900 | /** |
||
| 1901 | * Executes any extra updates that have been scheduled. |
||
| 1902 | */ |
||
| 1903 | private function executeExtraUpdates() |
||
| 1912 | |||
| 1913 | private function executeDeletions(ApiMetadata $class) |
||
| 1936 | |||
| 1937 | /** |
||
| 1938 | * @param object $entity |
||
| 1939 | * @param object $managedCopy |
||
| 1940 | */ |
||
| 1941 | private function mergeEntityStateIntoManagedCopy($entity, $managedCopy) |
||
| 2018 | |||
| 2019 | /** |
||
| 2020 | * Deletes an entity as part of the current unit of work. |
||
| 2021 | * |
||
| 2022 | * This method is internally called during delete() cascades as it tracks |
||
| 2023 | * the already visited entities to prevent infinite recursions. |
||
| 2024 | * |
||
| 2025 | * @param object $entity The entity to delete. |
||
| 2026 | * @param array $visited The map of the already visited entities. |
||
| 2027 | * |
||
| 2028 | * @return void |
||
| 2029 | * |
||
| 2030 | * @throws \InvalidArgumentException If the instance is a detached entity. |
||
| 2031 | * @throws \UnexpectedValueException |
||
| 2032 | */ |
||
| 2033 | private function doRemove($entity, array &$visited) |
||
| 2059 | |||
| 2060 | /** |
||
| 2061 | * Tests if an entity is loaded - must either be a loaded proxy or not a proxy |
||
| 2062 | * |
||
| 2063 | * @param object $entity |
||
| 2064 | * |
||
| 2065 | * @return bool |
||
| 2066 | */ |
||
| 2067 | private function isLoaded($entity) |
||
| 2071 | |||
| 2072 | /** |
||
| 2073 | * Sets/adds associated managed copies into the previous entity's association field |
||
| 2074 | * |
||
| 2075 | * @param object $entity |
||
| 2076 | * @param array $association |
||
| 2077 | * @param object $previousManagedCopy |
||
| 2078 | * @param object $managedCopy |
||
| 2079 | * |
||
| 2080 | * @return void |
||
| 2081 | */ |
||
| 2082 | private function updateAssociationWithMergedEntity($entity, array $association, $previousManagedCopy, $managedCopy) |
||
| 2099 | |||
| 2100 | /** |
||
| 2101 | * Executes a merge operation on an entity. |
||
| 2102 | * |
||
| 2103 | * @param object $entity |
||
| 2104 | * @param array $visited |
||
| 2105 | * @param object|null $prevManagedCopy |
||
| 2106 | * @param array|null $assoc |
||
| 2107 | * |
||
| 2108 | * @return object The managed copy of the entity. |
||
| 2109 | * |
||
| 2110 | * @throws \InvalidArgumentException If the entity instance is NEW. |
||
| 2111 | * @throws \OutOfBoundsException |
||
| 2112 | */ |
||
| 2113 | private function doMerge($entity, array &$visited, $prevManagedCopy = null, array $assoc = []) |
||
| 2183 | |||
| 2184 | /** |
||
| 2185 | * Executes a detach operation on the given entity. |
||
| 2186 | * |
||
| 2187 | * @param object $entity |
||
| 2188 | * @param array $visited |
||
| 2189 | * @param boolean $noCascade if true, don't cascade detach operation. |
||
| 2190 | * |
||
| 2191 | * @return void |
||
| 2192 | */ |
||
| 2193 | private function doDetach($entity, array &$visited, $noCascade = false) |
||
| 2222 | |||
| 2223 | /** |
||
| 2224 | * @param ApiMetadata $class |
||
| 2225 | * |
||
| 2226 | * @return EntityHydrator |
||
| 2227 | */ |
||
| 2228 | 18 | private function getHydratorForClass(ApiMetadata $class) |
|
| 2236 | } |
||
| 2237 |
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.