blob: f8480adba4e136f5dbc9073df49d38a0a9dff915 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\DataCollector;
13
14use Symfony\Component\HttpFoundation\Request;
15use Symfony\Component\HttpFoundation\Response;
16use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
18use Symfony\Component\Translation\DataCollectorTranslator;
19use Symfony\Component\VarDumper\Cloner\Data;
20
21/**
22 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
23 *
24 * @final
25 */
26class 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 /**
66 * @return array|Data
67 */
68 public function getMessages()
69 {
70 return $this->data['messages'] ?? [];
71 }
72
73 /**
74 * @return int
75 */
76 public function getCountMissings()
77 {
78 return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0;
79 }
80
81 /**
82 * @return int
83 */
84 public function getCountFallbacks()
85 {
86 return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0;
87 }
88
89 /**
90 * @return int
91 */
92 public function getCountDefines()
93 {
94 return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0;
95 }
96
97 public function getLocale()
98 {
99 return !empty($this->data['locale']) ? $this->data['locale'] : null;
100 }
101
102 /**
103 * @internal
104 */
105 public function getFallbackLocales()
106 {
107 return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
108 }
109
110 /**
111 * {@inheritdoc}
112 */
113 public function getName()
114 {
115 return 'translation';
116 }
117
118 private function sanitizeCollectedMessages(array $messages)
119 {
120 $result = [];
121 foreach ($messages as $key => $message) {
122 $messageId = $message['locale'].$message['domain'].$message['id'];
123
124 if (!isset($result[$messageId])) {
125 $message['count'] = 1;
126 $message['parameters'] = !empty($message['parameters']) ? [$message['parameters']] : [];
127 $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
128 $result[$messageId] = $message;
129 } else {
130 if (!empty($message['parameters'])) {
131 $result[$messageId]['parameters'][] = $message['parameters'];
132 }
133
134 ++$result[$messageId]['count'];
135 }
136
137 unset($messages[$key]);
138 }
139
140 return $result;
141 }
142
143 private function computeCount(array $messages)
144 {
145 $count = [
146 DataCollectorTranslator::MESSAGE_DEFINED => 0,
147 DataCollectorTranslator::MESSAGE_MISSING => 0,
148 DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
149 ];
150
151 foreach ($messages as $message) {
152 ++$count[$message['state']];
153 }
154
155 return $count;
156 }
157
158 private function sanitizeString(string $string, int $length = 80)
159 {
160 $string = trim(preg_replace('/\s+/', ' ', $string));
161
162 if (false !== $encoding = mb_detect_encoding($string, null, true)) {
163 if (mb_strlen($string, $encoding) > $length) {
164 return mb_substr($string, 0, $length - 3, $encoding).'...';
165 }
166 } elseif (\strlen($string) > $length) {
167 return substr($string, 0, $length - 3).'...';
168 }
169
170 return $string;
171 }
172}