Skip to content

Commit

Permalink
refactor: Rework exit codes, skip dots
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Jan 10, 2025
1 parent cd84a6e commit 47a6f8a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
41 changes: 27 additions & 14 deletions system/Commands/Translation/LocalizationSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use CodeIgniter\CLI\CLI;
use CodeIgniter\Exceptions\LogicException;
use Config\App;
use ErrorException;
use FilesystemIterator;
use Locale;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
Expand Down Expand Up @@ -87,31 +89,45 @@ public function run(array $params)
$this->languagePath = SUPPORTPATH . 'Language';
}

$this->process($optionLocale, $optionTargetLocale);

CLI::write('All operations done!');
if ($this->process($optionLocale, $optionTargetLocale) === EXIT_SUCCESS) {
CLI::write('All operations done!');
}

return EXIT_SUCCESS;
}

private function process(string $originalLocale, string $targetLocale): void
private function process(string $originalLocale, string $targetLocale): int
{
$originalLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $originalLocale;
$targetLocaleDir = $this->languagePath . DIRECTORY_SEPARATOR . $targetLocale;

if (! is_dir($originalLocaleDir)) {
CLI::error(
'Error: The "' . $originalLocaleDir . '" directory was not found.'
'Error: The "' . clean_path($originalLocaleDir) . '" directory was not found.'
);

return EXIT_ERROR;
}

if (! is_dir($targetLocaleDir) && ! mkdir($targetLocaleDir, 0775)) {
// Unifying the error - mkdir() may cause an exception.
try {
if (! is_dir($targetLocaleDir) && ! mkdir($targetLocaleDir, 0775)) {
throw new ErrorException();
}
} catch (ErrorException $e) {
CLI::error(
'Error: The target directory "' . $targetLocaleDir . '" cannot be accessed.'
'Error: The target directory "' . clean_path($targetLocaleDir) . '" cannot be accessed.'
);

return EXIT_ERROR;
}

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($originalLocaleDir));
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$originalLocaleDir,
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS
)
);

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

foreach ($files as $originalLanguageFile) {
if ($this->isIgnoredFile($originalLanguageFile)) {
if ($originalLanguageFile->getExtension() !== 'php') {
continue;
}

Expand All @@ -138,6 +154,8 @@ private function process(string $originalLocale, string $targetLocale): void
$content = "<?php\n\nreturn " . var_export($targetLanguageKeys, true) . ";\n";
file_put_contents($targetLanguageFile, $content);
}

return EXIT_SUCCESS;
}

/**
Expand Down Expand Up @@ -179,9 +197,4 @@ private function mergeLanguageKeys(array $originalLanguageKeys, array $targetLan

return $mergedLanguageKeys;
}

private function isIgnoredFile(SplFileInfo $file): bool
{
return $file->isDir() || $file->getFilename() === '.' || $file->getFilename() === '..' || $file->getExtension() !== 'php';
}
}
22 changes: 22 additions & 0 deletions tests/system/Commands/Translation/LocalizationSyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\Exceptions\LogicException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ReflectionHelper;
use CodeIgniter\Test\StreamFilterTrait;
use Config\App;
use Locale;
Expand All @@ -27,6 +28,7 @@
final class LocalizationSyncTest extends CIUnitTestCase
{
use StreamFilterTrait;
use ReflectionHelper;

private static string $locale;
private static string $languageTestPath;
Expand Down Expand Up @@ -205,6 +207,26 @@ public function testSyncWithIncorrectTargetOption(): void
$this->assertStringContainsString('is not supported', $this->getStreamFilterBuffer());
}

public function testProcessWithInvalidOption(): void
{
$langPath = SUPPORTPATH . 'Language';
$command = new LocalizationSync(service('logger'), service('commands'));
$this->setPrivateProperty($command, 'languagePath', $langPath);
$runner = $this->getPrivateMethodInvoker($command, 'process');

$status = $runner('de', 'jp');

$this->assertSame(EXIT_ERROR, $status);
$this->assertStringContainsString('Error: The "ROOTPATH/tests/_support/Language/de" directory was not found.', $this->getStreamFilterBuffer());

chmod($langPath, 0544);
$status = $runner('en', 'jp');
chmod($langPath, 0775);

$this->assertSame(EXIT_ERROR, $status);
$this->assertStringContainsString('Error: The target directory "ROOTPATH/tests/_support/Language/jp" cannot be accessed.', $this->getStreamFilterBuffer());
}

private function makeLanguageFiles(): void
{
$lang = <<<'TEXT_WRAP'
Expand Down

0 comments on commit 47a6f8a

Please sign in to comment.