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

Skip to content

Commit 44dd0ad

Browse files
[Seriaizer] Add local cache to normalizers
1 parent 40a3466 commit 44dd0ad

8 files changed

+40
-48
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
3636

3737
private $propertyTypeExtractor;
3838
private $attributesCache = array();
39+
private $cache = array();
3940

4041
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
4142
{
@@ -49,7 +50,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
4950
*/
5051
public function supportsNormalization($data, $format = null)
5152
{
52-
return is_object($data) && !$data instanceof \Traversable;
53+
return \is_object($data) && !$data instanceof \Traversable;
5354
}
5455

5556
/**
@@ -163,7 +164,7 @@ abstract protected function getAttributeValue($object, $attribute, $format = nul
163164
*/
164165
public function supportsDenormalization($data, $type, $format = null)
165166
{
166-
return class_exists($type);
167+
return isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = class_exists($type);
167168
}
168169

169170
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
6666
*/
6767
public function supportsDenormalization($data, $type, $format = null/*, array $context = array()*/)
6868
{
69-
$context = func_num_args() > 3 ? func_get_arg(3) : array();
69+
$context = \func_num_args() > 3 ? func_get_arg(3) : array();
7070

7171
return '[]' === substr($type, -2)
7272
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
2121
{
22+
private $cache = array();
23+
2224
use SerializerAwareTrait;
2325

2426
/**
@@ -64,10 +66,14 @@ public function supportsNormalization($data, $format = null)
6466
*/
6567
public function supportsDenormalization($data, $type, $format = null)
6668
{
69+
if (isset($this->cache[$type])) {
70+
return $this->cache[$type];
71+
}
72+
6773
if (!class_exists($type)) {
68-
return false;
74+
return $this->cache[$type] = false;
6975
}
7076

71-
return is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
77+
return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
7278
}
7379
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
*/
2626
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface
2727
{
28+
private static $supportedTypes = array(
29+
\SplFileInfo::class => true,
30+
\SplFileObject::class => true,
31+
File::class => true,
32+
);
33+
2834
/**
2935
* @var MimeTypeGuesserInterface
3036
*/
@@ -107,13 +113,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
107113
*/
108114
public function supportsDenormalization($data, $type, $format = null)
109115
{
110-
$supportedTypes = array(
111-
\SplFileInfo::class => true,
112-
\SplFileObject::class => true,
113-
'Symfony\Component\HttpFoundation\File\File' => true,
114-
);
115-
116-
return isset($supportedTypes[$type]);
116+
return isset(self::$supportedTypes[$type]);
117117
}
118118

119119
/**

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface
3131
private $format;
3232
private $timezone;
3333

34+
private static $supportedTypes = array(
35+
\DateTimeInterface::class => true,
36+
\DateTimeImmutable::class => true,
37+
\DateTime::class => true,
38+
);
39+
3440
/**
3541
* @param string $format
3642
* @param \DateTimeZone|null $timezone
@@ -115,13 +121,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
115121
*/
116122
public function supportsDenormalization($data, $type, $format = null)
117123
{
118-
$supportedTypes = array(
119-
\DateTimeInterface::class => true,
120-
\DateTimeImmutable::class => true,
121-
\DateTime::class => true,
122-
);
123-
124-
return isset($supportedTypes[$type]);
124+
return isset(self::$supportedTypes[$type]);
125125
}
126126

127127
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
class GetSetMethodNormalizer extends AbstractObjectNormalizer
3636
{
3737
private static $setterAccessibleCache = array();
38+
private $cache = array();
3839

3940
/**
4041
* {@inheritdoc}
4142
*/
4243
public function supportsNormalization($data, $format = null)
4344
{
44-
return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
45+
return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
4546
}
4647

4748
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
*/
3131
class PropertyNormalizer extends AbstractObjectNormalizer
3232
{
33+
private $cache = array();
34+
3335
/**
3436
* {@inheritdoc}
3537
*/
3638
public function supportsNormalization($data, $format = null)
3739
{
38-
return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
40+
return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
3941
}
4042

4143
/**

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,15 @@ public function normalize($data, $format = null, array $context = array())
171171
*/
172172
public function denormalize($data, $type, $format = null, array $context = array())
173173
{
174-
return $this->denormalizeObject($data, $type, $format, $context);
174+
if (!$this->normalizers) {
175+
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
176+
}
177+
178+
if ($normalizer = $this->getDenormalizer($data, $class, $format, $context)) {
179+
return $normalizer->denormalize($data, $class, $format, $context);
180+
}
181+
182+
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
175183
}
176184

177185
/**
@@ -269,32 +277,6 @@ final public function decode($data, $format, array $context = array())
269277
return $this->decoder->decode($data, $format, $context);
270278
}
271279

272-
/**
273-
* Denormalizes data back into an object of the given class.
274-
*
275-
* @param mixed $data data to restore
276-
* @param string $class the expected class to instantiate
277-
* @param string $format format name, present to give the option to normalizers to act differently based on formats
278-
* @param array $context The context data for this particular denormalization
279-
*
280-
* @return object
281-
*
282-
* @throws LogicException
283-
* @throws UnexpectedValueException
284-
*/
285-
private function denormalizeObject($data, $class, $format, array $context = array())
286-
{
287-
if (!$this->normalizers) {
288-
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
289-
}
290-
291-
if ($normalizer = $this->getDenormalizer($data, $class, $format, $context)) {
292-
return $normalizer->denormalize($data, $class, $format, $context);
293-
}
294-
295-
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
296-
}
297-
298280
/**
299281
* {@inheritdoc}
300282
*/

0 commit comments

Comments
 (0)