| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\DataCollector; |
| 13 |
|
| 14 |
use Symfony\Component\HttpFoundation\Request; |
| 15 |
use Symfony\Component\HttpFoundation\Response; |
| 16 |
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
| 17 |
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; |
| 18 |
use Symfony\Component\Serializer\Debug\TraceableSerializer; |
| 19 |
use Symfony\Component\VarDumper\Cloner\Data; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class SerializerDataCollector extends DataCollector implements LateDataCollectorInterface |
| 27 |
{ |
| 28 |
private const DATA_TEMPLATE = [ |
| 29 |
'serialize' => [], |
| 30 |
'deserialize' => [], |
| 31 |
'normalize' => [], |
| 32 |
'denormalize' => [], |
| 33 |
'encode' => [], |
| 34 |
'decode' => [], |
| 35 |
]; |
| 36 |
|
| 37 |
private array $dataGroupedByName; |
| 38 |
private array $collected = []; |
| 39 |
|
| 40 |
public function reset(): void |
| 41 |
{ |
| 42 |
$this->data = []; |
| 43 |
unset($this->dataGroupedByName); |
| 44 |
$this->collected = []; |
| 45 |
} |
| 46 |
|
| 47 |
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void |
| 48 |
{ |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
public function getName(): string |
| 53 |
{ |
| 54 |
return 'serializer'; |
| 55 |
} |
| 56 |
|
| 57 |
public function getData(?string $name = null): Data|array |
| 58 |
{ |
| 59 |
return null === $name ? $this->data : $this->getDataGroupedByName()[$name]; |
| 60 |
} |
| 61 |
|
| 62 |
public function getHandledCount(?string $name = null): int |
| 63 |
{ |
| 64 |
return array_sum(array_map('count', $this->getData($name))); |
| 65 |
} |
| 66 |
|
| 67 |
public function getTotalTime(): float |
| 68 |
{ |
| 69 |
$totalTime = 0; |
| 70 |
|
| 71 |
foreach ($this->data as $handled) { |
| 72 |
$totalTime += array_sum(array_map(static fn (array $el): float => $el['time'], $handled)); |
| 73 |
} |
| 74 |
|
| 75 |
return $totalTime; |
| 76 |
} |
| 77 |
|
| 78 |
public function getSerializerNames(): array |
| 79 |
{ |
| 80 |
return array_keys($this->getDataGroupedByName()); |
| 81 |
} |
| 82 |
|
| 83 |
public function collectSerialize(string $traceId, mixed $data, string $format, array $context, float $time, array $caller, string $name): void |
| 84 |
{ |
| 85 |
unset($context[TraceableSerializer::DEBUG_TRACE_ID]); |
| 86 |
|
| 87 |
$this->collected[$traceId] = array_merge( |
| 88 |
$this->collected[$traceId] ?? [], |
| 89 |
compact('data', 'format', 'context', 'time', 'caller', 'name'), |
| 90 |
['method' => 'serialize'], |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
public function collectDeserialize(string $traceId, mixed $data, string $type, string $format, array $context, float $time, array $caller, string $name): void |
| 95 |
{ |
| 96 |
unset($context[TraceableSerializer::DEBUG_TRACE_ID]); |
| 97 |
|
| 98 |
$this->collected[$traceId] = array_merge( |
| 99 |
$this->collected[$traceId] ?? [], |
| 100 |
compact('data', 'format', 'type', 'context', 'time', 'caller', 'name'), |
| 101 |
['method' => 'deserialize'], |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
public function collectNormalize(string $traceId, mixed $data, ?string $format, array $context, float $time, array $caller, string $name): void |
| 106 |
{ |
| 107 |
unset($context[TraceableSerializer::DEBUG_TRACE_ID]); |
| 108 |
|
| 109 |
$this->collected[$traceId] = array_merge( |
| 110 |
$this->collected[$traceId] ?? [], |
| 111 |
compact('data', 'format', 'context', 'time', 'caller', 'name'), |
| 112 |
['method' => 'normalize'], |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
public function collectDenormalize(string $traceId, mixed $data, string $type, ?string $format, array $context, float $time, array $caller, string $name): void |
| 117 |
{ |
| 118 |
unset($context[TraceableSerializer::DEBUG_TRACE_ID]); |
| 119 |
|
| 120 |
$this->collected[$traceId] = array_merge( |
| 121 |
$this->collected[$traceId] ?? [], |
| 122 |
compact('data', 'format', 'type', 'context', 'time', 'caller', 'name'), |
| 123 |
['method' => 'denormalize'], |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
public function collectEncode(string $traceId, mixed $data, ?string $format, array $context, float $time, array $caller, string $name): void |
| 128 |
{ |
| 129 |
unset($context[TraceableSerializer::DEBUG_TRACE_ID]); |
| 130 |
|
| 131 |
$this->collected[$traceId] = array_merge( |
| 132 |
$this->collected[$traceId] ?? [], |
| 133 |
compact('data', 'format', 'context', 'time', 'caller', 'name'), |
| 134 |
['method' => 'encode'], |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
public function collectDecode(string $traceId, mixed $data, ?string $format, array $context, float $time, array $caller, string $name): void |
| 139 |
{ |
| 140 |
unset($context[TraceableSerializer::DEBUG_TRACE_ID]); |
| 141 |
|
| 142 |
$this->collected[$traceId] = array_merge( |
| 143 |
$this->collected[$traceId] ?? [], |
| 144 |
compact('data', 'format', 'context', 'time', 'caller', 'name'), |
| 145 |
['method' => 'decode'], |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
public function collectNormalization(string $traceId, string $normalizer, float $time, string $name): void |
| 150 |
{ |
| 151 |
$method = 'normalize'; |
| 152 |
|
| 153 |
$this->collected[$traceId]['normalization'][] = compact('normalizer', 'method', 'time', 'name'); |
| 154 |
} |
| 155 |
|
| 156 |
public function collectDenormalization(string $traceId, string $normalizer, float $time, string $name): void |
| 157 |
{ |
| 158 |
$method = 'denormalize'; |
| 159 |
|
| 160 |
$this->collected[$traceId]['normalization'][] = compact('normalizer', 'method', 'time', 'name'); |
| 161 |
} |
| 162 |
|
| 163 |
public function collectEncoding(string $traceId, string $encoder, float $time, string $name): void |
| 164 |
{ |
| 165 |
$method = 'encode'; |
| 166 |
|
| 167 |
$this->collected[$traceId]['encoding'][] = compact('encoder', 'method', 'time', 'name'); |
| 168 |
} |
| 169 |
|
| 170 |
public function collectDecoding(string $traceId, string $encoder, float $time, string $name): void |
| 171 |
{ |
| 172 |
$method = 'decode'; |
| 173 |
|
| 174 |
$this->collected[$traceId]['encoding'][] = compact('encoder', 'method', 'time', 'name'); |
| 175 |
} |
| 176 |
|
| 177 |
public function lateCollect(): void |
| 178 |
{ |
| 179 |
$this->data = self::DATA_TEMPLATE; |
| 180 |
|
| 181 |
foreach ($this->collected as $collected) { |
| 182 |
if (!isset($collected['data'])) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
$data = [ |
| 187 |
'data' => $this->cloneVar($collected['data']), |
| 188 |
'dataType' => get_debug_type($collected['data']), |
| 189 |
'type' => $collected['type'] ?? null, |
| 190 |
'format' => $collected['format'], |
| 191 |
'time' => $collected['time'], |
| 192 |
'context' => $this->cloneVar($collected['context']), |
| 193 |
'normalization' => [], |
| 194 |
'encoding' => [], |
| 195 |
'caller' => $collected['caller'] ?? null, |
| 196 |
'name' => $collected['name'], |
| 197 |
]; |
| 198 |
|
| 199 |
if (isset($collected['normalization'])) { |
| 200 |
$mainNormalization = array_pop($collected['normalization']); |
| 201 |
|
| 202 |
$data['normalizer'] = ['time' => $mainNormalization['time']] + $this->getMethodLocation($mainNormalization['normalizer'], $mainNormalization['method']); |
| 203 |
|
| 204 |
foreach ($collected['normalization'] as $normalization) { |
| 205 |
if (!isset($data['normalization'][$normalization['normalizer']])) { |
| 206 |
$data['normalization'][$normalization['normalizer']] = ['time' => 0, 'calls' => 0] + $this->getMethodLocation($normalization['normalizer'], $normalization['method']); |
| 207 |
} |
| 208 |
|
| 209 |
++$data['normalization'][$normalization['normalizer']]['calls']; |
| 210 |
$data['normalization'][$normalization['normalizer']]['time'] += $normalization['time']; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (isset($collected['encoding'])) { |
| 215 |
$mainEncoding = array_pop($collected['encoding']); |
| 216 |
|
| 217 |
$data['encoder'] = ['time' => $mainEncoding['time']] + $this->getMethodLocation($mainEncoding['encoder'], $mainEncoding['method']); |
| 218 |
|
| 219 |
foreach ($collected['encoding'] as $encoding) { |
| 220 |
if (!isset($data['encoding'][$encoding['encoder']])) { |
| 221 |
$data['encoding'][$encoding['encoder']] = ['time' => 0, 'calls' => 0] + $this->getMethodLocation($encoding['encoder'], $encoding['method']); |
| 222 |
} |
| 223 |
|
| 224 |
++$data['encoding'][$encoding['encoder']]['calls']; |
| 225 |
$data['encoding'][$encoding['encoder']]['time'] += $encoding['time']; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
$this->data[$collected['method']][] = $data; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
private function getDataGroupedByName(): array |
| 234 |
{ |
| 235 |
if (!isset($this->dataGroupedByName)) { |
| 236 |
$this->dataGroupedByName = []; |
| 237 |
|
| 238 |
foreach ($this->data as $method => $items) { |
| 239 |
foreach ($items as $item) { |
| 240 |
$this->dataGroupedByName[$item['name']] ??= self::DATA_TEMPLATE; |
| 241 |
$this->dataGroupedByName[$item['name']][$method][] = $item; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
return $this->dataGroupedByName; |
| 247 |
} |
| 248 |
|
| 249 |
private function getMethodLocation(string $class, string $method): array |
| 250 |
{ |
| 251 |
$reflection = new \ReflectionClass($class); |
| 252 |
|
| 253 |
return [ |
| 254 |
'class' => $reflection->getShortName(), |
| 255 |
'file' => $reflection->getFileName(), |
| 256 |
'line' => $reflection->getMethod($method)->getStartLine(), |
| 257 |
]; |
| 258 |
} |
| 259 |
} |