comparison vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
92 foreach ($stack as $attribute => $attributeValue) { 92 foreach ($stack as $attribute => $attributeValue) {
93 if (!$this->serializer instanceof NormalizerInterface) { 93 if (!$this->serializer instanceof NormalizerInterface) {
94 throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute)); 94 throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute));
95 } 95 }
96 96
97 $data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute))); 97 $data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute, $format)));
98 } 98 }
99 99
100 return $data; 100 return $data;
101 } 101 }
102 102
126 } 126 }
127 127
128 return $allowedAttributes; 128 return $allowedAttributes;
129 } 129 }
130 130
131 if (isset($context['attributes'])) { 131 $attributes = $this->extractAttributes($object, $format, $context);
132 return $this->extractAttributes($object, $format, $context); 132
133 } 133 if ($context['cache_key']) {
134 134 $this->attributesCache[$key] = $attributes;
135 if (isset($this->attributesCache[$class])) { 135 }
136 return $this->attributesCache[$class]; 136
137 } 137 return $attributes;
138
139 return $this->attributesCache[$class] = $this->extractAttributes($object, $format, $context);
140 } 138 }
141 139
142 /** 140 /**
143 * Extracts attributes to normalize from the class of the given object, format and context. 141 * Extracts attributes to normalize from the class of the given object, format and context.
144 * 142 *
274 if (Type::BUILTIN_TYPE_OBJECT === $builtinType) { 272 if (Type::BUILTIN_TYPE_OBJECT === $builtinType) {
275 if (!$this->serializer instanceof DenormalizerInterface) { 273 if (!$this->serializer instanceof DenormalizerInterface) {
276 throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class)); 274 throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class));
277 } 275 }
278 276
279 $childContext = $this->createChildContext($context, $attribute); 277 $childContext = $this->createChildContext($context, $attribute, $format);
280 if ($this->serializer->supportsDenormalization($data, $class, $format, $childContext)) { 278 if ($this->serializer->supportsDenormalization($data, $class, $format, $childContext)) {
281 return $this->serializer->denormalize($data, $class, $format, $childContext); 279 return $this->serializer->denormalize($data, $class, $format, $childContext);
282 } 280 }
283 } 281 }
284 282
371 369
372 return false; 370 return false;
373 } 371 }
374 372
375 /** 373 /**
376 * Gets the cache key to use. 374 * Overwritten to update the cache key for the child.
375 *
376 * We must not mix up the attribute cache between parent and children.
377 *
378 * {@inheritdoc}
379 */
380 protected function createChildContext(array $parentContext, $attribute/*, string $format = null */)
381 {
382 if (\func_num_args() >= 3) {
383 $format = \func_get_arg(2);
384 } else {
385 // will be deprecated in version 4
386 $format = null;
387 }
388
389 $context = parent::createChildContext($parentContext, $attribute, $format);
390 // format is already included in the cache_key of the parent.
391 $context['cache_key'] = $this->getCacheKey($format, $context);
392
393 return $context;
394 }
395
396 /**
397 * Builds the cache key for the attributes cache.
398 *
399 * The key must be different for every option in the context that could change which attributes should be handled.
377 * 400 *
378 * @param string|null $format 401 * @param string|null $format
379 * @param array $context 402 * @param array $context
380 * 403 *
381 * @return bool|string 404 * @return bool|string
382 */ 405 */
383 private function getCacheKey($format, array $context) 406 private function getCacheKey($format, array $context)
384 { 407 {
408 unset($context['cache_key']); // avoid artificially different keys
385 try { 409 try {
386 return md5($format.serialize($context)); 410 return md5($format.serialize([
411 'context' => $context,
412 'ignored' => $this->ignoredAttributes,
413 'camelized' => $this->camelizedAttributes,
414 ]));
387 } catch (\Exception $exception) { 415 } catch (\Exception $exception) {
388 // The context cannot be serialized, skip the cache 416 // The context cannot be serialized, skip the cache
389 return false; 417 return false;
390 } 418 }
391 } 419 }