Skip to content

[Serializer] Rename CacheableSupportsMethodInterface to VaryingSupportInterface #27210

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
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG
4.1.0
-----

* added `CacheableSupportsMethodInterface` for normalizers and denormalizers that use
* added `VaryingSupportInterface` for normalizers and denormalizers that use
only the type and the format in their `supports*()` methods
* added `MissingConstructorArgumentsException` new exception for deserialization failure
of objects that needs data insertion in constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, VaryingSupportInterface
{
use ObjectToPopulateTrait;
use SerializerAwareTrait;
Expand Down Expand Up @@ -150,9 +150,9 @@ public function setIgnoredAttributes(array $ignoredAttributes)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return false;
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @final
*/
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface, VaryingSupportInterface
{
/**
* @var SerializerInterface|DenormalizerInterface
Expand Down Expand Up @@ -87,8 +87,8 @@ public function setSerializer(SerializerInterface $serializer)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod();
return !$this->serializer instanceof VaryingSupportInterface || $this->serializer->isSupportVariedOnDataAndContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
class ConstraintViolationListNormalizer implements NormalizerInterface, VaryingSupportInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -60,8 +60,8 @@ public function supportsNormalization($data, $format = null)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, VaryingSupportInterface
{
use ObjectToPopulateTrait;
use SerializerAwareTrait;
Expand Down Expand Up @@ -71,8 +71,8 @@ public function supportsDenormalization($data, $type, $format = null)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, VaryingSupportInterface
{
private static $supportedTypes = array(
\SplFileInfo::class => true,
Expand Down Expand Up @@ -122,9 +122,9 @@ public function supportsDenormalization($data, $type, $format = null)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author Jérôme Parmentier <jerome@prmntr.me>
*/
class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface, VaryingSupportInterface
{
const FORMAT_KEY = 'dateinterval_format';

Expand Down Expand Up @@ -58,9 +58,9 @@ public function supportsNormalization($data, $format = null)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, VaryingSupportInterface
{
const FORMAT_KEY = 'datetime_format';
const TIMEZONE_KEY = 'datetime_timezone';
Expand Down Expand Up @@ -119,9 +119,9 @@ public function supportsDenormalization($data, $type, $format = null)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function supportsDenormalization($data, $type, $format = null)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function denormalize($data, $class, $format = null, array $context = arra
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public function supportsDenormalization($data, $type, $format = null)
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
public function isSupportVariedOnDataAndContext(): bool
{
return __CLASS__ === \get_class($this);
return __CLASS__ !== \get_class($this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
namespace Symfony\Component\Serializer\Normalizer;
Copy link
Contributor Author

@teohhanhui teohhanhui May 11, 2018

Choose a reason for hiding this comment

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

I've removed any mention about caching, because that is an implementation detail of what the serializer does with classes implementing this interface. I think it's better to have a pure abstraction here.

If necessary, we could document it in the Serializer class. WDYT @nicolas-grekas @dunglas?


/**
* Marker interface for normalizers and denormalizers that use
* only the type and the format in their supports*() methods.
*
* By implementing this interface, the return value of the
* supports*() methods will be cached by type and format.
* Defines the criteria by which normalizers and denormalizers may vary
* their support.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface CacheableSupportsMethodInterface
interface VaryingSupportInterface
{
public function hasCacheableSupportsMethod(): bool;
/**
* Checks whether the normalization and denormalization support
* will vary depending on the data and context provided.
*/
public function isSupportVariedOnDataAndContext(): bool;
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\VaryingSupportInterface;
use Symfony\Component\Serializer\Exception\LogicException;

/**
Expand Down Expand Up @@ -221,7 +221,7 @@ private function getNormalizer($data, ?string $format, array $context)
continue;
}

if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
if (!$normalizer instanceof VaryingSupportInterface || $normalizer->isSupportVariedOnDataAndContext()) {
$this->normalizerCache[$format][$type][$k] = false;
} elseif ($normalizer->supportsNormalization($data, $format)) {
$this->normalizerCache[$format][$type][$k] = true;
Expand Down Expand Up @@ -262,7 +262,7 @@ private function getDenormalizer($data, string $class, ?string $format, array $c
continue;
}

if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
if (!$normalizer instanceof VaryingSupportInterface || $normalizer->isSupportVariedOnDataAndContext()) {
$this->denormalizerCache[$format][$class][$k] = false;
} elseif ($normalizer->supportsDenormalization(null, $class, $format)) {
$this->denormalizerCache[$format][$class][$k] = true;
Expand Down