You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.5 KiB

3 years ago
  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. /**
  55. * @return array|Data
  56. */
  57. public function getMessages()
  58. {
  59. return $this->data['messages'] ?? [];
  60. }
  61. /**
  62. * @return int
  63. */
  64. public function getCountMissings()
  65. {
  66. return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0;
  67. }
  68. /**
  69. * @return int
  70. */
  71. public function getCountFallbacks()
  72. {
  73. return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0;
  74. }
  75. /**
  76. * @return int
  77. */
  78. public function getCountDefines()
  79. {
  80. return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0;
  81. }
  82. public function getLocale()
  83. {
  84. return !empty($this->data['locale']) ? $this->data['locale'] : null;
  85. }
  86. /**
  87. * @internal
  88. */
  89. public function getFallbackLocales()
  90. {
  91. return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getName()
  97. {
  98. return 'translation';
  99. }
  100. private function sanitizeCollectedMessages(array $messages)
  101. {
  102. $result = [];
  103. foreach ($messages as $key => $message) {
  104. $messageId = $message['locale'].$message['domain'].$message['id'];
  105. if (!isset($result[$messageId])) {
  106. $message['count'] = 1;
  107. $message['parameters'] = !empty($message['parameters']) ? [$message['parameters']] : [];
  108. $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
  109. $result[$messageId] = $message;
  110. } else {
  111. if (!empty($message['parameters'])) {
  112. $result[$messageId]['parameters'][] = $message['parameters'];
  113. }
  114. ++$result[$messageId]['count'];
  115. }
  116. unset($messages[$key]);
  117. }
  118. return $result;
  119. }
  120. private function computeCount(array $messages)
  121. {
  122. $count = [
  123. DataCollectorTranslator::MESSAGE_DEFINED => 0,
  124. DataCollectorTranslator::MESSAGE_MISSING => 0,
  125. DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
  126. ];
  127. foreach ($messages as $message) {
  128. ++$count[$message['state']];
  129. }
  130. return $count;
  131. }
  132. private function sanitizeString(string $string, int $length = 80)
  133. {
  134. $string = trim(preg_replace('/\s+/', ' ', $string));
  135. if (false !== $encoding = mb_detect_encoding($string, null, true)) {
  136. if (mb_strlen($string, $encoding) > $length) {
  137. return mb_substr($string, 0, $length - 3, $encoding).'...';
  138. }
  139. } elseif (\strlen($string) > $length) {
  140. return substr($string, 0, $length - 3).'...';
  141. }
  142. return $string;
  143. }
  144. }