Skip to content

[Serializer] Unify usage of normalizer cache #10457

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 3 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
Next Next commit
Move normalizer cache to getNormalier()/getDenormalizer(), use those …
…in normalizeObject()/denormalizeObject()
  • Loading branch information
Berdir committed Mar 15, 2014
commit 1467ae657057d9fd060ed1292b78d28e756a468e
70 changes: 41 additions & 29 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,28 @@ public function supportsDenormalization($data, $type, $format = null)
}

/**
* {@inheritdoc}
* Returns a matching normalizer.
*
* @param $data
Copy link
Member

Choose a reason for hiding this comment

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

The type is missing here

* @param null $format
Copy link
Member

Choose a reason for hiding this comment

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

null is not valid here; should be string instead, no?

*
* @return mixed
*
* @throws Exception\LogicException
* @throws Exception\UnexpectedValueException
*/
private function getNormalizer($data, $format = null)
{

$class = get_class($data);
if (isset($this->normalizerCache[$class][$format])) {
return $this->normalizerCache[$class][$format];
}

foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;

return $normalizer;
}
}
Expand All @@ -182,12 +198,27 @@ private function getNormalizer($data, $format = null)
}

/**
* {@inheritdoc}
* Returns a matching denormalizer.
*
* @param $data
* @param $type
* @param null $format
*
* @return mixed
*
* @throws Exception\LogicException
* @throws Exception\UnexpectedValueException
Copy link
Contributor

Choose a reason for hiding this comment

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

RuntimeException missing

*/
private function getDenormalizer($data, $type, $format = null)
{
if (isset($this->denormalizerCache[$type][$format])) {
return $this->denormalizerCache[$type][$format];
}

foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $type, $format)) {
$this->denormalizerCache[$type][$format] = $normalizer;

return $normalizer;
}
}
Expand Down Expand Up @@ -229,21 +260,11 @@ private function normalizeObject($object, $format = null, array $context = array
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
}

$class = get_class($object);
if (isset($this->normalizerCache[$class][$format])) {
return $this->normalizerCache[$class][$format]->normalize($object, $format, $context);
}

foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof NormalizerInterface
&& $normalizer->supportsNormalization($object, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;

return $normalizer->normalize($object, $format, $context);
}
try {
return $this->getNormalizer($object, $format)->normalize($object, $format, $context);
} catch (RuntimeException $e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This could swallow other RuntimeExceptions thrown within the normaler classes... Maybe better return null in getNormalizer() and throw here

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

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

/**
Expand All @@ -265,20 +286,11 @@ private function denormalizeObject($data, $class, $format = null, array $context
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
}

if (isset($this->denormalizerCache[$class][$format])) {
return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format, $context);
}

foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof DenormalizerInterface
&& $normalizer->supportsDenormalization($data, $class, $format)) {
$this->denormalizerCache[$class][$format] = $normalizer;

return $normalizer->denormalize($data, $class, $format, $context);
}
try {
return $this->getDenormalizer($data, $class, $format)->denormalize($data, $class, $format, $context);
} catch (RuntimeException $e) {
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
}

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

/**
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy