diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 5a49cfec91..93e85c6fe6 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -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) { @@ -49,7 +50,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer */ public function supportsNormalization($data, $format = null) { - return is_object($data) && !$data instanceof \Traversable; + return \is_object($data) && !$data instanceof \Traversable; } /** @@ -163,7 +164,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer */ 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); } /** diff --git a/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php index 7a94d59aad..14b23d01a0 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php @@ -66,7 +66,7 @@ class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterfa */ 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(); return '[]' === substr($type, -2) && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context); diff --git a/src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php index 688590ef02..fd8bbca274 100644 --- a/src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php @@ -19,6 +19,8 @@ use Symfony\Component\Serializer\SerializerAwareTrait; */ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface { + private $cache = array(); + use SerializerAwareTrait; /** @@ -64,10 +66,14 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se */ public function supportsDenormalization($data, $type, $format = null) { - if (!class_exists($type)) { - return false; + if (isset($this->cache[$type])) { + return $this->cache[$type]; } - return is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface'); + if (!class_exists($type)) { + return $this->cache[$type] = false; + } + + return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface'); } } diff --git a/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php index 9e5af130d0..66a9ed6bd8 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php @@ -25,6 +25,12 @@ use Symfony\Component\Serializer\Exception\UnexpectedValueException; */ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface { + private static $supportedTypes = array( + \SplFileInfo::class => true, + \SplFileObject::class => true, + File::class => true, + ); + /** * @var MimeTypeGuesserInterface */ @@ -107,13 +113,7 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface */ 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]); } /** diff --git a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php index 35b0b5f0e5..26fd9c6201 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php @@ -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 @@ -115,13 +121,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface */ 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]); } /** diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index 3d2c0665be..426ae562fb 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -35,13 +35,14 @@ namespace Symfony\Component\Serializer\Normalizer; 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)); } /** @@ -49,7 +50,7 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer */ 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)); } /** diff --git a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php index 9795ec4bc8..2d96608ff2 100644 --- a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php @@ -30,12 +30,14 @@ namespace Symfony\Component\Serializer\Normalizer; */ 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)); } /** @@ -43,7 +45,7 @@ class PropertyNormalizer extends AbstractObjectNormalizer */ 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)); } /** diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index 36752465ac..4c5cec1653 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -171,7 +171,15 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz */ 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)); } /** @@ -269,32 +277,6 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz 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} */