diff --git a/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php b/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php index c4615d8c..bbc5f232 100644 --- a/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php +++ b/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php @@ -167,7 +167,9 @@ public function getMetadataFor($className) if (isset($this->loadedMetadata[$realClassName])) { // We do not have the alias name in the map, include it - return $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName]; + $this->setMetadataFor($realClassName, $this->loadedMetadata[$realClassName]); + + return $this->loadedMetadata[$realClassName]; } $loadingException = null; @@ -180,13 +182,7 @@ public function getMetadataFor($className) $this->wakeupReflection($cached, $this->getReflectionService()); } else { - foreach ($this->loadMetadata($realClassName) as $loadedClassName) { - $this->cacheDriver->save( - $loadedClassName . $this->cacheSalt, - $this->loadedMetadata[$loadedClassName], - null - ); - } + $this->loadMetadata($realClassName); } } else { $this->loadMetadata($realClassName); @@ -198,12 +194,12 @@ public function getMetadataFor($className) throw $loadingException; } - $this->loadedMetadata[$realClassName] = $fallbackMetadataResponse; + $this->setMetadataFor($realClassName, $fallbackMetadataResponse); } if ($className !== $realClassName) { // We do not have the alias name in the map, include it - $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName]; + $this->setMetadataFor($className, $this->loadedMetadata[$realClassName]); } return $this->loadedMetadata[$className]; @@ -234,6 +230,16 @@ public function hasMetadataFor($className) public function setMetadataFor($className, $class) { $this->loadedMetadata[$className] = $class; + + if (! $this->cacheDriver) { + return; + } + + $this->cacheDriver->save( + $className . $this->cacheSalt, + $this->loadedMetadata[$className], + null + ); } /** @@ -303,8 +309,7 @@ protected function loadMetadata($name) $this->initializeReflection($class, $reflService); $this->doLoadMetadata($class, $parent, $rootEntityFound, $visited); - - $this->loadedMetadata[$className] = $class; + $this->setMetadataFor($className, $class); $parent = $class; diff --git a/tests/Doctrine/Tests/Common/Persistence/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/Common/Persistence/Mapping/ClassMetadataFactoryTest.php index 1699bbcd..a0c3bc1f 100644 --- a/tests/Doctrine/Tests/Common/Persistence/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/Common/Persistence/Mapping/ClassMetadataFactoryTest.php @@ -157,6 +157,35 @@ public function testWillIgnoreCacheEntriesThatAreNotMetadataInstances() self::assertSame($metadata, $this->cmf->getMetadataFor('Foo')); } + + public function testFallbackMetadataShouldBeCached() + { + /** @var Cache|\PHPUnit_Framework_MockObject_MockObject $cacheDriver */ + $cacheDriver = $this->createMock(Cache::class); + $cacheDriver->expects($this->once())->method('save'); + + $this->cmf->setCacheDriver($cacheDriver); + + $classMetadata = $this->createMock(ClassMetadata::class); + + $this->cmf->fallbackCallback = function () use ($classMetadata) { + return $classMetadata; + }; + + $this->cmf->getMetadataFor('Foo'); + } + + public function testSetMetadataForShouldUpdateCache() + { + /** @var Cache|\PHPUnit_Framework_MockObject_MockObject $cacheDriver */ + $cacheDriver = $this->createMock(Cache::class); + $cacheDriver->expects($this->once())->method('save'); + + $this->cmf->setCacheDriver($cacheDriver); + + $classMetadata = $this->createMock(ClassMetadata::class); + $this->cmf->setMetadataFor('Foo', $classMetadata); + } } class TestClassMetadataFactory extends AbstractClassMetadataFactory