Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aad62c4

Browse filesBrowse files
minor #24228 [Serializer] Add local cache to normalizers (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [Serializer] Add local cache to normalizers | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24206 | License | MIT | Doc PR | - Should help making the Serializer a bit faster. Commits ------- b0c5cf0 [Serializer] Add local cache to normalizers
2 parents 779e8ed + b0c5cf0 commit aad62c4
Copy full SHA for aad62c4

File tree

Expand file treeCollapse file tree

8 files changed

+42
-50
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+42
-50
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+3-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php
+8-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php
+7-7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
+7-7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,22 @@
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
/**
4849
* {@inheritdoc}
4950
*/
5051
public function supportsDenormalization($data, $type, $format = null)
5152
{
52-
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
53+
return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
5354
}
5455

5556
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,22 @@
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
/**
4244
* {@inheritdoc}
4345
*/
4446
public function supportsDenormalization($data, $type, $format = null)
4547
{
46-
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
48+
return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
4749
}
4850

4951
/**

‎src/Symfony/Component/Serializer/Serializer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Serializer.php
+9-27Lines 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, $type, $format, $context)) {
179+
return $normalizer->denormalize($data, $type, $format, $context);
180+
}
181+
182+
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $type));
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.