/** * Generates a closure capable of finalizing a cloned proxy * * @param \Doctrine\ODM\PHPCR\Mapping\ClassMetadata $classMetadata * @param \ReflectionProperty $reflectionId * * @return \Closure * * @throws \Doctrine\Common\Proxy\Exception\UnexpectedValueException */ private function createCloner(ClassMetadata $classMetadata, ReflectionProperty $reflectionId) { $className = $classMetadata->getName(); $documentManager = $this->documentManager; return function (Proxy $cloned) use($className, $classMetadata, $documentManager, $reflectionId) { if ($cloned->__isInitialized()) { return; } $cloned->__setInitialized(true); $cloned->__setInitializer(null); $original = $documentManager->find($className, $reflectionId->getValue($cloned)); if (null === $original) { throw new UnexpectedValueException(sprintf('Proxy with ID "%s" could not be loaded', $reflectionId->getValue($cloned))); } foreach ($classMetadata->getReflectionClass()->getProperties() as $reflectionProperty) { $propertyName = $reflectionProperty->getName(); if ($classMetadata->hasField($propertyName) || $classMetadata->hasAssociation($propertyName)) { $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($cloned, $reflectionProperty->getValue($original)); } } }; }