|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
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 ONGR\TranslationsBundle\Service\Export; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\TranslationsBundle\Document\Message; |
|
15
|
|
|
use ONGR\TranslationsBundle\Document\Translation; |
|
16
|
|
|
use ONGR\TranslationsBundle\Service\TranslationManager; |
|
17
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
19
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
|
20
|
|
|
/** |
|
21
|
|
|
* Class Export. |
|
22
|
|
|
*/ |
|
23
|
|
|
class ExportManager |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var TranslationManager |
|
27
|
|
|
*/ |
|
28
|
|
|
private $translationManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var YmlExport |
|
32
|
|
|
*/ |
|
33
|
|
|
private $exporter; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $locales; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Translation[] |
|
42
|
|
|
*/ |
|
43
|
|
|
private $refresh = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var YamlParser |
|
47
|
|
|
*/ |
|
48
|
|
|
private $parser; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param ParameterBag $loadersContainer |
|
|
|
|
|
|
52
|
|
|
* @param TranslationManager $translationManager |
|
53
|
|
|
* @param YmlExport $exporter |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( |
|
56
|
|
|
TranslationManager $translationManager, |
|
57
|
|
|
YmlExport $exporter |
|
58
|
|
|
) { |
|
59
|
|
|
$this->parser = new YamlParser(); |
|
60
|
|
|
$this->translationManager = $translationManager; |
|
61
|
|
|
$this->exporter = $exporter; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getLocales() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->locales; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $locales |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setLocales($locales) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->locales = $locales; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Exports translations from ES to files. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $domains To export. |
|
84
|
|
|
* @param bool $force |
|
85
|
|
|
*/ |
|
86
|
|
|
public function export($domains = [], $force = null) |
|
87
|
|
|
{ |
|
88
|
|
|
foreach ($this->formExportList($domains, $force) as $file => $translations) { |
|
|
|
|
|
|
89
|
|
|
if (!file_exists($file)) { |
|
90
|
|
|
(new Filesystem())->touch($file); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$currentTranslations = $this->parser->parse(file_get_contents($file)) ?? []; |
|
94
|
|
|
|
|
95
|
|
|
$currentTranslations = $this->flatten($currentTranslations); |
|
96
|
|
|
|
|
97
|
|
|
$translations = array_replace_recursive($currentTranslations, $translations); |
|
98
|
|
|
|
|
99
|
|
|
$this->exporter->export($file, $translations); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->translationManager->save($this->refresh); |
|
103
|
|
|
$this->refresh = []; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Flatten multidimensional array and concatenating keys |
|
109
|
|
|
* |
|
110
|
|
|
* @param $array |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
private function flatten($array, $prefix = '') { |
|
|
|
|
|
|
114
|
|
|
$result = []; |
|
115
|
|
|
foreach($array as $key=>$value) { |
|
116
|
|
|
if(is_array($value)) { |
|
117
|
|
|
$result = $result + $this->flatten($value, $prefix . $key . '.'); |
|
118
|
|
|
} |
|
119
|
|
|
else { |
|
120
|
|
|
$result[$prefix . $key] = $value; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
return $result; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get translations for export. |
|
128
|
|
|
* |
|
129
|
|
|
* @param array $domains To read from storage. |
|
130
|
|
|
* @param bool $force Determines if the message status is relevant. |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
private function formExportList($domains, $force) |
|
135
|
|
|
{ |
|
136
|
|
|
$output = []; |
|
137
|
|
|
$filters = array_filter([ |
|
138
|
|
|
'messages.locale' => $this->getLocales(), |
|
139
|
|
|
'domain' => $domains |
|
140
|
|
|
]); |
|
141
|
|
|
|
|
142
|
|
|
$translations = $this->translationManager->getAll($filters); |
|
143
|
|
|
|
|
144
|
|
|
/** @var Translation $translation */ |
|
145
|
|
|
foreach ($translations as $translation) { |
|
146
|
|
|
$messages = $translation->getMessages(); |
|
147
|
|
|
|
|
148
|
|
|
foreach ($messages as $key => $message) { |
|
149
|
|
|
if ($message->getStatus() === Message::DIRTY || $force) { |
|
150
|
|
|
$path = sprintf( |
|
151
|
|
|
'%s' . DIRECTORY_SEPARATOR . '%s.%s.%s', |
|
152
|
|
|
$translation->getPath(), |
|
153
|
|
|
'messages', |
|
154
|
|
|
$message->getLocale(), |
|
155
|
|
|
$translation->getFormat() |
|
156
|
|
|
); |
|
157
|
|
|
$output[$path][$translation->getKey()] = $message->getMessage(); |
|
158
|
|
|
|
|
159
|
|
|
$message->setStatus(Message::FRESH); |
|
160
|
|
|
$this->refresh[] = $translation; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $output; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.