diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 1115b3d92cc..f3d2115aa85 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -33,14 +33,16 @@ class MakePublicationCommand extends ValidatingCommand /** @var string */ protected $description = 'Create a new publication item'; + protected PublicationType $pubType; + public function safeHandle(): int { $this->title('Creating a new Publication!'); - $pubType = $this->getPubTypeSelection($this->getPublicationTypes()); - $fieldData = $this->collectFieldData($pubType); + $this->pubType = $this->getPubTypeSelection($this->getPublicationTypes()); + $fieldData = $this->collectFieldData($this->pubType); - $creator = new CreatesNewPublicationPage($pubType, $fieldData, $this->hasForceOption(), $this->output); + $creator = new CreatesNewPublicationPage($this->pubType, $fieldData, $this->hasForceOption(), $this->output); if ($creator->hasFileConflict()) { $this->error('Error: A publication already exists with the same canonical field value'); if ($this->confirm('Do you wish to overwrite the existing file?')) { @@ -172,7 +174,7 @@ protected function captureTagFieldInput(PublicationField $field) $this->output->writeln($field->name.' (enter 0 to reload tag definitions)'); do { $offset = 0; - $tagsForGroup = PublicationService::getAllTags()->{$field->tagGroup}; + $tagsForGroup = PublicationService::getAllTags()->{$this->pubType->name}; foreach ($tagsForGroup as $index => $value) { $offset = $index + 1; $this->output->writeln(" $offset: $value"); diff --git a/packages/framework/src/Console/Commands/ValidatePublicationsCommand.php b/packages/framework/src/Console/Commands/ValidatePublicationsCommand.php index ec41566102c..7e35cd20e70 100644 --- a/packages/framework/src/Console/Commands/ValidatePublicationsCommand.php +++ b/packages/framework/src/Console/Commands/ValidatePublicationsCommand.php @@ -68,7 +68,7 @@ public function safeHandle(): int foreach ($publication->type->fields as $field) { $countFields++; $fieldName = $field['name']; - $pubTypeField = new PublicationField($field['type'], $fieldName, $field['tagGroup'] ?? null); + $pubTypeField = new PublicationField($field['type'], $fieldName); try { if ($verbose) { diff --git a/packages/framework/src/Framework/Actions/SeedsPublicationFiles.php b/packages/framework/src/Framework/Actions/SeedsPublicationFiles.php index 66fc03b96fb..d4db6552f73 100644 --- a/packages/framework/src/Framework/Actions/SeedsPublicationFiles.php +++ b/packages/framework/src/Framework/Actions/SeedsPublicationFiles.php @@ -98,7 +98,7 @@ protected function generateFieldData(PublicationField $field): string|int|float| 'image' => 'https://picsum.photos/id/'.rand(1, 1000).'/400/400', 'integer' => rand(-100000, 100000), 'string' => substr($this->fakeSentence(10), 0, rand(0, 255)), - 'tag' => $this->getTags($field), + 'tag' => $this->getTags(), 'text' => $this->getTextValue(rand(3, 20)), 'url' => $this->fakeUrl(), }; @@ -128,9 +128,9 @@ protected function getArrayItems(): array return $arrayItems; } - protected function getTags(PublicationField $field): string + protected function getTags(): string { - $tags = PublicationService::getValuesForTagName($field->tagGroup, false); + $tags = PublicationService::getValuesForTagName($this->pubType->getIdentifier()); return $tags->isEmpty() ? '' : $tags->random(); } diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index cdd191bde3d..8e9c20ca5c7 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -27,7 +27,6 @@ class PublicationField implements SerializableContract public readonly PublicationFieldTypes $type; public readonly string $name; - public readonly ?string $tagGroup; public readonly array $rules; public static function fromArray(array $array): static @@ -35,11 +34,10 @@ public static function fromArray(array $array): static return new static(...$array); } - public function __construct(PublicationFieldTypes|string $type, string $name, ?string $tagGroup = null, array $rules = []) + public function __construct(PublicationFieldTypes|string $type, string $name, array $rules = []) { $this->type = $type instanceof PublicationFieldTypes ? $type : PublicationFieldTypes::from(strtolower($type)); $this->name = Str::kebab($name); - $this->tagGroup = $tagGroup; $this->rules = $rules; } @@ -48,7 +46,6 @@ public function toArray(): array return array_filter([ 'type' => $this->type->value, 'name' => $this->name, - 'tagGroup' => $this->tagGroup, 'rules' => $this->rules, ]); } @@ -75,7 +72,7 @@ public function getValidationRules(?PublicationType $publicationType = null): Co $fieldRules->add("in:$valueList"); break; case 'tag': - $tagValues = PublicationService::getValuesForTagName($this->tagGroup) ?? collect([]); + $tagValues = PublicationService::getValuesForTagName($publicationType?->getIdentifier() ?? '') ?? collect([]); $valueList = $tagValues->implode(','); $fieldRules->add("in:$valueList"); break; diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index ca66bcadc01..bea5dac9341 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Features\Publications; +use function file_exists; use Hyde\Framework\Actions\SourceFileParser; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Hyde; @@ -57,46 +58,18 @@ public static function getMediaForPubType(PublicationType $pubType): Collection /** * Get all available tags. - * - * @param bool $reload Reload the tags from the filesystem - * @return \Rgasch\Collection\Collection - * - * @throws \Safe\Exceptions\FilesystemException - * @throws \Safe\Exceptions\JsonException */ - public static function getAllTags(bool $reload = true): Collection + public static function getAllTags(): Collection { - $filename = Hyde::path('tags.json'); - if (! file_exists($filename)) { - return Collection::create(); - } - - static $tags = null; - if (! $tags || $reload) { - $tags = Collection::create(json_decode(file_get_contents($filename), true))->sortKeys(); - } - - return $tags; + return Collection::create(self::parseTagsFile())->sortKeys(); } /** * Get all values for a given tag name. - * - * @param string $tagName - * @param bool $reload Reload the tags from the filesystem - * @return \Rgasch\Collection\Collection - * - * @throws \Safe\Exceptions\FilesystemException - * @throws \Safe\Exceptions\JsonException */ - public static function getValuesForTagName(string $tagName, bool $reload = true): Collection + public static function getValuesForTagName(string $tagName): Collection { - $tags = self::getAllTags($reload); - if (! $tags->get($tagName)) { - return Collection::create(); - } - - return $tags->$tagName; + return self::getAllTags()->get($tagName) ?? Collection::create(); } /** @@ -131,4 +104,13 @@ protected static function getMediaFiles(string $directory, string $extensions = { return glob(Hyde::path("_media/$directory/*.$extensions"), GLOB_BRACE); } + + protected static function parseTagsFile(): array + { + if (file_exists(Hyde::path('tags.json'))) { + return json_decode(file_get_contents(Hyde::path('tags.json')), true); + } + + return []; + } } diff --git a/packages/framework/tests/Feature/Actions/SeedsPublicationFilesTest.php b/packages/framework/tests/Feature/Actions/SeedsPublicationFilesTest.php index 9707d7e1a43..60b345ec2fb 100644 --- a/packages/framework/tests/Feature/Actions/SeedsPublicationFilesTest.php +++ b/packages/framework/tests/Feature/Actions/SeedsPublicationFilesTest.php @@ -131,9 +131,9 @@ public function testWithStringType() public function testWithTagType() { - $tags = ['foo' => ['foo', 'bar', 'baz']]; + $tags = ['test-publication' => ['foo', 'bar', 'baz']]; $this->file('tags.json', json_encode($tags)); - $this->updateSchema('tag', 'tag', tagGroup: 'foo'); + $this->updateSchema('tag', 'tag'); (new SeedsPublicationFiles($this->pubType))->create(); $publication = $this->firstPublication(); @@ -141,7 +141,7 @@ public function testWithTagType() $this->assertBaseline($publication); $this->assertNotEmpty($publication->matter('tag')); $this->assertIsString($publication->matter('tag')); - $this->assertTrue(in_array($publication->matter('tag'), $tags['foo'])); + $this->assertTrue(in_array($publication->matter('tag'), $tags['test-publication'])); } public function testWithTextType() @@ -182,10 +182,10 @@ protected function firstPublication(): MarkdownDocument return MarkdownDocument::parse(Hyde::pathToRelative($this->getPublicationFiles()[0])); } - protected function updateSchema(string $type, string $name, ?string $tagGroup = null): void + protected function updateSchema(string $type, string $name): void { $this->pubType->fields = [ - (new PublicationField($type, $name, tagGroup: $tagGroup))->toArray(), + (new PublicationField($type, $name))->toArray(), ]; $this->pubType->save(); } diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php index cb25990fef5..f90483423a0 100644 --- a/packages/framework/tests/Feature/PublicationFieldTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -51,9 +51,8 @@ public function test_can_get_field_with_optional_properties_as_array() $this->assertSame([ 'type' => 'string', 'name' => 'test', - 'tagGroup' => 'foo', 'rules' => ['required'], - ], (new PublicationField('string', 'test', 'foo', ['required']))->toArray()); + ], (new PublicationField('string', 'test', ['required']))->toArray()); } public function test_can_encode_field_as_json() @@ -63,7 +62,10 @@ public function test_can_encode_field_as_json() public function test_can_get_field_with_optional_properties_as_json() { - $this->assertSame('{"type":"string","name":"test","tagGroup":"foo","rules":["required"]}', json_encode(new PublicationField('string', 'test', 'foo', ['required']))); + $this->assertSame('{"type":"string","name":"test","rules":["required"]}', json_encode(new PublicationField('string', + 'test', + ['required'] + ))); } public function test_can_construct_type_using_enum_case() @@ -225,7 +227,7 @@ public function testGetRulesForImage() public function testGetRulesForTag() { - $rules = (new PublicationField('tag', 'myTag', tagGroup: 'foo'))->getValidationRules(); + $rules = (new PublicationField('tag', 'myTag'))->getValidationRules(); $this->assertSame(['in:'], $rules->toArray()); }