[Serializer] Reduce class discriminator overhead

This commit is contained in:
Fabien Bourigault 2018-10-16 14:04:33 +02:00 committed by Nicolas Grekas
parent 5f253bf39f
commit 326c267de2

View File

@ -30,6 +30,8 @@ class ObjectNormalizer extends AbstractObjectNormalizer
{
protected $propertyAccessor;
private $discriminatorCache = array();
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
{
if (!\class_exists(PropertyAccess::class)) {
@ -110,15 +112,16 @@ class ObjectNormalizer extends AbstractObjectNormalizer
*/
protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
{
if (null !== $this->classDiscriminatorResolver) {
$mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object);
if (null !== $mapping && $attribute == $mapping->getTypeProperty()) {
return $this->classDiscriminatorResolver->getTypeForMappedObject($object);
$cacheKey = \get_class($object);
if (!array_key_exists($cacheKey, $this->discriminatorCache)) {
$this->discriminatorCache[$cacheKey] = null;
if (null !== $this->classDiscriminatorResolver) {
$mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object);
$this->discriminatorCache[$cacheKey] = null === $mapping ? null : $mapping->getTypeProperty();
}
}
return $this->propertyAccessor->getValue($object, $attribute);
return $attribute === $this->discriminatorCache[$cacheKey] ? $this->classDiscriminatorResolver->getTypeForMappedObject($object) : $this->propertyAccessor->getValue($object, $attribute);
}
/**