Drupal investigation

TranslationDataCollector.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /**
  17. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  18. */
  19. class TranslationDataCollector extends DataCollector implements LateDataCollectorInterface
  20. {
  21. /**
  22. * @var DataCollectorTranslator
  23. */
  24. private $translator;
  25. /**
  26. * @param DataCollectorTranslator $translator
  27. */
  28. public function __construct(DataCollectorTranslator $translator)
  29. {
  30. $this->translator = $translator;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function lateCollect()
  36. {
  37. $messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
  38. $this->data = $this->computeCount($messages);
  39. $this->data['messages'] = $messages;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function collect(Request $request, Response $response, \Exception $exception = null)
  45. {
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function getMessages()
  51. {
  52. return isset($this->data['messages']) ? $this->data['messages'] : array();
  53. }
  54. /**
  55. * @return int
  56. */
  57. public function getCountMissings()
  58. {
  59. return isset($this->data[DataCollectorTranslator::MESSAGE_MISSING]) ? $this->data[DataCollectorTranslator::MESSAGE_MISSING] : 0;
  60. }
  61. /**
  62. * @return int
  63. */
  64. public function getCountFallbacks()
  65. {
  66. return isset($this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK]) ? $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] : 0;
  67. }
  68. /**
  69. * @return int
  70. */
  71. public function getCountDefines()
  72. {
  73. return isset($this->data[DataCollectorTranslator::MESSAGE_DEFINED]) ? $this->data[DataCollectorTranslator::MESSAGE_DEFINED] : 0;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getName()
  79. {
  80. return 'translation';
  81. }
  82. private function sanitizeCollectedMessages($messages)
  83. {
  84. $result = array();
  85. foreach ($messages as $key => $message) {
  86. $messageId = $message['locale'].$message['domain'].$message['id'];
  87. if (!isset($result[$messageId])) {
  88. $message['count'] = 1;
  89. $message['parameters'] = !empty($message['parameters']) ? array($message['parameters']) : array();
  90. $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
  91. $result[$messageId] = $message;
  92. } else {
  93. if (!empty($message['parameters'])) {
  94. $result[$messageId]['parameters'][] = $message['parameters'];
  95. }
  96. ++$result[$messageId]['count'];
  97. }
  98. unset($messages[$key]);
  99. }
  100. return $result;
  101. }
  102. private function computeCount($messages)
  103. {
  104. $count = array(
  105. DataCollectorTranslator::MESSAGE_DEFINED => 0,
  106. DataCollectorTranslator::MESSAGE_MISSING => 0,
  107. DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
  108. );
  109. foreach ($messages as $message) {
  110. ++$count[$message['state']];
  111. }
  112. return $count;
  113. }
  114. private function sanitizeString($string, $length = 80)
  115. {
  116. $string = trim(preg_replace('/\s+/', ' ', $string));
  117. if (false !== $encoding = mb_detect_encoding($string, null, true)) {
  118. if (mb_strlen($string, $encoding) > $length) {
  119. return mb_substr($string, 0, $length - 3, $encoding).'...';
  120. }
  121. } elseif (strlen($string) > $length) {
  122. return substr($string, 0, $length - 3).'...';
  123. }
  124. return $string;
  125. }
  126. }