| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\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\Translation\DataCollectorTranslator; |
| 19 |
use Symfony\Component\VarDumper\Cloner\Data; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class TranslationDataCollector extends DataCollector implements LateDataCollectorInterface |
| 27 |
{ |
| 28 |
public function __construct( |
| 29 |
private DataCollectorTranslator $translator, |
| 30 |
) { |
| 31 |
} |
| 32 |
|
| 33 |
public function lateCollect(): void |
| 34 |
{ |
| 35 |
$messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages()); |
| 36 |
|
| 37 |
$this->data += $this->computeCount($messages); |
| 38 |
$this->data['messages'] = $messages; |
| 39 |
|
| 40 |
$this->data = $this->cloneVar($this->data); |
| 41 |
} |
| 42 |
|
| 43 |
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void |
| 44 |
{ |
| 45 |
$this->data['locale'] = $this->translator->getLocale(); |
| 46 |
$this->data['fallback_locales'] = $this->translator->getFallbackLocales(); |
| 47 |
$this->data['global_parameters'] = $this->translator->getGlobalParameters(); |
| 48 |
} |
| 49 |
|
| 50 |
public function reset(): void |
| 51 |
{ |
| 52 |
$this->data = []; |
| 53 |
} |
| 54 |
|
| 55 |
public function getMessages(): array|Data |
| 56 |
{ |
| 57 |
return $this->data['messages'] ?? []; |
| 58 |
} |
| 59 |
|
| 60 |
public function getCountMissings(): int |
| 61 |
{ |
| 62 |
return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0; |
| 63 |
} |
| 64 |
|
| 65 |
public function getCountFallbacks(): int |
| 66 |
{ |
| 67 |
return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0; |
| 68 |
} |
| 69 |
|
| 70 |
public function getCountDefines(): int |
| 71 |
{ |
| 72 |
return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0; |
| 73 |
} |
| 74 |
|
| 75 |
public function getLocale(): ?string |
| 76 |
{ |
| 77 |
return !empty($this->data['locale']) ? $this->data['locale'] : null; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function getFallbackLocales(): Data|array |
| 84 |
{ |
| 85 |
return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : []; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function getGlobalParameters(): Data|array |
| 92 |
{ |
| 93 |
return $this->data['global_parameters'] ?? []; |
| 94 |
} |
| 95 |
|
| 96 |
public function getName(): string |
| 97 |
{ |
| 98 |
return 'translation'; |
| 99 |
} |
| 100 |
|
| 101 |
private function sanitizeCollectedMessages(array $messages): array |
| 102 |
{ |
| 103 |
$result = []; |
| 104 |
foreach ($messages as $key => $message) { |
| 105 |
$messageId = $message['locale'].$message['domain'].$message['id']; |
| 106 |
|
| 107 |
if (!isset($result[$messageId])) { |
| 108 |
$message['count'] = 1; |
| 109 |
$message['parameters'] = !empty($message['parameters']) ? [$message['parameters']] : []; |
| 110 |
$messages[$key]['translation'] = $this->sanitizeString($message['translation']); |
| 111 |
$result[$messageId] = $message; |
| 112 |
} else { |
| 113 |
if (!empty($message['parameters'])) { |
| 114 |
$result[$messageId]['parameters'][] = $message['parameters']; |
| 115 |
} |
| 116 |
|
| 117 |
++$result[$messageId]['count']; |
| 118 |
} |
| 119 |
|
| 120 |
unset($messages[$key]); |
| 121 |
} |
| 122 |
|
| 123 |
return $result; |
| 124 |
} |
| 125 |
|
| 126 |
private function computeCount(array $messages): array |
| 127 |
{ |
| 128 |
$count = [ |
| 129 |
DataCollectorTranslator::MESSAGE_DEFINED => 0, |
| 130 |
DataCollectorTranslator::MESSAGE_MISSING => 0, |
| 131 |
DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0, |
| 132 |
]; |
| 133 |
|
| 134 |
foreach ($messages as $message) { |
| 135 |
++$count[$message['state']]; |
| 136 |
} |
| 137 |
|
| 138 |
return $count; |
| 139 |
} |
| 140 |
|
| 141 |
private function sanitizeString(string $string, int $length = 80): string |
| 142 |
{ |
| 143 |
$string = trim(preg_replace('/\s+/', ' ', $string)); |
| 144 |
|
| 145 |
if (false !== $encoding = mb_detect_encoding($string, null, true)) { |
| 146 |
if (mb_strlen($string, $encoding) > $length) { |
| 147 |
return mb_substr($string, 0, $length - 3, $encoding).'...'; |
| 148 |
} |
| 149 |
} elseif (\strlen($string) > $length) { |
| 150 |
return substr($string, 0, $length - 3).'...'; |
| 151 |
} |
| 152 |
|
| 153 |
return $string; |
| 154 |
} |
| 155 |
} |