Skip to content

Commit ca3652b

Browse files
committed
fix: Move long code to process()
1 parent 3b97664 commit ca3652b

1 file changed

Lines changed: 90 additions & 70 deletions

File tree

‎system/Commands/Translation/LocalizationFinder.php‎

Lines changed: 90 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,22 @@ class LocalizationFinder extends BaseCommand
3838
*/
3939
private bool $verbose = false;
4040

41+
/**
42+
* Flag for showing only translations, without saving
43+
*/
44+
private bool $showNew = false;
45+
4146
private string $languagePath;
4247

4348
public function run(array $params)
4449
{
4550
$this->verbose = array_key_exists('verbose', $params);
46-
$optionShowNew = array_key_exists('show-new', $params);
51+
$this->showNew = array_key_exists('show-new', $params);
4752
$optionLocale = ! empty($params['locale']) ? $params['locale'] : null;
4853
$optionDir = ! empty($params['dir']) ? $params['dir'] : null;
4954
$currentLocale = Locale::getDefault();
5055
$currentDir = APPPATH;
5156
$this->languagePath = $currentDir . 'Language';
52-
$tableRows = [];
53-
$languageFoundKeys = [];
54-
$countFiles = 0;
55-
$countNewKeys = 0;
5657

5758
if (ENVIRONMENT === 'testing') {
5859
$currentDir = SUPPORTPATH . 'Services/';
@@ -87,72 +88,8 @@ public function run(array $params)
8788
$currentDir = $tempCurrentDir;
8889
}
8990

90-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($currentDir));
91-
$files = iterator_to_array($iterator, true);
92-
ksort($files);
93-
$this->writeIsVerbose('Current locale: ' . $currentLocale);
94-
$this->writeIsVerbose('Find phrases in ' . $currentDir . ' folder...');
95-
96-
/**
97-
* @var SplFileInfo $file
98-
*/
99-
foreach ($files as $file) {
100-
if ($this->isIgnoredFile($file)) {
101-
continue;
102-
}
103-
104-
$this->writeIsVerbose('File found: ' . mb_substr($file->getRealPath(), mb_strlen(APPPATH)));
105-
$countFiles++;
106-
$languageFoundKeys = array_replace_recursive($this->findTranslationsInFile($file), $languageFoundKeys);
107-
}
108-
109-
ksort($languageFoundKeys);
110-
$languageDiff = [];
111-
$languageFoundGroups = array_unique(array_keys($languageFoundKeys));
112-
113-
foreach ($languageFoundGroups as $langFileName) {
114-
$languageStoredKeys = [];
115-
$languageFilePath = $this->languagePath . DIRECTORY_SEPARATOR . $currentLocale . DIRECTORY_SEPARATOR . $langFileName . '.php';
116-
117-
if (is_file($languageFilePath)) {
118-
/**
119-
* Load old localization
120-
*/
121-
$languageStoredKeys = require $languageFilePath;
122-
}
91+
$this->process($currentDir, $currentLocale);
12392

124-
$languageDiff = $this->arrayDiffRecursive($languageFoundKeys[$langFileName], $languageStoredKeys);
125-
$countNewKeys += $this->arrayCountRecursive($languageDiff);
126-
127-
if ($optionShowNew) {
128-
$tableRows = array_merge($this->arrayToTableRows($langFileName, $languageDiff), $tableRows);
129-
} else {
130-
$newLanguageKeys = array_replace_recursive($languageFoundKeys[$langFileName], $languageStoredKeys);
131-
132-
/**
133-
* New translates exists
134-
*/
135-
if ($languageDiff !== []) {
136-
if (false === file_put_contents($languageFilePath, $this->templateFile($newLanguageKeys))) {
137-
$this->writeIsVerbose('Lang file ' . $langFileName . ' (error write).', 'red');
138-
} else {
139-
/**
140-
* FIXME: Need help command (slow run)
141-
*/
142-
exec('composer cs-fix --quiet ' . $this->languagePath);
143-
$this->writeIsVerbose('Lang file "' . $langFileName . '" successful updated!', 'green');
144-
}
145-
}
146-
}
147-
}
148-
149-
if ($optionShowNew && ! empty($tableRows)) {
150-
sort($tableRows);
151-
CLI::table($tableRows, ['File', 'Key']);
152-
}
153-
154-
$this->writeIsVerbose('Files found: ' . $countFiles);
155-
$this->writeIsVerbose('New translates found: ' . $countNewKeys);
15693
CLI::write('All operations done!');
15794

15895
return EXIT_SUCCESS;
@@ -347,4 +284,87 @@ private function isSubDirectory(string $directory, string $rootDirectory): bool
347284
{
348285
return 0 === strncmp($directory, $rootDirectory, strlen($directory));
349286
}
287+
288+
private function process(string $currentDir, string $currentLocale): void
289+
{
290+
$tableRows = [];
291+
$countNewKeys = 0;
292+
293+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($currentDir));
294+
$files = iterator_to_array($iterator, true);
295+
ksort($files);
296+
297+
[$languageFoundKeys, $countFiles] = $this->findLanguageKeysInFiles($files);
298+
ksort($languageFoundKeys);
299+
300+
/**
301+
* New translates
302+
*/
303+
$languageDiff = [];
304+
$languageFoundGroups = array_unique(array_keys($languageFoundKeys));
305+
306+
foreach ($languageFoundGroups as $langFileName) {
307+
$languageStoredKeys = [];
308+
$languageFilePath = $this->languagePath . DIRECTORY_SEPARATOR . $currentLocale . DIRECTORY_SEPARATOR . $langFileName . '.php';
309+
310+
if (is_file($languageFilePath)) {
311+
/**
312+
* Load old localization
313+
*/
314+
$languageStoredKeys = require $languageFilePath;
315+
}
316+
317+
$languageDiff = $this->arrayDiffRecursive($languageFoundKeys[$langFileName], $languageStoredKeys);
318+
$countNewKeys += $this->arrayCountRecursive($languageDiff);
319+
320+
if ($this->showNew) {
321+
$tableRows = array_merge($this->arrayToTableRows($langFileName, $languageDiff), $tableRows);
322+
} else {
323+
$newLanguageKeys = array_replace_recursive($languageFoundKeys[$langFileName], $languageStoredKeys);
324+
325+
/**
326+
* New translates exists
327+
*/
328+
if ($languageDiff !== []) {
329+
if (false === file_put_contents($languageFilePath, $this->templateFile($newLanguageKeys))) {
330+
$this->writeIsVerbose('Lang file ' . $langFileName . ' (error write).', 'red');
331+
} else {
332+
exec('composer cs-fix --quiet ' . $this->languagePath);
333+
$this->writeIsVerbose('Lang file "' . $langFileName . '" successful updated!', 'green');
334+
}
335+
}
336+
}
337+
}
338+
339+
if ($this->showNew && ! empty($tableRows)) {
340+
sort($tableRows);
341+
CLI::table($tableRows, ['File', 'Key']);
342+
}
343+
344+
$this->writeIsVerbose('Files found: ' . $countFiles);
345+
$this->writeIsVerbose('New translates found: ' . $countNewKeys);
346+
}
347+
348+
/**
349+
* @param SplFileInfo[] $files
350+
*
351+
* @return array<int, array|int>
352+
*/
353+
private function findLanguageKeysInFiles(array $files): array
354+
{
355+
$languageFoundKeys = [];
356+
$countFiles = 0;
357+
358+
foreach ($files as $file) {
359+
if ($this->isIgnoredFile($file)) {
360+
continue;
361+
}
362+
363+
$this->writeIsVerbose('File found: ' . mb_substr($file->getRealPath(), mb_strlen(APPPATH)));
364+
$countFiles++;
365+
$languageFoundKeys = array_replace_recursive($this->findTranslationsInFile($file), $languageFoundKeys);
366+
}
367+
368+
return [$languageFoundKeys, $countFiles];
369+
}
350370
}

0 commit comments

Comments
 (0)