1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

Reduce code duplication in ProxyFactory::createInitialized

The only difference between the ~30 lines in the IF-statement
is that the __wakup method is called on the proxy object.
This commit is contained in:
Andreas Schempp 2015-12-06 14:56:36 +01:00
parent 3c3b7364ba
commit 4df7699ced

View file

@ -123,45 +123,9 @@ class ProxyFactory extends AbstractProxyFactory
*/ */
private function createInitializer(ClassMetadata $classMetadata, EntityPersister $entityPersister) private function createInitializer(ClassMetadata $classMetadata, EntityPersister $entityPersister)
{ {
if ($classMetadata->getReflectionClass()->hasMethod('__wakeup')) { $wakeupProxy = $classMetadata->getReflectionClass()->hasMethod('__wakeup');
return function (BaseProxy $proxy) use ($entityPersister, $classMetadata) {
$initializer = $proxy->__getInitializer();
$cloner = $proxy->__getCloner();
$proxy->__setInitializer(null); return function (BaseProxy $proxy) use ($entityPersister, $classMetadata, $wakeupProxy) {
$proxy->__setCloner(null);
if ($proxy->__isInitialized()) {
return;
}
$properties = $proxy->__getLazyProperties();
foreach ($properties as $propertyName => $property) {
if ( ! isset($proxy->$propertyName)) {
$proxy->$propertyName = $properties[$propertyName];
}
}
$proxy->__setInitialized(true);
$proxy->__wakeup();
$identifier = $classMetadata->getIdentifierValues($proxy);
if (null === $entityPersister->loadById($identifier, $proxy)) {
$proxy->__setInitializer($initializer);
$proxy->__setCloner($cloner);
$proxy->__setInitialized(false);
throw EntityNotFoundException::fromClassNameAndIdentifier(
$classMetadata->getName(),
$this->identifierFlattener->flattenIdentifier($classMetadata, $identifier)
);
}
};
}
return function (BaseProxy $proxy) use ($entityPersister, $classMetadata) {
$initializer = $proxy->__getInitializer(); $initializer = $proxy->__getInitializer();
$cloner = $proxy->__getCloner(); $cloner = $proxy->__getCloner();
@ -175,13 +139,17 @@ class ProxyFactory extends AbstractProxyFactory
$properties = $proxy->__getLazyProperties(); $properties = $proxy->__getLazyProperties();
foreach ($properties as $propertyName => $property) { foreach ($properties as $propertyName => $property) {
if (!isset($proxy->$propertyName)) { if ( ! isset($proxy->$propertyName)) {
$proxy->$propertyName = $properties[$propertyName]; $proxy->$propertyName = $properties[$propertyName];
} }
} }
$proxy->__setInitialized(true); $proxy->__setInitialized(true);
if ($wakeupProxy) {
$proxy->__wakeup();
}
$identifier = $classMetadata->getIdentifierValues($proxy); $identifier = $classMetadata->getIdentifierValues($proxy);
if (null === $entityPersister->loadById($identifier, $proxy)) { if (null === $entityPersister->loadById($identifier, $proxy)) {