TranslationDataCollector.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 since Symfony 4.4
  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. * @param \Throwable|null $exception
  43. */
  44. public function collect(Request $request, Response $response/* , \Throwable $exception = null */)
  45. {
  46. $this->data['locale'] = $this->translator->getLocale();
  47. $this->data['fallback_locales'] = $this->translator->getFallbackLocales();
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function reset()
  53. {
  54. $this->data = [];
  55. }
  56. /**
  57. * @return array|Data
  58. */
  59. public function getMessages()
  60. {
  61. return $this->data['messages'] ?? [];
  62. }
  63. /**
  64. * @return int
  65. */
  66. public function getCountMissings()
  67. {
  68. return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0;
  69. }
  70. /**
  71. * @return int
  72. */
  73. public function getCountFallbacks()
  74. {
  75. return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0;
  76. }
  77. /**
  78. * @return int
  79. */
  80. public function getCountDefines()
  81. {
  82. return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0;
  83. }
  84. public function getLocale()
  85. {
  86. return !empty($this->data['locale']) ? $this->data['locale'] : null;
  87. }
  88. /**
  89. * @internal since Symfony 4.2
  90. */
  91. public function getFallbackLocales()
  92. {
  93. return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getName()
  99. {
  100. return 'translation';
  101. }
  102. private function sanitizeCollectedMessages(array $messages)
  103. {
  104. $result = [];
  105. foreach ($messages as $key => $message) {
  106. $messageId = $message['locale'].$message['domain'].$message['id'];
  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. ++$result[$messageId]['count'];
  117. }
  118. unset($messages[$key]);
  119. }
  120. return $result;
  121. }
  122. private function computeCount(array $messages)
  123. {
  124. $count = [
  125. DataCollectorTranslator::MESSAGE_DEFINED => 0,
  126. DataCollectorTranslator::MESSAGE_MISSING => 0,
  127. DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
  128. ];
  129. foreach ($messages as $message) {
  130. ++$count[$message['state']];
  131. }
  132. return $count;
  133. }
  134. private function sanitizeString(string $string, int $length = 80)
  135. {
  136. $string = trim(preg_replace('/\s+/', ' ', $string));
  137. if (false !== $encoding = mb_detect_encoding($string, null, true)) {
  138. if (mb_strlen($string, $encoding) > $length) {
  139. return mb_substr($string, 0, $length - 3, $encoding).'...';
  140. }
  141. } elseif (\strlen($string) > $length) {
  142. return substr($string, 0, $length - 3).'...';
  143. }
  144. return $string;
  145. }
  146. }