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

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