View file vendor/symfony/translation/DataCollector/TranslationDataCollector.php

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