Skip to content

Commit 9e12d44

Browse files
committed
Added local cache for the discriminator resolver
1 parent 256ef8b commit 9e12d44

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorFromClassMetadata.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ClassDiscriminatorFromClassMetadata implements ClassDiscriminatorResolverI
2222
* @var ClassMetadataFactoryInterface
2323
*/
2424
private $classMetadataFactory;
25+
private $mappingForMappedObjectCache = [];
2526

2627
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory)
2728
{
@@ -53,18 +54,12 @@ public function getMappingForMappedObject($object)
5354
}
5455
}
5556

56-
$reflectionClass = new \ReflectionClass($object);
57-
if ($parentClass = $reflectionClass->getParentClass()) {
58-
return $this->getMappingForMappedObject($parentClass->getName());
57+
$cacheKey = is_object($object) ? get_class($object) : $object;
58+
if (!array_key_exists($cacheKey, $this->mappingForMappedObjectCache)) {
59+
$this->mappingForMappedObjectCache[$cacheKey] = $this->resolveMappingForMappedObject($object);
5960
}
6061

61-
foreach ($reflectionClass->getInterfaceNames() as $interfaceName) {
62-
if (null !== ($interfaceMapping = $this->getMappingForMappedObject($interfaceName))) {
63-
return $interfaceMapping;
64-
}
65-
}
66-
67-
return null;
62+
return $this->mappingForMappedObjectCache[$cacheKey];
6863
}
6964

7065
/**
@@ -78,4 +73,20 @@ public function getTypeForMappedObject($object)
7873

7974
return $mapping->getMappedObjectType($object);
8075
}
76+
77+
private function resolveMappingForMappedObject($object)
78+
{
79+
$reflectionClass = new \ReflectionClass($object);
80+
if ($parentClass = $reflectionClass->getParentClass()) {
81+
return $this->getMappingForMappedObject($parentClass->getName());
82+
}
83+
84+
foreach ($reflectionClass->getInterfaceNames() as $interfaceName) {
85+
if (null !== ($interfaceMapping = $this->getMappingForMappedObject($interfaceName))) {
86+
return $interfaceMapping;
87+
}
88+
}
89+
90+
return null;
91+
}
8192
}

0 commit comments

Comments
 (0)