Skip to content

Commit

Permalink
avoid unnecessary cache key changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Apr 8, 2019
1 parent e43accb commit be2e3fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Expand Up @@ -397,7 +397,7 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
}
$parameterClass = $parameter->getClass()->getName();

return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName));
return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName, $format));
}

return $parameterData;
Expand All @@ -407,14 +407,15 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
}

/**
* @param array $parentContext
* @param string $attribute
* @param array $parentContext
* @param string $attribute Attribute name
* @param string|null $format
*
* @return array
*
* @internal
*/
protected function createChildContext(array $parentContext, $attribute)
protected function createChildContext(array $parentContext, $attribute/*, ?string $format*/)
{
if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
$parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
Expand Down
Expand Up @@ -58,7 +58,7 @@ public function supportsNormalization($data, $format = null)
public function normalize($object, $format = null, array $context = [])
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getAttributeCacheKey($format, $context);
$context['cache_key'] = $this->getCacheKey($format, $context);
}

if ($this->isCircularReference($object, $context)) {
Expand Down Expand Up @@ -94,7 +94,7 @@ public function normalize($object, $format = null, array $context = [])
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute));
}

$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute)));
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute, $format)));
}

return $data;
Expand Down Expand Up @@ -174,7 +174,7 @@ public function supportsDenormalization($data, $type, $format = null)
public function denormalize($data, $class, $format = null, array $context = [])
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getAttributeCacheKey($format, $context);
$context['cache_key'] = $this->getCacheKey($format, $context);
}

$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
Expand Down Expand Up @@ -274,7 +274,7 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class));
}

$childContext = $this->createChildContext($context, $attribute);
$childContext = $this->createChildContext($context, $attribute, $format);
if ($this->serializer->supportsDenormalization($data, $class, $format, $childContext)) {
return $this->serializer->denormalize($data, $class, $format, $childContext);
}
Expand Down Expand Up @@ -377,11 +377,19 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute
*
* {@inheritdoc}
*/
protected function createChildContext(array $parentContext, $attribute)
protected function createChildContext(array $parentContext, $attribute/*, string $format = null*/)
{
$context = parent::createChildContext($parentContext, $attribute);
if (\func_num_args() >= 3) {
$format = \func_get_arg(2);
} else {
@trigger_error(sprintf('Method %s::%s() will have a 3rd `string $format = null` argument in version 5.0. Not defining it is deprecated since Symfony 3.4.', \get_class($this), __FUNCTION__), E_USER_DEPRECATED);

$format = null;
}

$context = parent::createChildContext($parentContext, $attribute, $format);
// format is already included in the cache_key of the parent.
$context['cache_key'] = $this->getAttributeCacheKey('', $context);
$context['cache_key'] = $this->getCacheKey($format, $context);

return $context;
}
Expand All @@ -396,8 +404,9 @@ protected function createChildContext(array $parentContext, $attribute)
*
* @return bool|string
*/
private function getAttributeCacheKey($format, array $context)
private function getCacheKey($format, array $context)
{
unset($context['cache_key']); // avoid artifically different keys
try {
return md5($format.serialize([
'context' => $context,
Expand Down

0 comments on commit be2e3fa

Please sign in to comment.