Skip to content

Commit

Permalink
Fix Metadata Caching when it changes in EventListeners
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-bondar-dron committed Oct 20, 2018
1 parent af1ec23 commit 4e124e8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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];
Expand Down Expand Up @@ -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
);
}

/**
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e124e8

Please sign in to comment.