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
|
|
|
$translations = array_merge_recursive($currentTranslations, $translations); |
96
|
|
|
|
97
|
|
|
$this->exporter->export($file, $translations); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->translationManager->save($this->refresh); |
101
|
|
|
$this->refresh = []; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get translations for export. |
106
|
|
|
* |
107
|
|
|
* @param array $domains To read from storage. |
108
|
|
|
* @param bool $force Determines if the message status is relevant. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
private function formExportList($domains, $force) |
113
|
|
|
{ |
114
|
|
|
$output = []; |
115
|
|
|
$filters = array_filter([ |
116
|
|
|
'messages.locale' => $this->getLocales(), |
117
|
|
|
'domain' => $domains |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
$translations = $this->translationManager->getAll($filters); |
121
|
|
|
|
122
|
|
|
/** @var Translation $translation */ |
123
|
|
|
foreach ($translations as $translation) { |
124
|
|
|
$messages = $translation->getMessages(); |
125
|
|
|
|
126
|
|
|
foreach ($messages as $key => $message) { |
127
|
|
|
if ($message->getStatus() === Message::DIRTY || $force) { |
128
|
|
|
$path = sprintf( |
129
|
|
|
'%s' . DIRECTORY_SEPARATOR . '%s.%s.%s', |
130
|
|
|
$translation->getPath(), |
131
|
|
|
'messages', |
132
|
|
|
$message->getLocale(), |
133
|
|
|
$translation->getFormat() |
134
|
|
|
); |
135
|
|
|
$output[$path][$translation->getKey()] = $message->getMessage(); |
136
|
|
|
|
137
|
|
|
$message->setStatus(Message::FRESH); |
138
|
|
|
$this->refresh[] = $translation; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $output; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.