Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50fd769

Browse files
committed
feature #27105 [Serializer] Add ->hasCacheableSupportsMethod() to CacheableSupportsMethodInterface (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
2 parents e6f99da + 04b3692 commit 50fd769

14 files changed

+98
-11
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* @author Kévin Dunglas <[email protected]>
2929
*/
30-
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
30+
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
3131
{
3232
use ObjectToPopulateTrait;
3333
use SerializerAwareTrait;
@@ -147,6 +147,14 @@ public function setIgnoredAttributes(array $ignoredAttributes)
147147
return $this;
148148
}
149149

150+
/**
151+
* {@inheritdoc}
152+
*/
153+
public function hasCacheableSupportsMethod(): bool
154+
{
155+
return false;
156+
}
157+
150158
/**
151159
* Detects if the configured circular reference limit is reached.
152160
*

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @author Kévin Dunglas <[email protected]>
3232
*/
33-
abstract class AbstractObjectNormalizer extends AbstractNormalizer implements CacheableSupportsMethodInterface
33+
abstract class AbstractObjectNormalizer extends AbstractNormalizer
3434
{
3535
const ENABLE_MAX_DEPTH = 'enable_max_depth';
3636
const DEPTH_KEY_PATTERN = 'depth_%s::%s';

src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @final
2626
*/
27-
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface
27+
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
2828
{
2929
/**
3030
* @var SerializerInterface|DenormalizerInterface
@@ -83,4 +83,12 @@ public function setSerializer(SerializerInterface $serializer)
8383

8484
$this->serializer = $serializer;
8585
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function hasCacheableSupportsMethod(): bool
91+
{
92+
return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod();
93+
}
8694
}

src/Symfony/Component/Serializer/Normalizer/CacheableSupportsMethodInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
*/
2323
interface CacheableSupportsMethodInterface
2424
{
25+
public function hasCacheableSupportsMethod(): bool;
2526
}

src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ public function supportsNormalization($data, $format = null)
5656
{
5757
return $data instanceof ConstraintViolationListInterface;
5858
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function hasCacheableSupportsMethod(): bool
64+
{
65+
return __CLASS__ === \get_class($this);
66+
}
5967
}

src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ public function supportsDenormalization($data, $type, $format = null)
6767
{
6868
return \is_subclass_of($type, DenormalizableInterface::class);
6969
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function hasCacheableSupportsMethod(): bool
75+
{
76+
return __CLASS__ === \get_class($this);
77+
}
7078
}

src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ public function supportsDenormalization($data, $type, $format = null)
119119
return isset(self::$supportedTypes[$type]);
120120
}
121121

122+
/**
123+
* {@inheritdoc}
124+
*/
125+
public function hasCacheableSupportsMethod(): bool
126+
{
127+
return __CLASS__ === \get_class($this);
128+
}
129+
122130
/**
123131
* Gets the mime type of the object. Defaults to application/octet-stream.
124132
*

src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public function supportsNormalization($data, $format = null)
5555
return $data instanceof \DateInterval;
5656
}
5757

58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function hasCacheableSupportsMethod(): bool
62+
{
63+
return __CLASS__ === \get_class($this);
64+
}
65+
5866
/**
5967
* {@inheritdoc}
6068
*

src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public function supportsDenormalization($data, $type, $format = null)
116116
return isset(self::$supportedTypes[$type]);
117117
}
118118

119+
/**
120+
* {@inheritdoc}
121+
*/
122+
public function hasCacheableSupportsMethod(): bool
123+
{
124+
return __CLASS__ === \get_class($this);
125+
}
126+
119127
/**
120128
* Formats datetime errors.
121129
*

src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public function supportsDenormalization($data, $type, $format = null)
5252
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
5353
}
5454

55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function hasCacheableSupportsMethod(): bool
59+
{
60+
return __CLASS__ === \get_class($this);
61+
}
62+
5563
/**
5664
* Checks if the given class has any get{Property} method.
5765
*/

src/Symfony/Component/Serializer/Normalizer/JsonSerializableNormalizer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Fred Cox <[email protected]>
2121
*/
22-
class JsonSerializableNormalizer extends AbstractNormalizer implements CacheableSupportsMethodInterface
22+
class JsonSerializableNormalizer extends AbstractNormalizer
2323
{
2424
/**
2525
* {@inheritdoc}
@@ -64,4 +64,12 @@ public function denormalize($data, $class, $format = null, array $context = arra
6464
{
6565
throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class));
6666
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function hasCacheableSupportsMethod(): bool
72+
{
73+
return __CLASS__ === \get_class($this);
74+
}
6775
}

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
4040
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
4141
}
4242

43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function hasCacheableSupportsMethod(): bool
47+
{
48+
return __CLASS__ === \get_class($this);
49+
}
50+
4351
/**
4452
* {@inheritdoc}
4553
*/

src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function supportsDenormalization($data, $type, $format = null)
4646
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
4747
}
4848

49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function hasCacheableSupportsMethod(): bool
53+
{
54+
return __CLASS__ === \get_class($this);
55+
}
56+
4957
/**
5058
* Checks if the given class has any non-static property.
5159
*/

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
use Symfony\Component\Serializer\Encoder\DecoderInterface;
2020
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
2121
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
22+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
2223
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
2324
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
2425
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
2526
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
2627
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2728
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2829
use Symfony\Component\Serializer\Exception\LogicException;
29-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
3030

3131
/**
3232
* Serializer serializes and deserializes data.
@@ -221,12 +221,11 @@ private function getNormalizer($data, ?string $format, array $context)
221221
continue;
222222
}
223223

224-
if (!$normalizer instanceof CacheableSupportsMethodInterface) {
224+
if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
225225
$this->normalizerCache[$format][$type][$k] = false;
226226
} elseif ($normalizer->supportsNormalization($data, $format)) {
227227
$this->normalizerCache[$format][$type][$k] = true;
228-
229-
return $normalizer;
228+
break;
230229
}
231230
}
232231
}
@@ -263,12 +262,11 @@ private function getDenormalizer($data, string $class, ?string $format, array $c
263262
continue;
264263
}
265264

266-
if (!$normalizer instanceof CacheableSupportsMethodInterface) {
265+
if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
267266
$this->denormalizerCache[$format][$class][$k] = false;
268267
} elseif ($normalizer->supportsDenormalization(null, $class, $format)) {
269268
$this->denormalizerCache[$format][$class][$k] = true;
270-
271-
return $normalizer;
269+
break;
272270
}
273271
}
274272
}

0 commit comments

Comments
 (0)