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