private function createProxy(AggregateContainer $aggregateContainer) { $serializedMetadata = $aggregateContainer->serializedMetadata(); $aggregateClassName = $this->serializer->getAggregateClass($serializedMetadata); return $this->proxyFactory->createProxy($aggregateClassName, function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) use($aggregateContainer) { $serializationResult = new SerializationResult($aggregateContainer->serializedAggregate(), $aggregateContainer->serializedMetadata()); $wrappedObject = $this->serializer->deserialize($serializationResult, $this); $initializer = null; // turning off further lazy initialization $aggregateContainer->setAggregate($wrappedObject); }); }
public function add(AggregateContainer $container) { $aggregateRootId = $container->aggregateRootId(); $aggregate = $container->aggregate(); if (!isset($this->containers[$aggregateRootId])) { $objectHash = spl_object_hash($aggregate); if (isset($this->aggregateRootIds[$objectHash])) { throw new AggregatePersistedWithMultipleIdsException(); } $this->containers[$aggregateRootId] = $container; $this->aggregateRootIds[$objectHash] = $aggregateRootId; $container->onSetAggregate(function ($newAggregateInstance) use($aggregateRootId) { $newObjectHash = spl_object_hash($newAggregateInstance); $this->aggregateRootIds[$newObjectHash] = $aggregateRootId; }); } elseif ($aggregate !== $this->containers[$aggregateRootId]->aggregate()) { throw new AggregateIdCollisionException(); } }