-
-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Metadata Caching when it changes in EventListeners #42
base: 1.1.x
Are you sure you want to change the base?
Changes from all commits
af490ed
8285c60
8056780
0f042fa
1012742
128c4a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,9 @@ abstract class AbstractClassMetadataFactory implements ClassMetadataFactory | |
/** @var ClassMetadata[] */ | ||
private $loadedMetadata = []; | ||
|
||
/** @var string[] */ | ||
private $aliasesMap = []; | ||
|
||
/** @var bool */ | ||
protected $initialized = false; | ||
|
||
|
@@ -156,14 +159,7 @@ public function getMetadataFor($className) | |
return $this->loadedMetadata[$className]; | ||
} | ||
|
||
// Check for namespace alias | ||
if (strpos($className, ':') !== false) { | ||
[$namespaceAlias, $simpleClassName] = explode(':', $className, 2); | ||
|
||
$realClassName = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName); | ||
} else { | ||
$realClassName = $this->getRealClass($className); | ||
} | ||
$realClassName = $this->getRealClassName($className); | ||
|
||
if (isset($this->loadedMetadata[$realClassName])) { | ||
// We do not have the alias name in the map, include it | ||
|
@@ -177,32 +173,25 @@ public function getMetadataFor($className) | |
$cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt); | ||
if ($cached instanceof ClassMetadata) { | ||
$this->loadedMetadata[$realClassName] = $cached; | ||
|
||
$this->wakeupReflection($cached, $this->getReflectionService()); | ||
} else { | ||
foreach ($this->loadMetadata($realClassName) as $loadedClassName) { | ||
$this->cacheDriver->save( | ||
$loadedClassName . $this->cacheSalt, | ||
$this->loadedMetadata[$loadedClassName] | ||
); | ||
} | ||
$this->loadMetadata($realClassName); | ||
} | ||
} else { | ||
$this->loadMetadata($realClassName); | ||
} | ||
} catch (MappingException $loadingException) { | ||
$fallbackMetadataResponse = $this->onNotFoundMetadata($realClassName); | ||
$fallbackMetadataResponse = $this->onNotFoundMetadata($className); | ||
|
||
if (! $fallbackMetadataResponse) { | ||
throw $loadingException; | ||
} | ||
|
||
$this->loadedMetadata[$realClassName] = $fallbackMetadataResponse; | ||
$this->setMetadataFor($className, $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]; | ||
|
@@ -233,6 +222,15 @@ public function hasMetadataFor($className) | |
public function setMetadataFor($className, $class) | ||
{ | ||
$this->loadedMetadata[$className] = $class; | ||
|
||
if ($this->cacheDriver === null) { | ||
return; | ||
} | ||
|
||
$this->cacheDriver->save( | ||
$className . $this->cacheSalt, | ||
$this->loadedMetadata[$className] | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -302,8 +300,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; | ||
|
||
|
@@ -398,16 +395,28 @@ public function getReflectionService() | |
} | ||
|
||
/** | ||
* Gets the real class name of a class name that could be a proxy. | ||
* Gets the real class name of a class name that could be a proxy or alias. | ||
*/ | ||
private function getRealClass(string $class) : string | ||
protected function getRealClassName(string $className) : string | ||
{ | ||
$pos = strrpos($class, '\\' . Proxy::MARKER . '\\'); | ||
if (isset($this->aliasesMap[$className])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering that duplicate entries are allowed, is this property still needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was done because of #21 (comment) Can you please decipher comment chain and explain if something still needs to be done? |
||
return $this->aliasesMap[$className]; | ||
} | ||
|
||
if ($pos === false) { | ||
return $class; | ||
$realClassName = $className; | ||
|
||
if (strpos($className, ':') !== false) { | ||
[$namespaceAlias, $simpleClassName] = explode(':', $className, 2); | ||
$realClassName = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName); | ||
} | ||
|
||
$pos = strrpos($className, '\\' . Proxy::MARKER . '\\'); | ||
if ($pos !== false) { | ||
$realClassName = substr($className, $pos + Proxy::MARKER_LENGTH + 2); | ||
} | ||
|
||
return substr($class, $pos + Proxy::MARKER_LENGTH + 2); | ||
$this->aliasesMap[$className] = $realClassName; | ||
|
||
return $realClassName; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should most likely stay
private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is protected as per suggestion of @alcaeus at #21 (comment)