6b5e165aad9a7bf8a1e771c808adedde7d4f4243
[yaffs-website] / vendor / symfony / translation / DataCollector / TranslationDataCollector.php
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
49     /**
50      * {@inheritdoc}
51      */
52     public function collect(Request $request, Response $response, \Exception $exception = null)
53     {
54     }
55
56     /**
57      * @return array
58      */
59     public function getMessages()
60     {
61         return isset($this->data['messages']) ? $this->data['messages'] : array();
62     }
63
64     /**
65      * @return int
66      */
67     public function getCountMissings()
68     {
69         return isset($this->data[DataCollectorTranslator::MESSAGE_MISSING]) ? $this->data[DataCollectorTranslator::MESSAGE_MISSING] : 0;
70     }
71
72     /**
73      * @return int
74      */
75     public function getCountFallbacks()
76     {
77         return isset($this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK]) ? $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] : 0;
78     }
79
80     /**
81      * @return int
82      */
83     public function getCountDefines()
84     {
85         return isset($this->data[DataCollectorTranslator::MESSAGE_DEFINED]) ? $this->data[DataCollectorTranslator::MESSAGE_DEFINED] : 0;
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     public function getName()
92     {
93         return 'translation';
94     }
95
96     private function sanitizeCollectedMessages($messages)
97     {
98         $result = array();
99         foreach ($messages as $key => $message) {
100             $messageId = $message['locale'].$message['domain'].$message['id'];
101
102             if (!isset($result[$messageId])) {
103                 $message['count'] = 1;
104                 $message['parameters'] = !empty($message['parameters']) ? array($this->cloneVar($message['parameters'])) : array();
105                 $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
106                 $result[$messageId] = $message;
107             } else {
108                 if (!empty($message['parameters'])) {
109                     $result[$messageId]['parameters'][] = $this->cloneVar($message['parameters']);
110                 }
111
112                 ++$result[$messageId]['count'];
113             }
114
115             unset($messages[$key]);
116         }
117
118         return $result;
119     }
120
121     private function computeCount($messages)
122     {
123         $count = array(
124             DataCollectorTranslator::MESSAGE_DEFINED => 0,
125             DataCollectorTranslator::MESSAGE_MISSING => 0,
126             DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
127         );
128
129         foreach ($messages as $message) {
130             ++$count[$message['state']];
131         }
132
133         return $count;
134     }
135
136     private function sanitizeString($string, $length = 80)
137     {
138         $string = trim(preg_replace('/\s+/', ' ', $string));
139
140         if (false !== $encoding = mb_detect_encoding($string, null, true)) {
141             if (mb_strlen($string, $encoding) > $length) {
142                 return mb_substr($string, 0, $length - 3, $encoding).'...';
143             }
144         } elseif (strlen($string) > $length) {
145             return substr($string, 0, $length - 3).'...';
146         }
147
148         return $string;
149     }
150 }