[Serializer] Add ->hasCacheableSupportsMethod() to CacheableSupportsMethodInterface

This commit is contained in:
Nicolas Grekas 2018-04-30 14:41:10 -07:00
parent 4fb673677e
commit 04b369215c
14 changed files with 98 additions and 11 deletions

View File

@ -27,7 +27,7 @@ use Symfony\Component\Serializer\SerializerAwareTrait;
* *
* @author Kévin Dunglas <dunglas@gmail.com> * @author Kévin Dunglas <dunglas@gmail.com>
*/ */
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
{ {
use ObjectToPopulateTrait; use ObjectToPopulateTrait;
use SerializerAwareTrait; use SerializerAwareTrait;
@ -147,6 +147,14 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return false;
}
/** /**
* Detects if the configured circular reference limit is reached. * Detects if the configured circular reference limit is reached.
* *

View File

@ -30,7 +30,7 @@ use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
* *
* @author Kévin Dunglas <dunglas@gmail.com> * @author Kévin Dunglas <dunglas@gmail.com>
*/ */
abstract class AbstractObjectNormalizer extends AbstractNormalizer implements CacheableSupportsMethodInterface abstract class AbstractObjectNormalizer extends AbstractNormalizer
{ {
const ENABLE_MAX_DEPTH = 'enable_max_depth'; const ENABLE_MAX_DEPTH = 'enable_max_depth';
const DEPTH_KEY_PATTERN = 'depth_%s::%s'; const DEPTH_KEY_PATTERN = 'depth_%s::%s';

View File

@ -24,7 +24,7 @@ use Symfony\Component\Serializer\SerializerInterface;
* *
* @final * @final
*/ */
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
{ {
/** /**
* @var SerializerInterface|DenormalizerInterface * @var SerializerInterface|DenormalizerInterface
@ -83,4 +83,12 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Serializer
$this->serializer = $serializer; $this->serializer = $serializer;
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod();
}
} }

View File

@ -22,4 +22,5 @@ namespace Symfony\Component\Serializer\Normalizer;
*/ */
interface CacheableSupportsMethodInterface interface CacheableSupportsMethodInterface
{ {
public function hasCacheableSupportsMethod(): bool;
} }

View File

@ -56,4 +56,12 @@ class ConstraintViolationListNormalizer implements NormalizerInterface, Cacheabl
{ {
return $data instanceof ConstraintViolationListInterface; return $data instanceof ConstraintViolationListInterface;
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
} }

View File

@ -67,4 +67,12 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
{ {
return \is_subclass_of($type, DenormalizableInterface::class); return \is_subclass_of($type, DenormalizableInterface::class);
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
} }

View File

@ -119,6 +119,14 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C
return isset(self::$supportedTypes[$type]); return isset(self::$supportedTypes[$type]);
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/** /**
* Gets the mime type of the object. Defaults to application/octet-stream. * Gets the mime type of the object. Defaults to application/octet-stream.
* *

View File

@ -55,6 +55,14 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa
return $data instanceof \DateInterval; return $data instanceof \DateInterval;
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* *

View File

@ -116,6 +116,14 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
return isset(self::$supportedTypes[$type]); return isset(self::$supportedTypes[$type]);
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/** /**
* Formats datetime errors. * Formats datetime errors.
* *

View File

@ -52,6 +52,14 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type); return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/** /**
* Checks if the given class has any get{Property} method. * Checks if the given class has any get{Property} method.
*/ */

View File

@ -19,7 +19,7 @@ use Symfony\Component\Serializer\Exception\LogicException;
* *
* @author Fred Cox <mcfedr@gmail.com> * @author Fred Cox <mcfedr@gmail.com>
*/ */
class JsonSerializableNormalizer extends AbstractNormalizer implements CacheableSupportsMethodInterface class JsonSerializableNormalizer extends AbstractNormalizer
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -64,4 +64,12 @@ class JsonSerializableNormalizer extends AbstractNormalizer implements Cacheable
{ {
throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class)); throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class));
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
} }

View File

@ -40,6 +40,14 @@ class ObjectNormalizer extends AbstractObjectNormalizer
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -46,6 +46,14 @@ class PropertyNormalizer extends AbstractObjectNormalizer
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type); return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
} }
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/** /**
* Checks if the given class has any non-static property. * Checks if the given class has any non-static property.
*/ */

View File

@ -19,6 +19,7 @@ use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface; use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Exception\NotEncodableValueException; use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
@ -26,7 +27,6 @@ use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Exception\LogicException; use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
/** /**
* Serializer serializes and deserializes data. * Serializer serializes and deserializes data.
@ -221,12 +221,11 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
continue; continue;
} }
if (!$normalizer instanceof CacheableSupportsMethodInterface) { if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
$this->normalizerCache[$format][$type][$k] = false; $this->normalizerCache[$format][$type][$k] = false;
} elseif ($normalizer->supportsNormalization($data, $format)) { } elseif ($normalizer->supportsNormalization($data, $format)) {
$this->normalizerCache[$format][$type][$k] = true; $this->normalizerCache[$format][$type][$k] = true;
break;
return $normalizer;
} }
} }
} }
@ -263,12 +262,11 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
continue; continue;
} }
if (!$normalizer instanceof CacheableSupportsMethodInterface) { if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
$this->denormalizerCache[$format][$class][$k] = false; $this->denormalizerCache[$format][$class][$k] = false;
} elseif ($normalizer->supportsDenormalization(null, $class, $format)) { } elseif ($normalizer->supportsDenormalization(null, $class, $format)) {
$this->denormalizerCache[$format][$class][$k] = true; $this->denormalizerCache[$format][$class][$k] = true;
break;
return $normalizer;
} }
} }
} }