| 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\Encoder\DecoderInterface; |
| 16 |
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
| 17 |
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface; |
| 18 |
use Symfony\Component\Serializer\SerializerAwareInterface; |
| 19 |
use Symfony\Component\Serializer\SerializerInterface; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class TraceableEncoder implements EncoderInterface, DecoderInterface, SerializerAwareInterface |
| 29 |
{ |
| 30 |
public function __construct( |
| 31 |
private EncoderInterface|DecoderInterface $encoder, |
| 32 |
private SerializerDataCollector $dataCollector, |
| 33 |
private readonly string $serializerName = 'default', |
| 34 |
) { |
| 35 |
} |
| 36 |
|
| 37 |
public function encode(mixed $data, string $format, array $context = []): string |
| 38 |
{ |
| 39 |
if (!$this->encoder instanceof EncoderInterface) { |
| 40 |
throw new \BadMethodCallException(\sprintf('The "%s()" method cannot be called as nested encoder doesn\'t implements "%s".', __METHOD__, EncoderInterface::class)); |
| 41 |
} |
| 42 |
|
| 43 |
$startTime = microtime(true); |
| 44 |
$encoded = $this->encoder->encode($data, $format, $context); |
| 45 |
$time = microtime(true) - $startTime; |
| 46 |
|
| 47 |
if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) { |
| 48 |
$this->dataCollector->collectEncoding($traceId, $this->encoder::class, $time, $this->serializerName); |
| 49 |
} |
| 50 |
|
| 51 |
return $encoded; |
| 52 |
} |
| 53 |
|
| 54 |
public function supportsEncoding(string $format, array $context = []): bool |
| 55 |
{ |
| 56 |
if (!$this->encoder instanceof EncoderInterface) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
return $this->encoder->supportsEncoding($format, $context); |
| 61 |
} |
| 62 |
|
| 63 |
public function decode(string $data, string $format, array $context = []): mixed |
| 64 |
{ |
| 65 |
if (!$this->encoder instanceof DecoderInterface) { |
| 66 |
throw new \BadMethodCallException(\sprintf('The "%s()" method cannot be called as nested encoder doesn\'t implements "%s".', __METHOD__, DecoderInterface::class)); |
| 67 |
} |
| 68 |
|
| 69 |
$startTime = microtime(true); |
| 70 |
$encoded = $this->encoder->decode($data, $format, $context); |
| 71 |
$time = microtime(true) - $startTime; |
| 72 |
|
| 73 |
if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) { |
| 74 |
$this->dataCollector->collectDecoding($traceId, $this->encoder::class, $time, $this->serializerName); |
| 75 |
} |
| 76 |
|
| 77 |
return $encoded; |
| 78 |
} |
| 79 |
|
| 80 |
public function supportsDecoding(string $format, array $context = []): bool |
| 81 |
{ |
| 82 |
if (!$this->encoder instanceof DecoderInterface) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
return $this->encoder->supportsDecoding($format, $context); |
| 87 |
} |
| 88 |
|
| 89 |
public function setSerializer(SerializerInterface $serializer): void |
| 90 |
{ |
| 91 |
if (!$this->encoder instanceof SerializerAwareInterface) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
$this->encoder->setSerializer($serializer); |
| 96 |
} |
| 97 |
|
| 98 |
public function needsNormalization(): bool |
| 99 |
{ |
| 100 |
return !$this->encoder instanceof NormalizationAwareInterface; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function __call(string $method, array $arguments): mixed |
| 107 |
{ |
| 108 |
return $this->encoder->{$method}(...$arguments); |
| 109 |
} |
| 110 |
} |