TranslationDataCollector.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  14. use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
  15. use Symfony\Component\Translation\DataCollectorTranslator;
  16. use Symfony\Component\VarDumper\Cloner\Data;
  17. /**
  18. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  19. *
  20. * @final
  21. */
  22. class TranslationDataCollector extends DataCollector implements LateDataCollectorInterface
  23. {
  24. private $translator;
  25. public function __construct(DataCollectorTranslator $translator)
  26. {
  27. $this->translator = $translator;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function lateCollect()
  33. {
  34. $messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
  35. $this->data += $this->computeCount($messages);
  36. $this->data['messages'] = $messages;
  37. $this->data = $this->cloneVar($this->data);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function collect(Request $request, Response $response, \Throwable $exception = null)
  43. {
  44. $this->data['locale'] = $this->translator->getLocale();
  45. $this->data['fallback_locales'] = $this->translator->getFallbackLocales();
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function reset()
  51. {
  52. $this->data = [];
  53. }
  54. public function getMessages(): array|Data
  55. {
  56. return $this->data['messages'] ?? [];
  57. }
  58. public function getCountMissings(): int
  59. {
  60. return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0;
  61. }
  62. public function getCountFallbacks(): int
  63. {
  64. return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0;
  65. }
  66. public function getCountDefines(): int
  67. {
  68. return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0;
  69. }
  70. public function getLocale()
  71. {
  72. return !empty($this->data['locale']) ? $this->data['locale'] : null;
  73. }
  74. /**
  75. * @internal
  76. */
  77. public function getFallbackLocales()
  78. {
  79. return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getName(): string
  85. {
  86. return 'translation';
  87. }
  88. private function sanitizeCollectedMessages(array $messages)
  89. {
  90. $result = [];
  91. foreach ($messages as $key => $message) {
  92. $messageId = $message['locale'].$message['domain'].$message['id'];
  93. if (!isset($result[$messageId])) {
  94. $message['count'] = 1;
  95. $message['parameters'] = !empty($message['parameters']) ? [$message['parameters']] : [];
  96. $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
  97. $result[$messageId] = $message;
  98. } else {
  99. if (!empty($message['parameters'])) {
  100. $result[$messageId]['parameters'][] = $message['parameters'];
  101. }
  102. ++$result[$messageId]['count'];
  103. }
  104. unset($messages[$key]);
  105. }
  106. return $result;
  107. }
  108. private function computeCount(array $messages)
  109. {
  110. $count = [
  111. DataCollectorTranslator::MESSAGE_DEFINED => 0,
  112. DataCollectorTranslator::MESSAGE_MISSING => 0,
  113. DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
  114. ];
  115. foreach ($messages as $message) {
  116. ++$count[$message['state']];
  117. }
  118. return $count;
  119. }
  120. private function sanitizeString(string $string, int $length = 80)
  121. {
  122. $string = trim(preg_replace('/\s+/', ' ', $string));
  123. if (false !== $encoding = mb_detect_encoding($string, null, true)) {
  124. if (mb_strlen($string, $encoding) > $length) {
  125. return mb_substr($string, 0, $length - 3, $encoding).'...';
  126. }
  127. } elseif (\strlen($string) > $length) {
  128. return substr($string, 0, $length - 3).'...';
  129. }
  130. return $string;
  131. }
  132. }