| 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\Normalizer\DenormalizerInterface; |
| 18 |
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 19 |
use Symfony\Component\Serializer\SerializerInterface; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class TraceableSerializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface |
| 29 |
{ |
| 30 |
public const DEBUG_TRACE_ID = 'debug_trace_id'; |
| 31 |
|
| 32 |
public function __construct( |
| 33 |
private SerializerInterface&NormalizerInterface&DenormalizerInterface&EncoderInterface&DecoderInterface $serializer, |
| 34 |
private SerializerDataCollector $dataCollector, |
| 35 |
private readonly string $serializerName = 'default', |
| 36 |
) { |
| 37 |
} |
| 38 |
|
| 39 |
public function serialize(mixed $data, string $format, array $context = []): string |
| 40 |
{ |
| 41 |
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4)); |
| 42 |
|
| 43 |
$startTime = microtime(true); |
| 44 |
$result = $this->serializer->serialize($data, $format, $context); |
| 45 |
$time = microtime(true) - $startTime; |
| 46 |
|
| 47 |
$caller = $this->getCaller(__FUNCTION__, SerializerInterface::class); |
| 48 |
|
| 49 |
$this->dataCollector->collectSerialize($traceId, $data, $format, $context, $time, $caller, $this->serializerName); |
| 50 |
|
| 51 |
return $result; |
| 52 |
} |
| 53 |
|
| 54 |
public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed |
| 55 |
{ |
| 56 |
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4)); |
| 57 |
|
| 58 |
$startTime = microtime(true); |
| 59 |
$result = $this->serializer->deserialize($data, $type, $format, $context); |
| 60 |
$time = microtime(true) - $startTime; |
| 61 |
|
| 62 |
$caller = $this->getCaller(__FUNCTION__, SerializerInterface::class); |
| 63 |
|
| 64 |
$this->dataCollector->collectDeserialize($traceId, $data, $type, $format, $context, $time, $caller, $this->serializerName); |
| 65 |
|
| 66 |
return $result; |
| 67 |
} |
| 68 |
|
| 69 |
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null |
| 70 |
{ |
| 71 |
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4)); |
| 72 |
|
| 73 |
$startTime = microtime(true); |
| 74 |
$result = $this->serializer->normalize($data, $format, $context); |
| 75 |
$time = microtime(true) - $startTime; |
| 76 |
|
| 77 |
$caller = $this->getCaller(__FUNCTION__, NormalizerInterface::class); |
| 78 |
|
| 79 |
$this->dataCollector->collectNormalize($traceId, $data, $format, $context, $time, $caller, $this->serializerName); |
| 80 |
|
| 81 |
return $result; |
| 82 |
} |
| 83 |
|
| 84 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 85 |
{ |
| 86 |
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4)); |
| 87 |
|
| 88 |
$startTime = microtime(true); |
| 89 |
$result = $this->serializer->denormalize($data, $type, $format, $context); |
| 90 |
$time = microtime(true) - $startTime; |
| 91 |
|
| 92 |
$caller = $this->getCaller(__FUNCTION__, DenormalizerInterface::class); |
| 93 |
|
| 94 |
$this->dataCollector->collectDenormalize($traceId, $data, $type, $format, $context, $time, $caller, $this->serializerName); |
| 95 |
|
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
public function encode(mixed $data, string $format, array $context = []): string |
| 100 |
{ |
| 101 |
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4)); |
| 102 |
|
| 103 |
$startTime = microtime(true); |
| 104 |
$result = $this->serializer->encode($data, $format, $context); |
| 105 |
$time = microtime(true) - $startTime; |
| 106 |
|
| 107 |
$caller = $this->getCaller(__FUNCTION__, EncoderInterface::class); |
| 108 |
|
| 109 |
$this->dataCollector->collectEncode($traceId, $data, $format, $context, $time, $caller, $this->serializerName); |
| 110 |
|
| 111 |
return $result; |
| 112 |
} |
| 113 |
|
| 114 |
public function decode(string $data, string $format, array $context = []): mixed |
| 115 |
{ |
| 116 |
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4)); |
| 117 |
|
| 118 |
$startTime = microtime(true); |
| 119 |
$result = $this->serializer->decode($data, $format, $context); |
| 120 |
$time = microtime(true) - $startTime; |
| 121 |
|
| 122 |
$caller = $this->getCaller(__FUNCTION__, DecoderInterface::class); |
| 123 |
|
| 124 |
$this->dataCollector->collectDecode($traceId, $data, $format, $context, $time, $caller, $this->serializerName); |
| 125 |
|
| 126 |
return $result; |
| 127 |
} |
| 128 |
|
| 129 |
public function getSupportedTypes(?string $format): array |
| 130 |
{ |
| 131 |
return $this->serializer->getSupportedTypes($format); |
| 132 |
} |
| 133 |
|
| 134 |
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 135 |
{ |
| 136 |
return $this->serializer->supportsNormalization($data, $format, $context); |
| 137 |
} |
| 138 |
|
| 139 |
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool |
| 140 |
{ |
| 141 |
return $this->serializer->supportsDenormalization($data, $type, $format, $context); |
| 142 |
} |
| 143 |
|
| 144 |
public function supportsEncoding(string $format, array $context = []): bool |
| 145 |
{ |
| 146 |
return $this->serializer->supportsEncoding($format, $context); |
| 147 |
} |
| 148 |
|
| 149 |
public function supportsDecoding(string $format, array $context = []): bool |
| 150 |
{ |
| 151 |
return $this->serializer->supportsDecoding($format, $context); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function __call(string $method, array $arguments): mixed |
| 158 |
{ |
| 159 |
return $this->serializer->{$method}(...$arguments); |
| 160 |
} |
| 161 |
|
| 162 |
private function getCaller(string $method, string $interface): array |
| 163 |
{ |
| 164 |
$trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 8); |
| 165 |
|
| 166 |
$file = $trace[0]['file']; |
| 167 |
$line = $trace[0]['line']; |
| 168 |
|
| 169 |
for ($i = 1; $i < 8; ++$i) { |
| 170 |
if (isset($trace[$i]['class'], $trace[$i]['function']) |
| 171 |
&& $method === $trace[$i]['function'] |
| 172 |
&& is_a($trace[$i]['class'], $interface, true) |
| 173 |
) { |
| 174 |
$file = $trace[$i]['file'] ?? $trace[$i + 1]['file']; |
| 175 |
$line = $trace[$i]['line'] ?? $trace[$i + 1]['line']; |
| 176 |
|
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
$name = str_replace('\\', '/', $file); |
| 182 |
$name = substr($name, strrpos($name, '/') + 1); |
| 183 |
|
| 184 |
return compact('name', 'file', 'line'); |
| 185 |
} |
| 186 |
} |