diff --git a/src/app/Console/Commands/MakeManualSiteNamesJson.php b/src/app/Console/Commands/MakeManualSiteNamesJson.php index f209389..9ff4716 100644 --- a/src/app/Console/Commands/MakeManualSiteNamesJson.php +++ b/src/app/Console/Commands/MakeManualSiteNamesJson.php @@ -2,11 +2,14 @@ namespace App\Console\Commands; +use App\Console\Concerns\ResolvesLocalDiskPaths; use Illuminate\Console\Command; use Illuminate\Support\Facades\Storage; class MakeManualSiteNamesJson extends Command { + use ResolvesLocalDiskPaths; + protected $signature = 'world-heritage:make-manual-names-json {--in=storage/app/private/unesco/manual_input.txt : Input text file} {--out=storage/app/private/unesco/site_names_manual.json : Output JSON file} @@ -22,7 +25,7 @@ public function handle(): int $pretty = (bool) $this->option('pretty'); $strict = (bool) $this->option('strict'); - $inPath = $this->resolvePath($in); + $inPath = $this->resolvePathToFile($in); if (!is_file($inPath)) { $this->error("Input not found: {$inPath}"); return self::FAILURE; @@ -87,14 +90,14 @@ public function handle(): int return self::FAILURE; } - $outDiskPath = $this->toLocalDiskPath($out); + $outDiskPath = $this->normalizeLocalDiskPath($out); if ($outDiskPath === '') { $this->error("Output path is empty."); return self::FAILURE; } Storage::disk('local')->put($outDiskPath, $json); - $this->info("Wrote: storage/app/{$outDiskPath} (count=" . count($rows) . ")"); + $this->info('Wrote: ' . Storage::disk('local')->path($outDiskPath) . ' (count=' . count($rows) . ')'); return self::SUCCESS; } @@ -215,33 +218,4 @@ private function isSiteLine(string $line): bool return preg_match('/[\((]\s*\d{4}.*[\))]\s*$/u', $line) === 1; } - private function resolvePath(string $path): string - { - $path = trim($path); - if ($path === '') { - return $path; - } - if (str_starts_with($path, '/')) { - return $path; - } - if (preg_match('/^[A-Za-z]:\\\\/', $path) === 1) { - return $path; - } - - return base_path($path); - } - - private function toLocalDiskPath(string $path): string - { - $trimPath = trim($path); - if ($trimPath === '') { - return $trimPath; - } - - if (str_starts_with($trimPath, 'storage/app/')) { - return substr($trimPath, strlen('storage/app/')); - } - - return ltrim($trimPath, '/'); - } } diff --git a/src/app/Packages/Domains/WorldHeritage/Tests/MakeManualSiteNamesJsonTest.php b/src/app/Packages/Domains/WorldHeritage/Tests/MakeManualSiteNamesJsonTest.php new file mode 100644 index 0000000..13d1df9 --- /dev/null +++ b/src/app/Packages/Domains/WorldHeritage/Tests/MakeManualSiteNamesJsonTest.php @@ -0,0 +1,45 @@ +put('unesco/manual_input_test.txt', "Japan\nテストサイト (2000)\n"); + } + + public function test_accepts_disk_relative_input_path_like_other_commands(): void + { + Storage::fake('local'); + $this->writeInputFixture(); + + $exitCode = Artisan::call('world-heritage:make-manual-names-json', [ + '--in' => 'unesco/manual_input_test.txt', + '--out' => 'storage/app/private/unesco/manual_output_test.json', + ]); + + $this->assertSame(0, $exitCode); + } + + public function test_output_is_written_without_a_doubled_private_segment(): void + { + Storage::fake('local'); + $this->writeInputFixture(); + + Artisan::call('world-heritage:make-manual-names-json', [ + '--in' => 'storage/app/private/unesco/manual_input_test.txt', + '--out' => 'storage/app/private/unesco/manual_output_test.json', + ]); + + $this->assertTrue(Storage::disk('local')->exists('unesco/manual_output_test.json')); + $this->assertFalse(Storage::disk('local')->exists('private/unesco/manual_output_test.json')); + } +}