Skip to content

Commit 7f3fdbb

Browse files
authored
Avoid using reserved characters in PSR-6 cache keys (#70)
1 parent 9c3420b commit 7f3fdbb

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Doctrine/PolyglotListener.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ private function loadTranslationMetadataForClass($className, EntityManager $em):
140140
$cacheDriver = $em->getConfiguration()->getMetadataCacheImpl();
141141

142142
if ($cacheDriver) {
143-
if (($data = $cacheDriver->fetch($className.self::CACHE_SALT)) !== false) {
143+
$cacheKey = $this->getCacheKey($className);
144+
if (($data = $cacheDriver->fetch($cacheKey)) !== false) {
144145
if (null === $data) {
145146
$this->translatedClasses[$className] = null;
146147

@@ -164,8 +165,19 @@ private function loadTranslationMetadataForClass($className, EntityManager $em):
164165
}
165166

166167
// Save if cache driver available
167-
$cacheDriver?->save($className.self::CACHE_SALT, $meta?->sleep());
168+
$cacheDriver?->save($cacheKey, $meta?->sleep());
168169

169170
return $meta;
170171
}
172+
173+
// this is taken from \Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory::escapeClassName
174+
private function getCacheKey(string $class): string
175+
{
176+
if (str_contains($class, '@')) {
177+
// anonymous class: replace all PSR6-reserved characters
178+
return str_replace(["\0", '\\', '/', '@', ':', '{', '}', '(', ')'], '.', $class).self::CACHE_SALT;
179+
}
180+
181+
return str_replace('\\', '.', $class).self::CACHE_SALT;
182+
}
171183
}

0 commit comments

Comments
 (0)