Skip to content

Commit 47a6f8a

Browse files
committed
refactor: Rework exit codes, skip dots
1 parent cd84a6e commit 47a6f8a

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

system/Commands/Translation/LocalizationSync.php

+27-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use CodeIgniter\CLI\CLI;
1818
use CodeIgniter\Exceptions\LogicException;
1919
use Config\App;
20+
use ErrorException;
21+
use FilesystemIterator;
2022
use Locale;
2123
use RecursiveDirectoryIterator;
2224
use RecursiveIteratorIterator;
@@ -87,31 +89,45 @@ public function run(array $params)
8789
$this->languagePath = SUPPORTPATH . 'Language';
8890
}
8991

90-
$this->process($optionLocale, $optionTargetLocale);
91-
92-
CLI::write('All operations done!');
92+
if ($this->process($optionLocale, $optionTargetLocale) === EXIT_SUCCESS) {
93+
CLI::write('All operations done!');
94+
}
9395

9496
return EXIT_SUCCESS;
9597
}
9698

97-
private function process(string $originalLocale, string $targetLocale): void
99+
private function process(string $originalLocale, string $targetLocale): int
98100
{
99101
$originalLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $originalLocale;
100102
$targetLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $targetLocale;
101103

102104
if (! is_dir($originalLocaleDir)) {
103105
CLI::error(
104-
'Error: The "' . $originalLocaleDir . '" directory was not found.'
106+
'Error: The "' . clean_path($originalLocaleDir) . '" directory was not found.'
105107
);
108+
109+
return EXIT_ERROR;
106110
}
107111

108-
if (! is_dir($targetLocaleDir) && ! mkdir($targetLocaleDir, 0775)) {
112+
// Unifying the error - mkdir() may cause an exception.
113+
try {
114+
if (! is_dir($targetLocaleDir) && ! mkdir($targetLocaleDir, 0775)) {
115+
throw new ErrorException();
116+
}
117+
} catch (ErrorException $e) {
109118
CLI::error(
110-
'Error: The target directory "' . $targetLocaleDir . '" cannot be accessed.'
119+
'Error: The target directory "' . clean_path($targetLocaleDir) . '" cannot be accessed.'
111120
);
121+
122+
return EXIT_ERROR;
112123
}
113124

114-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($originalLocaleDir));
125+
$iterator = new RecursiveIteratorIterator(
126+
new RecursiveDirectoryIterator(
127+
$originalLocaleDir,
128+
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS
129+
)
130+
);
115131

116132
/**
117133
* @var list<SplFileInfo> $files
@@ -120,7 +136,7 @@ private function process(string $originalLocale, string $targetLocale): void
120136
ksort($files);
121137

122138
foreach ($files as $originalLanguageFile) {
123-
if ($this->isIgnoredFile($originalLanguageFile)) {
139+
if ($originalLanguageFile->getExtension() !== 'php') {
124140
continue;
125141
}
126142

@@ -138,6 +154,8 @@ private function process(string $originalLocale, string $targetLocale): void
138154
$content = "<?php\n\nreturn " . var_export($targetLanguageKeys, true) . ";\n";
139155
file_put_contents($targetLanguageFile, $content);
140156
}
157+
158+
return EXIT_SUCCESS;
141159
}
142160

143161
/**
@@ -179,9 +197,4 @@ private function mergeLanguageKeys(array $originalLanguageKeys, array $targetLan
179197

180198
return $mergedLanguageKeys;
181199
}
182-
183-
private function isIgnoredFile(SplFileInfo $file): bool
184-
{
185-
return $file->isDir() || $file->getFilename() === '.' || $file->getFilename() === '..' || $file->getExtension() !== 'php';
186-
}
187200
}

tests/system/Commands/Translation/LocalizationSyncTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Exceptions\LogicException;
1717
use CodeIgniter\Test\CIUnitTestCase;
18+
use CodeIgniter\Test\ReflectionHelper;
1819
use CodeIgniter\Test\StreamFilterTrait;
1920
use Config\App;
2021
use Locale;
@@ -27,6 +28,7 @@
2728
final class LocalizationSyncTest extends CIUnitTestCase
2829
{
2930
use StreamFilterTrait;
31+
use ReflectionHelper;
3032

3133
private static string $locale;
3234
private static string $languageTestPath;
@@ -205,6 +207,26 @@ public function testSyncWithIncorrectTargetOption(): void
205207
$this->assertStringContainsString('is not supported', $this->getStreamFilterBuffer());
206208
}
207209

210+
public function testProcessWithInvalidOption(): void
211+
{
212+
$langPath = SUPPORTPATH . 'Language';
213+
$command = new LocalizationSync(service('logger'), service('commands'));
214+
$this->setPrivateProperty($command, 'languagePath', $langPath);
215+
$runner = $this->getPrivateMethodInvoker($command, 'process');
216+
217+
$status = $runner('de', 'jp');
218+
219+
$this->assertSame(EXIT_ERROR, $status);
220+
$this->assertStringContainsString('Error: The "ROOTPATH/tests/_support/Language/de" directory was not found.', $this->getStreamFilterBuffer());
221+
222+
chmod($langPath, 0544);
223+
$status = $runner('en', 'jp');
224+
chmod($langPath, 0775);
225+
226+
$this->assertSame(EXIT_ERROR, $status);
227+
$this->assertStringContainsString('Error: The target directory "ROOTPATH/tests/_support/Language/jp" cannot be accessed.', $this->getStreamFilterBuffer());
228+
}
229+
208230
private function makeLanguageFiles(): void
209231
{
210232
$lang = <<<'TEXT_WRAP'

0 commit comments

Comments
 (0)