Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 6 additions & 32 deletions src/app/Console/Commands/MakeManualSiteNamesJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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, '/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Packages\Domains\WorldHeritage\Tests;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class MakeManualSiteNamesJsonTest extends TestCase
{
private function writeInputFixture(): void
{
// Always written at the disk-relative key, regardless of which --in
// string form (disk-relative or storage/app/private/...) a test uses,
// since both forms must normalize to this same key.
Storage::disk('local')->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'));
}
}
Loading