|
1
|
|
|
<?php |
|
2
|
|
|
namespace GettextParser; |
|
3
|
|
|
|
|
4
|
|
|
use GettextParser\Adapter\AbstractAdapter; |
|
5
|
|
|
|
|
6
|
|
|
class Parser |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var AbstractAdapter |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $adapter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* base path |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $basePath; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* log file path |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $logPath; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* result destination path |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $resultPath; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $phrasesList = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $filesList = array(); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $xgettextDir; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $xgettextBin; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $writeBuffer; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $adapterName |
|
61
|
|
|
* @param string $xgettextBin |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \Exception |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct($adapterName, $xgettextBin) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->basePath = realpath(__DIR__ . '/..'); |
|
68
|
|
|
|
|
69
|
|
|
//init files |
|
70
|
|
|
$this->logPath = $this->basePath . '/po_parser.log'; |
|
71
|
|
|
$this->resultPath = sys_get_temp_dir() . '/poedit_' . $adapterName . '_' . md5(microtime()) . '.php'; |
|
72
|
|
|
|
|
73
|
|
|
$this->setXgettextBin($xgettextBin); |
|
74
|
|
|
|
|
75
|
|
|
$this->loadAdapter($adapterName); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $xgettextBin |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Exception |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function setXgettextBin($xgettextBin) |
|
84
|
|
|
{ |
|
85
|
|
|
if (!is_file($xgettextBin)) { |
|
86
|
|
|
throw new \Exception('Invalid xgettext binary path supplied'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->xgettextBin = $xgettextBin; |
|
90
|
|
|
$this->xgettextDir = dirname($this->xgettextBin); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* loads adapter |
|
95
|
|
|
* |
|
96
|
|
|
* @param $adapterName |
|
97
|
|
|
* |
|
98
|
|
|
* @throws \Exception |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function loadAdapter($adapterName) |
|
103
|
|
|
{ |
|
104
|
|
|
if (!is_string($adapterName) || $adapterName === '') { |
|
105
|
|
|
throw new \Exception('Invalid adapterName supplied'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$targetClassName = '\\GettextParser\\Adapter\\' . $adapterName; |
|
109
|
|
|
|
|
110
|
|
|
if (class_exists($targetClassName)) { |
|
111
|
|
|
$this->adapter = new $targetClassName; |
|
112
|
|
|
} else { |
|
113
|
|
|
throw new \Exception("Cannot load Adapter \"{$adapterName}\""); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return AbstractAdapter |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getAdapter() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->adapter; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* performs files parsing and generates output for poEdit parser |
|
127
|
|
|
* |
|
128
|
|
|
* @param $params |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public function run($params) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->log(implode(' ', $params)); |
|
135
|
|
|
|
|
136
|
|
|
$this->processParams($params); |
|
137
|
|
|
$this->parse(); |
|
138
|
|
|
|
|
139
|
|
|
if (!count($this->phrasesList)) { |
|
140
|
|
|
$this->log('Nothing found!'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ($this->writeOutput()) { |
|
144
|
|
|
$this->executePoEditParser($params); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* processes params and sets some variables |
|
150
|
|
|
* |
|
151
|
|
|
* @param $params |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function processParams($params) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->filesList = array(); |
|
158
|
|
|
|
|
159
|
|
|
$paramsCount = count($params); |
|
160
|
|
|
for ($k = 7; $k < $paramsCount; $k++) { |
|
161
|
|
|
$this->filesList[] = $params[$k]; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function parse() |
|
169
|
|
|
{ |
|
170
|
|
|
$this->phrasesList = array(); |
|
171
|
|
|
|
|
172
|
|
|
foreach ($this->filesList as $filePath) { |
|
173
|
|
|
if (is_readable($filePath)) { |
|
174
|
|
|
$phrases = $this->getAdapter()->parse(file_get_contents($filePath)); |
|
175
|
|
|
if (is_array($phrases)) { |
|
176
|
|
|
$this->phrasesList = array_merge($this->phrasesList, $phrases); |
|
177
|
|
|
} |
|
178
|
|
|
} else { |
|
179
|
|
|
$this->log("Cannot read file {$filePath}" . PHP_EOL); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* returns true on success |
|
186
|
|
|
* |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function writeOutput() |
|
190
|
|
|
{ |
|
191
|
|
|
$this->writeHeader(); |
|
192
|
|
|
|
|
193
|
|
|
foreach ($this->phrasesList as $phrase) { |
|
194
|
|
|
$this->writePhrase($phrase); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return (bool) file_put_contents($this->resultPath, $this->writeBuffer, FILE_BINARY); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function writeHeader() |
|
204
|
|
|
{ |
|
205
|
|
|
$this->writeBuffer = "<?php" . PHP_EOL . "/*" . PHP_EOL; |
|
206
|
|
|
$this->writeBuffer .= implode(PHP_EOL, $this->filesList); |
|
207
|
|
|
$this->writeBuffer .= PHP_EOL . "*/"; |
|
208
|
|
|
$this->writeBuffer .= str_repeat(PHP_EOL, 2); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param $phrase |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function writePhrase($phrase) |
|
215
|
|
|
{ |
|
216
|
|
|
if (is_array($phrase)) { |
|
217
|
|
|
//plural |
|
218
|
|
|
$this->writeBuffer .= 'ngettext('; |
|
219
|
|
|
foreach ($phrase as $idx => $item) { |
|
220
|
|
|
if ($idx > 0) { |
|
221
|
|
|
$this->writeBuffer .= ', '; |
|
222
|
|
|
} |
|
223
|
|
|
$this->writeBuffer .= '"' . $item . '"'; |
|
224
|
|
|
} |
|
225
|
|
|
$this->writeBuffer .= ', 3);' . PHP_EOL; |
|
226
|
|
|
} else { |
|
227
|
|
|
//single |
|
228
|
|
|
$this->writeBuffer .= '_("' . $phrase . '");' . PHP_EOL; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param $params |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function executePoEditParser($params) |
|
236
|
|
|
{ |
|
237
|
|
|
chdir($this->xgettextDir); |
|
238
|
|
|
|
|
239
|
|
|
$cmd = $this->xgettextBin . ' --force-po -o "' . $params[2] . '" ' . $params[3] . ' ' . $params[4] . ' "' |
|
240
|
|
|
. $this->resultPath . '"'; |
|
241
|
|
|
|
|
242
|
|
|
$this->log($cmd); |
|
243
|
|
|
|
|
244
|
|
|
exec($cmd); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* writes messages to log |
|
249
|
|
|
* |
|
250
|
|
|
* @param $message |
|
251
|
|
|
* |
|
252
|
|
|
* @return void |
|
253
|
|
|
*/ |
|
254
|
|
|
protected function log($message) |
|
255
|
|
|
{ |
|
256
|
|
|
$f = fopen($this->logPath, 'a'); |
|
257
|
|
|
fwrite($f, $message . PHP_EOL); |
|
258
|
|
|
fclose($f); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @return string |
|
263
|
|
|
*/ |
|
264
|
|
|
public function getResultPath() |
|
265
|
|
|
{ |
|
266
|
|
|
return $this->resultPath; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|