Skip to content

[Serializer] Add local cache to normalizers #24228

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

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer

private $propertyTypeExtractor;
private $attributesCache = array();
private $cache = array();

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
{
Expand All @@ -49,7 +50,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && !$data instanceof \Traversable;
return \is_object($data) && !$data instanceof \Traversable;
}

/**
Expand Down Expand Up @@ -163,7 +164,7 @@ abstract protected function getAttributeValue($object, $attribute, $format = nul
*/
public function supportsDenormalization($data, $type, $format = null)
{
return class_exists($type);
return isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = class_exists($type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
*/
public function supportsDenormalization($data, $type, $format = null/*, array $context = array()*/)
{
$context = func_num_args() > 3 ? func_get_arg(3) : array();
$context = \func_num_args() > 3 ? func_get_arg(3) : array();
Copy link
Member Author

Choose a reason for hiding this comment

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


return '[]' === substr($type, -2)
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
{
private $cache = array();

use SerializerAwareTrait;

/**
Expand Down Expand Up @@ -64,10 +66,14 @@ public function supportsNormalization($data, $format = null)
*/
public function supportsDenormalization($data, $type, $format = null)
{
if (isset($this->cache[$type])) {
return $this->cache[$type];
}

if (!class_exists($type)) {
return false;
return $this->cache[$type] = false;
}

return is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
*/
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface
{
private static $supportedTypes = array(
\SplFileInfo::class => true,
\SplFileObject::class => true,
File::class => true,
);

/**
* @var MimeTypeGuesserInterface
*/
Expand Down Expand Up @@ -107,13 +113,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
*/
public function supportsDenormalization($data, $type, $format = null)
{
$supportedTypes = array(
\SplFileInfo::class => true,
\SplFileObject::class => true,
'Symfony\Component\HttpFoundation\File\File' => true,
);

return isset($supportedTypes[$type]);
return isset(self::$supportedTypes[$type]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface
private $format;
private $timezone;

private static $supportedTypes = array(
\DateTimeInterface::class => true,
\DateTimeImmutable::class => true,
\DateTime::class => true,
);

/**
* @param string $format
* @param \DateTimeZone|null $timezone
Expand Down Expand Up @@ -115,13 +121,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
*/
public function supportsDenormalization($data, $type, $format = null)
{
$supportedTypes = array(
\DateTimeInterface::class => true,
\DateTimeImmutable::class => true,
\DateTime::class => true,
);

return isset($supportedTypes[$type]);
return isset(self::$supportedTypes[$type]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@
class GetSetMethodNormalizer extends AbstractObjectNormalizer
{
private static $setterAccessibleCache = array();
private $cache = array();

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
Copy link
Contributor

Choose a reason for hiding this comment

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

applies to supportsDenormalization as well, no?

Copy link
Contributor

Choose a reason for hiding this comment

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

what about private static $cache + private static supports() + inline isGetMethod

}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@
*/
class PropertyNormalizer extends AbstractObjectNormalizer
{
private $cache = array();

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
Copy link
Contributor

Choose a reason for hiding this comment

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

same

}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
}

/**
Expand Down
36 changes: 9 additions & 27 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ public function normalize($data, $format = null, array $context = array())
*/
public function denormalize($data, $type, $format = null, array $context = array())
{
return $this->denormalizeObject($data, $type, $format, $context);
if (!$this->normalizers) {
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
}

if ($normalizer = $this->getDenormalizer($data, $type, $format, $context)) {
return $normalizer->denormalize($data, $type, $format, $context);
}

throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $type));
}

/**
Expand Down Expand Up @@ -269,32 +277,6 @@ final public function decode($data, $format, array $context = array())
return $this->decoder->decode($data, $format, $context);
}

/**
* Denormalizes data back into an object of the given class.
*
* @param mixed $data data to restore
* @param string $class the expected class to instantiate
* @param string $format format name, present to give the option to normalizers to act differently based on formats
* @param array $context The context data for this particular denormalization
*
* @return object
*
* @throws LogicException
* @throws UnexpectedValueException
*/
private function denormalizeObject($data, $class, $format, array $context = array())
{
if (!$this->normalizers) {
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
}

if ($normalizer = $this->getDenormalizer($data, $class, $format, $context)) {
return $normalizer->denormalize($data, $class, $format, $context);
}

throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
}

/**
* {@inheritdoc}
*/
Expand Down