mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
refactor: Rework exit codes, skip dots
This commit is contained in:
parent
cd84a6e02c
commit
47a6f8af18
@ -17,6 +17,8 @@ use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\Exceptions\LogicException;
|
||||
use Config\App;
|
||||
use ErrorException;
|
||||
use FilesystemIterator;
|
||||
use Locale;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
@ -87,31 +89,45 @@ class LocalizationSync extends BaseCommand
|
||||
$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
|
||||
@ -120,7 +136,7 @@ class LocalizationSync extends BaseCommand
|
||||
ksort($files);
|
||||
|
||||
foreach ($files as $originalLanguageFile) {
|
||||
if ($this->isIgnoredFile($originalLanguageFile)) {
|
||||
if ($originalLanguageFile->getExtension() !== 'php') {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -138,6 +154,8 @@ class LocalizationSync extends BaseCommand
|
||||
$content = "<?php\n\nreturn " . var_export($targetLanguageKeys, true) . ";\n";
|
||||
file_put_contents($targetLanguageFile, $content);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -179,9 +197,4 @@ class LocalizationSync extends BaseCommand
|
||||
|
||||
return $mergedLanguageKeys;
|
||||
}
|
||||
|
||||
private function isIgnoredFile(SplFileInfo $file): bool
|
||||
{
|
||||
return $file->isDir() || $file->getFilename() === '.' || $file->getFilename() === '..' || $file->getExtension() !== 'php';
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ namespace CodeIgniter\Commands\Translation;
|
||||
|
||||
use CodeIgniter\Exceptions\LogicException;
|
||||
use CodeIgniter\Test\CIUnitTestCase;
|
||||
use CodeIgniter\Test\ReflectionHelper;
|
||||
use CodeIgniter\Test\StreamFilterTrait;
|
||||
use Config\App;
|
||||
use Locale;
|
||||
@ -27,6 +28,7 @@ use PHPUnit\Framework\Attributes\Group;
|
||||
final class LocalizationSyncTest extends CIUnitTestCase
|
||||
{
|
||||
use StreamFilterTrait;
|
||||
use ReflectionHelper;
|
||||
|
||||
private static string $locale;
|
||||
private static string $languageTestPath;
|
||||
@ -205,6 +207,26 @@ final class LocalizationSyncTest extends CIUnitTestCase
|
||||
$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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user