This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
generator.php
372 lines (301 loc) · 11.6 KB
/
generator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* Kodekit Platform - http:https://www.timble.net/kodekit
*
* @copyright Copyright (C) 2011 - 2016 Johan Janssens and Timble CVBA. (http:https://www.timble.net)
* @license MPL v2.0 <https://www.mozilla.org/en-US/MPL/2.0>
* @link https://github.com/timble/kodekit-platform for the canonical source repository
*/
namespace Kodekit\Script;
use Kodekit\Library;
class TranslationsGenerator
{
public $strings = array();
public $errors = array();
public $current_file;
public $format;
protected $_location;
protected $_source;
protected $_language;
public function __construct($config = array())
{
// Only one supported for now ... forcing.
$this->format = 'yaml';
$this->_source = isset($config[1]) ? $config[1] : 'lib';
$this->_language = isset($config[2]) ? $config[2] : 'en-GB';
$this->parser = new \PHPParser_Parser(new \PHPParser_Lexer);
$this->traverser = new \PHPParser_NodeTraverser;
$this->traverser->addVisitor(new TranslationsNodeVisitor($this));
}
public function logNode($node)
{
if ($value = $node->value) {
if (!isset($this->strings[$this->_location][$value])) {
$this->strings[$this->_location][$value] = array();
}
$this->strings[$this->_location][$value][] = array(
'file' => $this->current_file,
'line' => $node->getAttribute('startLine')
);
} elseif ($node instanceof PHPParser_Node_Expr_Array) {
foreach ($node->items as $item) {
$this->logNode($item->value);
}
} elseif ($node instanceof \PHPParser_Node_Expr_Ternary) {
$outcomes = array('if', 'else');
foreach ($outcomes as $outcome)
{
if ($node->{$outcome} instanceof \PHPParser_Node_Scalar)
{
$this->logNode($node->{$outcome});
}
}
} elseif ($node instanceof PHPParser_Node_Expr_Variable) {
$this->errors[] = array(
'file' => $this->current_file,
'line' => $node->getAttribute('startLine'),
'name' => '$'.$node->name,
'type' => get_class($node)
);
} else {
$this->errors[] = array(
'file' => $this->current_file,
'line' => $node->getAttribute('startLine'),
'type' => get_class($node)
);
}
}
protected function _parse($directory)
{
if (file_exists($directory))
{
foreach (new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directory),
\RecursiveIteratorIterator::LEAVES_ONLY)
as $file)
{
if ($file->getExtension() !== 'php')
{
continue;
}
try
{
$code = file_get_contents($file);
$this->current_file = $file->getRealPath();
$stmts = $this->parser->parse($code);
/*$nodeDumper = new PHPParser_NodeDumper;
echo $nodeDumper->dump($stmts);*/
$stmts = $this->traverser->traverse($stmts);
} catch (PHPParser_Error $e)
{
echo 'Parse Error: ', $e->getMessage();
}
}
}
}
protected function _loadLanguageFiles($source)
{
$files = array();
$strategy = $this->getStrategy();
foreach ($this->_getLocations($source) as $location => $directory)
{
if ($file = $this->_getLanguageFile($directory))
{
$files[$location] = $strategy->parse($file);
}
}
return $files;
}
protected function _getBaseTranslations($source)
{
$translations = array();
$parts = explode(':', $source);
if ($parts[0] == 'com')
{
// Load Framework translations.
$library = $this->_loadLanguageFiles('lib');
if ($parts[1] != 'application')
{
// Load Application translations.
$application = $this->_loadLanguageFiles('com:application');
// Calculate base by merging library and application translations.
$translations['component'] = array_merge($library['library'], $application['component']);
$translations['admin'] = array_merge($library['library'], $application['component'],
$application['admin']);
$translations['site'] = array_merge($library['library'], $application['component'],
$application['site']);
}
else
{
// Use library translations as base.
$translations['component'] = $library['library'];
$translations['admin'] = $library['library'];
$translations['site'] = $library['library'];
}
}
return $translations;
}
protected function _isComponent($source)
{
$parts = explode(':', $source);
return $parts[0] == 'com';
}
public function getTranslations()
{
$strategy = $this->getStrategy();
$source = $this->_source;
foreach ($this->_getLocations($source) as $location => $directory)
{
$this->_setLocation($location);
$this->_parse($directory);
}
$files = $this->_loadLanguageFiles($source);
$translations = array();
foreach ($this->strings as $location => $strings)
{
$translations[$location] = array();
foreach ($strings as $key => $occurrences)
{
$value = $key;
// Use current value instead of key if any.
if (isset($files[$location]) && isset($files[$location][$key])) {
$value = $files[$location][$key];
}
$translations[$location][$key] = array('value' => $value, 'occurrences' => $occurrences);;
}
}
$output = '';
if (count($translations))
{
$output .= "\nTranslations for *** {$source} ***:\n";
}
else
{
$output .= "\nNo translations where found for *** {$source} *** ...\n\n";
}
$base = $this->_getBaseTranslations($source);
foreach ($translations as $location => $data)
{
$overrides = array();
$ignored = array();
// Do not list translations that are already included on base translations (application component
// and/or framework).
if ($this->_isComponent($source))
{
foreach (array_keys(array_intersect_key($data, $base[$location])) as $key)
{
if (isset($files[$location][$key]))
{
// Report the override.
$overrides[$key] = $data[$key];
}
else
{
// Remove it from translations list and notify.
$ignored[$key] = $data[$key];
unset($data[$key]);
}
}
if ($location != 'component')
{
// Do not list translations that are already included in the component's layer file unless they
// are overrides.
foreach (array_keys(array_intersect_key($data, $translations['component'])) as $key)
{
if (isset($files[$location][$key]))
{
// Report the override.
$overrides[$key] = $data[$key];
}
else
{
// Remove it from translations list and notify.
unset($data[$key]);
}
}
}
}
$output .= "\n\n### Location: {$location} ... ###\n\n";
$output .= $strategy->dump($data);
$output .= "\n";
$unreferenced = array_diff_key($files[$location], $translations[$location]);
// Report non-referenced (not found) translations.
if (!empty($unreferenced))
{
$output .= "!!! Non-referenced translations (present on current location file, but not referenced/present on location): !!!\n\n";
$output .= $strategy->dump($unreferenced);
$output .= "\n";
}
if (!empty($ignored))
{
$output .= "!!! Ignored translations (already present on a base translations file): !!!\n\n";
$output .= $strategy->dump($ignored);
$output .= "\n";
}
if (!empty($overrides)) {
$output .= "!!! Overridden translations (present on any base translations file but overridden on current location): !!!\n\n";
$output .= $strategy->dump($overrides);
$output .= "\n";
}
}
return $output;
}
protected function _setLocation($location)
{
$this->_location = $location;
// Initialize the strings array for current location if not done already.
if (isset($this->strings[$location])) $this->strings[$location] = array();
}
protected function _getLocations($source) {
$parts = explode(':', $source);
switch ($parts[0])
{
case 'lib':
$locations = array('library' => KODEKIT_PATH . '/library');
break;
case 'com':
$component = $parts[1];
$locations = array(
'site' => KODEKIT_PATH . "/application/site/component/{$component}",
"admin" => KODEKIT_PATH . "/application/admin/component/{$component}",
'component' => KODEKIT_PATH . "/component/{$component}");
break;
default:
$locations = array();
break;
}
return $locations;
}
protected function _getLanguageFile($directory)
{
$strategy = $this->getStrategy();
$file = $directory . "/resources/language/{$this->_language}." . $strategy->getFileExtension();
if (!file_exists($file)) $file = null;
return $file;
}
public function getStrategy()
{
$classname = 'Kodekit\Script\TranslationsStrategy' . ucfirst($this->format);
return new $classname();
}
public function getErrorMessage()
{
if (!count($this->errors)) {
return '';
}
$output = "\n\nThe following calls with non-string arguments were also found:\n";
$output .= "--------------------------------------------------------------\n";
foreach ($this->errors as $error) {
$output .= 'File: '.$error['file']."\n";
$output .= 'Line: '.$error['line']."\n";
if (!empty($error['name'])) {
$output .= "Type: Variable \n";
$output .= 'Variable name: '.$error['name']."\n";
} else {
$output .= 'Type: '.$error['type']."\n";
}
$output .= "--------------------------------------------------------------\n";
}
return $output;
}
}