10000 Speed up ObjectNormalizer by introducing ObjectPropertyAccessorInterface by fbourigault · Pull Request #29405 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Speed up ObjectNormalizer by introducing ObjectPropertyAccessorInterface #29405

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

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Serializer] Use ObjectPropertyAccessorInterface in ObjectNormalizer
  • Loading branch information
fbourigault committed Jan 24, 2019
commit 63ae16f77534d65b56ed698bc17bcb36065228fc
25 changes: 22 additions & 3 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\ObjectPropertyAccessorInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
Expand All @@ -32,14 +34,22 @@ class ObjectNormalizer extends AbstractObjectNormalizer

private $discriminatorCache = [];

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
{
if (!\class_exists(PropertyAccess::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this check be moved down a bit now as the PropertyAccess component is not necessarily needed anymore?

throw new LogicException('The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.');
}

parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);

if (null !== $propertyAccessor && !$propertyAccessor instanceof ObjectPropertyAccessorInterface && !$propertyAccessor instanceof PropertyAccessorInterface) {
throw new InvalidArgumentException(sprintf('Argument 3 passed to %s() must be an instance of %s or an instance of %s or null, %s given.', __METHOD__, ObjectPropertyAccessorInterface::class, PropertyAccessorInterface::class, \gettype($propertyAccessor)));
}

if (null !== $propertyAccessor && !$propertyAccessor instanceof ObjectPropertyAccessorInterface) {
@trigger_error(sprintf('Passing an instance of %s as the 3rd argument to "%s()" is deprecated since Symfony 4.3. Pass a %s instance instead.', PropertyAccessorInterface::class, __METHOD__, ObjectPropertyAccessorInterface::class), E_USER_DEPRECATED);
}

$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
}

Expand Down Expand Up @@ -121,7 +131,12 @@ protected function getAttributeValue($object, $attribute, $format = null, array
}
}

return $attribute === $this->discriminatorCache[$cacheKey] ? $this->classDiscriminatorResolver->getTypeForMappedObject($object) : $this->propertyAccessor->getValue($object, $attribute);
return $attribute === $this->discriminatorCache[$cacheKey]
? $this->classDiscriminatorResolver->getTypeForMappedObject($object)
: ($this->propertyAccessor instanceof ObjectPropertyAccessorInterface
? $this->propertyAccessor->getPropertyValue($object, $attribute)
: $this->propertyAccessor->getValue($object, $attribute)
);
}

/**
Expand All @@ -130,7 +145,11 @@ protected function getAttributeValue($object, $attribute, $format = null, array
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
{
try {
$this->propertyAccessor->setValue($object, $attribute, $value);
if ($this->propertyAccessor instanceof ObjectPropertyAccessorInterface) {
$this->propertyAccessor->setPropertyValue($object, $attribute, $value);
} else {
$this->propertyAccessor->setValue($object, $attribute, $value);
}
} catch (NoSuchPropertyException $exception) {
// Properties not found are ignored
}
Expand Down
0