Permalink
Browse files

feature #27105 [Serializer] Add ->hasCacheableSupportsMethod() to Cac…

…heableSupportsMethodInterface (nicolas-grekas)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Serializer] Add ->hasCacheableSupportsMethod() to CacheableSupportsMethodInterface

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Enhances the interface introduced in #27049 to make it possible to dynamically define if "supports" methods are cacheable.

Commits
-------

04b3692 [Serializer] Add ->hasCacheableSupportsMethod() to CacheableSupportsMethodInterface
  • Loading branch information...
dunglas committed May 3, 2018
2 parents e6f99da + 04b3692 commit 50fd769ea76885cc773404e8df02b234d6a9b908
@@ -27,7 +27,7 @@
*
* @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 SerializerAwareTrait;
@@ -147,6 +147,14 @@ public function setIgnoredAttributes(array $ignoredAttributes)
return $this;
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return false;
}
/**
* Detects if the configured circular reference limit is reached.
*
@@ -30,7 +30,7 @@
*
* @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 DEPTH_KEY_PATTERN = 'depth_%s::%s';
@@ -24,7 +24,7 @@
*
* @final
*/
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
{
/**
* @var SerializerInterface|DenormalizerInterface
@@ -83,4 +83,12 @@ public function setSerializer(SerializerInterface $serializer)
$this->serializer = $serializer;
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod();
}
}
@@ -22,4 +22,5 @@
*/
interface CacheableSupportsMethodInterface
{
public function hasCacheableSupportsMethod(): bool;
}
@@ -56,4 +56,12 @@ public function supportsNormalization($data, $format = null)
{
return $data instanceof ConstraintViolationListInterface;
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
}
@@ -67,4 +67,12 @@ public function supportsDenormalization($data, $type, $format = null)
{
return \is_subclass_of($type, DenormalizableInterface::class);
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
}
@@ -119,6 +119,14 @@ public function supportsDenormalization($data, $type, $format = null)
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.
*
@@ -55,6 +55,14 @@ public function supportsNormalization($data, $format = null)
return $data instanceof \DateInterval;
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/**
* {@inheritdoc}
*
@@ -116,6 +116,14 @@ public function supportsDenormalization($data, $type, $format = null)
return isset(self::$supportedTypes[$type]);
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/**
* Formats datetime errors.
*
@@ -52,6 +52,14 @@ public function supportsDenormalization($data, $type, $format = null)
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.
*/
@@ -19,7 +19,7 @@
*
* @author Fred Cox <mcfedr@gmail.com>
*/
class JsonSerializableNormalizer extends AbstractNormalizer implements CacheableSupportsMethodInterface
class JsonSerializableNormalizer extends AbstractNormalizer
{
/**
* {@inheritdoc}
@@ -64,4 +64,12 @@ public function denormalize($data, $class, $format = null, array $context = arra
{
throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class));
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
}
@@ -40,6 +40,14 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
/**
* {@inheritdoc}
*/
@@ -46,6 +46,14 @@ public function supportsDenormalization($data, $type, $format = null)
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.
*/
@@ -19,14 +19,14 @@
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\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
/**
* Serializer serializes and deserializes data.
@@ -221,12 +221,11 @@ private function getNormalizer($data, ?string $format, array $context)
continue;
}
if (!$normalizer instanceof CacheableSupportsMethodInterface) {
if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
$this->normalizerCache[$format][$type][$k] = false;
} elseif ($normalizer->supportsNormalization($data, $format)) {
$this->normalizerCache[$format][$type][$k] = true;
return $normalizer;
break;
}
}
}
@@ -263,12 +262,11 @@ private function getDenormalizer($data, string $class, ?string $format, array $c
continue;
}
if (!$normalizer instanceof CacheableSupportsMethodInterface) {
if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
$this->denormalizerCache[$format][$class][$k] = false;
} elseif ($normalizer->supportsDenormalization(null, $class, $format)) {
$this->denormalizerCache[$format][$class][$k] = true;
return $normalizer;
break;
}
}
}

0 comments on commit 50fd769

Please sign in to comment.