Skip to content

Commit e89c236

Browse files
[Serializer] Generalize caching support a bit
1 parent c1e850f commit e89c236

12 files changed

+75
-62
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
4.1.0
55
-----
66

7+
* added `CacheableSupportsMethodInterface` for normalizers and denormalizers that use
8+
only the type and the format in their `supports*()` methods
79
* added `MissingConstructorArgumentsException` new exception for deserialization failure
810
of objects that needs data insertion in constructor
911
* added an optional `default_constructor_arguments` option of context to specify a default data in

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@
3030
*
3131
* @author Kévin Dunglas <dunglas@gmail.com>
3232
*/
33-
abstract class AbstractObjectNormalizer extends AbstractNormalizer implements NormalizerWithCacheableSupportResultInterface
33+
abstract class AbstractObjectNormalizer extends AbstractNormalizer implements CacheableSupportsMethodInterface
3434
{
3535
const ENABLE_MAX_DEPTH = 'enable_max_depth';
3636
const DEPTH_KEY_PATTERN = 'depth_%s::%s';
3737
const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement';
3838

3939
private $propertyTypeExtractor;
4040
private $attributesCache = array();
41-
private $cache = array();
4241

4342
/**
4443
* @var callable|null
@@ -225,11 +224,7 @@ public function setMaxDepthHandler(?callable $handler): void
225224
*/
226225
public function supportsDenormalization($data, $type, $format = null)
227226
{
228-
if (!isset($this->cache[$type])) {
229-
$this->cache[$type] = class_exists($type) || (interface_exists($type) && null !== $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
230-
}
231-
232-
return $this->cache[$type];
227+
return \class_exists($type) || (\interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
233228
}
234229

235230
/**
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
namespace Symfony\Component\Serializer\Normalizer;
1313

1414
/**
15-
* "supportsNormalization()" methods of normalizers implementing this interface have a cacheable return.
15+
* Marker interface for normalizers and denormalizers that use
16+
* only the type and the format in their supports*() methods.
17+
*
18+
* By implementing this interface, the return value of the
19+
* supports*() methods will be cached by type and format.
1620
*
1721
* @author Kévin Dunglas <dunglas@gmail.com>
1822
*/
19-
interface NormalizerWithCacheableSupportResultInterface
23+
interface CacheableSupportsMethodInterface
2024
{
2125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2323
* @author Kévin Dunglas <dunglas@gmail.com>
2424
*/
25-
class ConstraintViolationListNormalizer implements NormalizerInterface, NormalizerWithCacheableSupportResultInterface
25+
class ConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
2626
{
2727
/**
2828
* {@inheritdoc}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
/**
1818
* @author Jordi Boggiano <j.boggiano@seld.be>
1919
*/
20-
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, NormalizerWithCacheableSupportResultInterface
20+
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
2121
{
2222
use ObjectToPopulateTrait;
2323
use SerializerAwareTrait;
2424

25-
private $cache = array();
26-
2725
/**
2826
* {@inheritdoc}
2927
*/
@@ -67,14 +65,6 @@ public function supportsNormalization($data, $format = null)
6765
*/
6866
public function supportsDenormalization($data, $type, $format = null)
6967
{
70-
if (isset($this->cache[$type])) {
71-
return $this->cache[$type];
72-
}
73-
74-
if (!class_exists($type)) {
75-
return $this->cache[$type] = false;
76-
}
77-
78-
return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
68+
return \is_subclass_of($type, DenormalizableInterface::class);
7969
}
8070
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Kévin Dunglas <dunglas@gmail.com>
2525
*/
26-
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerWithCacheableSupportResultInterface
26+
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
2727
{
2828
private static $supportedTypes = array(
2929
\SplFileInfo::class => true,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Jérôme Parmentier <jerome@prmntr.me>
2222
*/
23-
class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerWithCacheableSupportResultInterface
23+
class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
2424
{
2525
const FORMAT_KEY = 'dateinterval_format';
2626

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Kévin Dunglas <dunglas@gmail.com>
2222
*/
23-
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerWithCacheableSupportResultInterface
23+
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
2424
{
2525
const FORMAT_KEY = 'datetime_format';
2626
const TIMEZONE_KEY = 'datetime_timezone';

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,21 @@
3535
class GetSetMethodNormalizer extends AbstractObjectNormalizer
3636
{
3737
private static $setterAccessibleCache = array();
38-
private $cache = array();
3938

4039
/**
4140
* {@inheritdoc}
4241
*/
4342
public function supportsNormalization($data, $format = null)
4443
{
45-
return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
44+
return parent::supportsNormalization($data, $format) && $this->supports(\get_class($data));
4645
}
4746

4847
/**
4948
* {@inheritdoc}
5049
*/
5150
public function supportsDenormalization($data, $type, $format = null)
5251
{
53-
return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
52+
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
5453
}
5554

5655
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Fred Cox <mcfedr@gmail.com>
2121
*/
22-
class JsonSerializableNormalizer extends AbstractNormalizer implements NormalizerWithCacheableSupportResultInterface
22+
class JsonSerializableNormalizer extends AbstractNormalizer implements CacheableSupportsMethodInterface
2323
{
2424
/**
2525
* {@inheritdoc}

0 commit comments

Comments
 (0)