diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 299173b730e..5d8264bee82 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -145,17 +145,9 @@ protected function captureTextFieldInput(PublicationFieldType $field): string protected function captureArrayFieldInput(PublicationFieldType $field): array { - $lines = []; $this->output->writeln($field->name.' (end with an empty line)'); - do { - $line = Str::replace("\n", '', fgets(STDIN)); - if ($line === '') { - break; - } - $lines[] = trim($line); - } while (true); - return $lines; + return InputStreamHandler::call(); } protected function captureImageFieldInput(PublicationFieldType $field, PublicationType $pubType): string diff --git a/packages/framework/src/Framework/Actions/CreatesNewPublicationPage.php b/packages/framework/src/Framework/Actions/CreatesNewPublicationPage.php index cc508c8db62..dbefc39cf0a 100644 --- a/packages/framework/src/Framework/Actions/CreatesNewPublicationPage.php +++ b/packages/framework/src/Framework/Actions/CreatesNewPublicationPage.php @@ -13,6 +13,7 @@ use Illuminate\Support\Str; use Rgasch\Collection\Collection; use RuntimeException; +use function str_starts_with; /** * Scaffold a publication file. @@ -29,7 +30,9 @@ public function __construct( protected ?OutputStyle $output = null, ) { $canonicalFieldName = $this->pubType->canonicalField; - $canonicalFieldDefinition = $this->pubType->getFields()->filter(fn (PublicationFieldType $field): bool => $field->name === $canonicalFieldName)->first() ?? throw new RuntimeException("Could not find field definition for '$canonicalFieldName' which is required for this type as it's the canonical field"); + $canonicalFieldDefinition = $this->pubType->getFields()->filter(fn (PublicationFieldType $field): bool => $field->name === $canonicalFieldName)->first() ?? $this->handleMissingCanonicalField( + $canonicalFieldName + ); $canonicalValue = $canonicalFieldDefinition->type !== 'array' ? $this->fieldData->{$canonicalFieldName} : $this->fieldData->{$canonicalFieldName}[0]; $canonicalStr = Str::of($canonicalValue)->substr(0, 64); @@ -72,4 +75,15 @@ protected function handleCreate(): void $this->save($output); } + + protected function handleMissingCanonicalField(string $canonicalFieldName): PublicationFieldType + { + if (str_starts_with($canonicalFieldName, '__')) { + return new PublicationFieldType('text', $canonicalFieldName, '0', '0'); + } + + return throw new RuntimeException( + "Could not find field value for '$canonicalFieldName' which is required for this type as it's the canonical field" + ); + } } diff --git a/packages/framework/tests/Feature/Actions/CreatesNewPublicationPageTest.php b/packages/framework/tests/Feature/Actions/CreatesNewPublicationPageTest.php index cd357811858..1da75a8b27a 100644 --- a/packages/framework/tests/Feature/Actions/CreatesNewPublicationPageTest.php +++ b/packages/framework/tests/Feature/Actions/CreatesNewPublicationPageTest.php @@ -53,19 +53,19 @@ public function testCreate() ', file_get_contents(Hyde::path('test-publication/hello-world.md'))); } - protected function makePublicationType(): PublicationType + protected function makePublicationType(array $fields = [ + [ + 'type' => 'string', + 'name' => 'title', + 'min' => 0, + 'max' => 128, + ], + ]): PublicationType { return new PublicationType( 'test', 'title', - fields: [ - [ - 'type' => 'string', - 'name' => 'title', - 'min' => 0, - 'max' => 128, - ], - ], + fields: $fields, directory: 'test-publication', ); } diff --git a/packages/framework/tests/Feature/Commands/MakePublicationCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationCommandTest.php index 3aa87cbfa53..bc650dae56a 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationCommandTest.php @@ -136,6 +136,7 @@ public function test_command_with_invalid_publication_type_passed_as_argument() ->assertExitCode(1); } + // text public function test_command_with_text_input() { InputStreamHandler::mockInput("Hello\nWorld"); @@ -156,6 +157,27 @@ public function test_command_with_text_input() $this->assertStringContainsString("Hello\nWorld", file_get_contents(Hyde::path('test-publication/hello-world.md'))); } + // array + public function test_command_with_array_input() + { + InputStreamHandler::mockInput("Foo\nBar"); + $this->makeSchemaFile([ + 'fields' => [[ + 'type' => 'array', + 'name' => 'tags', + 'min' => '0', + 'max' => '0', + ], + ], + ]); + + $this->artisan('make:publication test-publication') + ->assertExitCode(0); + + $this->assertTrue(File::exists(Hyde::path('test-publication/hello-world.md'))); + $this->assertStringContainsString("Foo\nBar", file_get_contents(Hyde::path('test-publication/hello-world.md'))); + } + protected function makeSchemaFile(array $merge = []): void { file_put_contents(