From 1cecc9c4292a471d6d2028e5576e8021053d49ef Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 2 Jan 2012 15:57:32 +0100 Subject: [PATCH] DCOM-93 - Factor out __wakeup into a delegate-method from ClassMetadataFactory#wakeupReflection to ClassMetadataInfo#wakeupReflection --- lib/Doctrine/ORM/Mapping/ClassMetadata.php | 22 +++++++------- .../ORM/Mapping/ClassMetadataFactory.php | 17 +---------- .../ORM/Mapping/ClassMetadataInfo.php | 30 +++++++++++++++++++ .../Mapping/BasicInheritanceMappingTest.php | 3 +- .../Tests/ORM/Mapping/ClassMetadataTest.php | 1 + 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadata.php b/lib/Doctrine/ORM/Mapping/ClassMetadata.php index a5b983fbb..e20dd578e 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadata.php @@ -327,32 +327,34 @@ class ClassMetadata extends ClassMetadataInfo implements IClassMetadata /** * Restores some state that can not be serialized/unserialized. * + * @param ReflectionService $reflService * @return void */ - public function __wakeup() + public function wakeupReflection($reflService) { // Restore ReflectionClass and properties - $this->reflClass = new ReflectionClass($this->name); + $this->reflClass = $reflService->getClass($this->name); foreach ($this->fieldMappings as $field => $mapping) { $reflField = isset($mapping['declared']) - ? new ReflectionProperty($mapping['declared'], $field) - : $this->reflClass->getProperty($field); - - $reflField->setAccessible(true); + ? $reflService->getAccessibleProperty($mapping['declared'], $field) + : $reflService->getAccessibleProperty($this->name, $field); $this->reflFields[$field] = $reflField; } foreach ($this->associationMappings as $field => $mapping) { $reflField = isset($mapping['declared']) - ? new ReflectionProperty($mapping['declared'], $field) - : $this->reflClass->getProperty($field); - - $reflField->setAccessible(true); + ? $reflService->getAccessibleProperty($mapping['declared'], $field) + : $reflService->getAccessibleProperty($this->name, $field); $this->reflFields[$field] = $reflField; } } + public function initializeReflection($reflService) + { + + } + /** * Creates a new instance of the mapped class, without invoking the constructor. * diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 07d6344ca..c204f016c 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -576,22 +576,7 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface */ protected function wakeupReflection(ClassMetadataInfo $class, ReflectionService $reflService) { - // Restore ReflectionClass and properties - $class->reflClass = $reflService->getClass($class->name); - - foreach ($class->fieldMappings as $field => $mapping) { - $reflField = isset($mapping['declared']) - ? $reflService->getAccessibleProperty($mapping['declared'], $field) - : $reflService->getAccessibleProperty($class->name, $field); - $class->reflFields[$field] = $reflField; - } - - foreach ($class->associationMappings as $field => $mapping) { - $reflField = isset($mapping['declared']) - ? $reflService->getAccessibleProperty($mapping['declared'], $field) - : $reflService->getAccessibleProperty($class->name, $field); - $class->reflFields[$field] = $reflField; - } + $class->wakeupReflection($reflService); } /** diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 56dd16385..82c1adff2 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -506,6 +506,36 @@ class ClassMetadataInfo $this->rootEntityName = $entityName; } + /** + * Restores some state that can not be serialized/unserialized. + * + * @param ReflectionService $reflService + * @return void + */ + public function wakeupReflection($reflService) + { + // Restore ReflectionClass and properties + $this->reflClass = $reflService->getClass($this->name); + + foreach ($this->fieldMappings as $field => $mapping) { + $this->reflFields[$field] = isset($mapping['declared']) + ? $reflService->getAccessibleProperty($mapping['declared'], $field) + : $reflService->getAccessibleProperty($this->name, $field); + } + + foreach ($this->associationMappings as $field => $mapping) { + $this->reflFields[$field] = isset($mapping['declared']) + ? $reflService->getAccessibleProperty($mapping['declared'], $field) + : $reflService->getAccessibleProperty($this->name, $field); + } + } + + public function initializeReflection($reflService) + { + + } + + /** * Gets the ReflectionClass instance of the mapped class. * diff --git a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php index 0c739e9f8..ff7f04048 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php @@ -91,6 +91,7 @@ class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\EntitySubClass2'); $class2 = unserialize(serialize($class)); + $class2->wakeupReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService); $this->assertTrue(isset($class2->reflFields['mapped1'])); $this->assertTrue(isset($class2->reflFields['mapped2'])); @@ -315,4 +316,4 @@ class MediumSuperclassEntity extends MediumSuperclassBase class SubclassWithRepository extends \Doctrine\Tests\Models\DDC869\DDC869Payment { -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index 7a4de4ca4..88af7f2ae 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -36,6 +36,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $serialized = serialize($cm); $cm = unserialize($serialized); + $cm->wakeupReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService); // Check state $this->assertTrue(count($cm->getReflectionProperties()) > 0);