| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Debug; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\DataCollector\SerializerDataCollector; |
| 15 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
| 16 |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 17 |
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
| 18 |
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 19 |
use Symfony\Component\Serializer\SerializerAwareInterface; |
| 20 |
use Symfony\Component\Serializer\SerializerInterface; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class TraceableNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, NormalizerAwareInterface, DenormalizerAwareInterface |
| 30 |
{ |
| 31 |
public function __construct( |
| 32 |
private NormalizerInterface|DenormalizerInterface $normalizer, |
| 33 |
private SerializerDataCollector $dataCollector, |
| 34 |
private readonly string $serializerName = 'default', |
| 35 |
) { |
| 36 |
} |
| 37 |
|
| 38 |
public function getSupportedTypes(?string $format): array |
| 39 |
{ |
| 40 |
return $this->normalizer->getSupportedTypes($format); |
| 41 |
} |
| 42 |
|
| 43 |
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null |
| 44 |
{ |
| 45 |
if (!$this->normalizer instanceof NormalizerInterface) { |
| 46 |
throw new \BadMethodCallException(\sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".', __METHOD__, NormalizerInterface::class)); |
| 47 |
} |
| 48 |
|
| 49 |
$startTime = microtime(true); |
| 50 |
$normalized = $this->normalizer->normalize($data, $format, $context); |
| 51 |
$time = microtime(true) - $startTime; |
| 52 |
|
| 53 |
if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) { |
| 54 |
$this->dataCollector->collectNormalization($traceId, $this->normalizer::class, $time, $this->serializerName); |
| 55 |
} |
| 56 |
|
| 57 |
return $normalized; |
| 58 |
} |
| 59 |
|
| 60 |
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 61 |
{ |
| 62 |
if (!$this->normalizer instanceof NormalizerInterface) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
return $this->normalizer->supportsNormalization($data, $format, $context); |
| 67 |
} |
| 68 |
|
| 69 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 70 |
{ |
| 71 |
if (!$this->normalizer instanceof DenormalizerInterface) { |
| 72 |
throw new \BadMethodCallException(\sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".', __METHOD__, DenormalizerInterface::class)); |
| 73 |
} |
| 74 |
|
| 75 |
$startTime = microtime(true); |
| 76 |
$denormalized = $this->normalizer->denormalize($data, $type, $format, $context); |
| 77 |
$time = microtime(true) - $startTime; |
| 78 |
|
| 79 |
if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) { |
| 80 |
$this->dataCollector->collectDenormalization($traceId, $this->normalizer::class, $time, $this->serializerName); |
| 81 |
} |
| 82 |
|
| 83 |
return $denormalized; |
| 84 |
} |
| 85 |
|
| 86 |
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool |
| 87 |
{ |
| 88 |
if (!$this->normalizer instanceof DenormalizerInterface) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
return $this->normalizer->supportsDenormalization($data, $type, $format, $context); |
| 93 |
} |
| 94 |
|
| 95 |
public function setSerializer(SerializerInterface $serializer): void |
| 96 |
{ |
| 97 |
if (!$this->normalizer instanceof SerializerAwareInterface) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$this->normalizer->setSerializer($serializer); |
| 102 |
} |
| 103 |
|
| 104 |
public function setNormalizer(NormalizerInterface $normalizer): void |
| 105 |
{ |
| 106 |
if (!$this->normalizer instanceof NormalizerAwareInterface) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$this->normalizer->setNormalizer($normalizer); |
| 111 |
} |
| 112 |
|
| 113 |
public function setDenormalizer(DenormalizerInterface $denormalizer): void |
| 114 |
{ |
| 115 |
if (!$this->normalizer instanceof DenormalizerAwareInterface) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
$this->normalizer->setDenormalizer($denormalizer); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function __call(string $method, array $arguments): mixed |
| 126 |
{ |
| 127 |
return $this->normalizer->{$method}(...$arguments); |
| 128 |
} |
| 129 |
} |