vendor/symfony/translation/DataCollector/TranslationDataCollector.php line 45

  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 DataCollectorTranslator $translator;
  25.     public function __construct(DataCollectorTranslator $translator)
  26.     {
  27.         $this->translator $translator;
  28.     }
  29.     public function lateCollect()
  30.     {
  31.         $messages $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
  32.         $this->data += $this->computeCount($messages);
  33.         $this->data['messages'] = $messages;
  34.         $this->data $this->cloneVar($this->data);
  35.     }
  36.     public function collect(Request $requestResponse $response\Throwable $exception null)
  37.     {
  38.         $this->data['locale'] = $this->translator->getLocale();
  39.         $this->data['fallback_locales'] = $this->translator->getFallbackLocales();
  40.     }
  41.     public function reset()
  42.     {
  43.         $this->data = [];
  44.     }
  45.     public function getMessages(): array|Data
  46.     {
  47.         return $this->data['messages'] ?? [];
  48.     }
  49.     public function getCountMissings(): int
  50.     {
  51.         return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0;
  52.     }
  53.     public function getCountFallbacks(): int
  54.     {
  55.         return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0;
  56.     }
  57.     public function getCountDefines(): int
  58.     {
  59.         return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0;
  60.     }
  61.     public function getLocale()
  62.     {
  63.         return !empty($this->data['locale']) ? $this->data['locale'] : null;
  64.     }
  65.     /**
  66.      * @internal
  67.      */
  68.     public function getFallbackLocales()
  69.     {
  70.         return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
  71.     }
  72.     public function getName(): string
  73.     {
  74.         return 'translation';
  75.     }
  76.     private function sanitizeCollectedMessages(array $messages)
  77.     {
  78.         $result = [];
  79.         foreach ($messages as $key => $message) {
  80.             $messageId $message['locale'].$message['domain'].$message['id'];
  81.             if (!isset($result[$messageId])) {
  82.                 $message['count'] = 1;
  83.                 $message['parameters'] = !empty($message['parameters']) ? [$message['parameters']] : [];
  84.                 $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
  85.                 $result[$messageId] = $message;
  86.             } else {
  87.                 if (!empty($message['parameters'])) {
  88.                     $result[$messageId]['parameters'][] = $message['parameters'];
  89.                 }
  90.                 ++$result[$messageId]['count'];
  91.             }
  92.             unset($messages[$key]);
  93.         }
  94.         return $result;
  95.     }
  96.     private function computeCount(array $messages)
  97.     {
  98.         $count = [
  99.             DataCollectorTranslator::MESSAGE_DEFINED => 0,
  100.             DataCollectorTranslator::MESSAGE_MISSING => 0,
  101.             DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
  102.         ];
  103.         foreach ($messages as $message) {
  104.             ++$count[$message['state']];
  105.         }
  106.         return $count;
  107.     }
  108.     private function sanitizeString(string $stringint $length 80)
  109.     {
  110.         $string trim(preg_replace('/\s+/'' '$string));
  111.         if (false !== $encoding mb_detect_encoding($stringnulltrue)) {
  112.             if (mb_strlen($string$encoding) > $length) {
  113.                 return mb_substr($string0$length 3$encoding).'...';
  114.             }
  115.         } elseif (\strlen($string) > $length) {
  116.             return substr($string0$length 3).'...';
  117.         }
  118.         return $string;
  119.     }
  120. }