Skip to content

Commit edf9e02

Browse files
authored
Merge pull request #793 from hydephp/refactor-publication-field-classes
Refactor publication page creation internals
2 parents c15c614 + 6d26e9e commit edf9e02

File tree

7 files changed

+159
-65
lines changed

7 files changed

+159
-65
lines changed

packages/framework/src/Console/Commands/Helpers/InputStreamHandler.php

-6
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,4 @@ public static function mockInput(string $input): void
5454
{
5555
self::$mockedStreamBuffer = explode("\n", $input);
5656
}
57-
58-
/** Format a consistent message that can be output to the console */
59-
public static function formatMessage(string $name, string $type = 'values'): string
60-
{
61-
return "<info>Enter $type for field </>[<comment>$name</comment>] (end with an empty line)";
62-
}
6357
}

packages/framework/src/Console/Commands/MakePublicationCommand.php

+43-33
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Hyde\Framework\Features\Publications\PublicationFieldTypes;
1919
use Hyde\Framework\Features\Publications\PublicationService;
2020
use Illuminate\Support\Collection;
21+
use Illuminate\Support\Str;
2122
use function implode;
2223
use function in_array;
2324
use InvalidArgumentException;
@@ -42,15 +43,19 @@ class MakePublicationCommand extends ValidatingCommand
4243

4344
protected PublicationType $publicationType;
4445

46+
/** @var \Illuminate\Support\Collection<string, PublicationType> */
47+
protected Collection $fieldData;
48+
4549
public function safeHandle(): int
4650
{
4751
$this->title('Creating a new publication!');
4852

4953
$this->publicationType = $this->getPublicationTypeSelection();
54+
$this->fieldData = new Collection();
5055

51-
$fieldData = $this->collectFieldData();
56+
$this->collectFieldData();
5257

53-
$creator = new CreatesNewPublicationPage($this->publicationType, $fieldData, (bool) $this->option('force'));
58+
$creator = new CreatesNewPublicationPage($this->publicationType, $this->fieldData, (bool) $this->option('force'));
5459
if ($creator->hasFileConflict()) {
5560
$this->error('Error: A publication already exists with the same canonical field value');
5661
if ($this->confirm('Do you wish to overwrite the existing file?')) {
@@ -63,20 +68,18 @@ public function safeHandle(): int
6368
}
6469
$creator->create();
6570

66-
$this->info("Created file {$creator->getOutputPath()}");
71+
$this->infoComment('All done! Created file', $creator->getOutputPath());
6772

6873
return Command::SUCCESS;
6974
}
7075

7176
protected function getPublicationTypeSelection(): PublicationType
7277
{
7378
$publicationTypes = $this->getPublicationTypes();
74-
75-
$publicationTypeSelection = $this->argument('publicationType') ?? $publicationTypes->keys()->get(
76-
(int) $this->choice(
77-
'Which publication type would you like to create a publication item for?',
78-
$publicationTypes->keys()->toArray()
79-
)
79+
$publicationTypeSelection = $this->argument('publicationType') ??
80+
$this->choice(
81+
'Which publication type would you like to create a publication item for?',
82+
$publicationTypes->keys()->toArray()
8083
);
8184

8285
if ($publicationTypes->has($publicationTypeSelection)) {
@@ -88,7 +91,6 @@ protected function getPublicationTypeSelection(): PublicationType
8891
throw new InvalidArgumentException("Unable to locate publication type [$publicationTypeSelection]");
8992
}
9093

91-
/** @return \Illuminate\Support\Collection<string, PublicationType> */
9294
protected function getPublicationTypes(): Collection
9395
{
9496
$publicationTypes = PublicationService::getPublicationTypes();
@@ -99,57 +101,50 @@ protected function getPublicationTypes(): Collection
99101
return $publicationTypes;
100102
}
101103

102-
/** @return \Illuminate\Support\Collection<string, string|array|null> */
103-
protected function collectFieldData(): Collection
104+
protected function collectFieldData(): void
104105
{
105106
$this->newLine();
106107
$this->info('Now please enter the field data:');
107-
$data = new Collection();
108108

109109
/** @var PublicationField $field */
110110
foreach ($this->publicationType->getFields() as $field) {
111111
if (str_starts_with($field->name, '__')) {
112112
continue;
113113
}
114+
114115
$this->newLine();
115116
$fieldInput = $this->captureFieldInput($field);
116-
if ($fieldInput !== null) {
117-
$data->put($field->name, $fieldInput);
117+
if (empty($fieldInput)) {
118+
$this->line("<fg=gray> > Skipping field $field->name</>");
119+
} else {
120+
$this->fieldData->put($field->name, $fieldInput);
118121
}
119122
}
120123

121-
return $data;
124+
$this->newLine();
122125
}
123126

124127
protected function captureFieldInput(PublicationField $field): ?PublicationFieldValue
125128
{
126-
$selection = match ($field->type) {
129+
return match ($field->type) {
127130
PublicationFieldTypes::Text => $this->captureTextFieldInput($field),
128131
PublicationFieldTypes::Array => $this->captureArrayFieldInput($field),
129132
PublicationFieldTypes::Image => $this->captureImageFieldInput($field),
130133
PublicationFieldTypes::Tag => $this->captureTagFieldInput($field),
131-
default => new ($field->type->fieldClass())($this->askWithValidation($field->name, "Enter data for field </>[<comment>$field->name</comment>]", $field->getValidationRules()->toArray())),
134+
default => $this->captureOtherFieldInput($field),
132135
};
133-
134-
if (empty($selection)) {
135-
$this->line("<fg=gray> > Skipping field $field->name</>");
136-
137-
return null;
138-
}
139-
140-
return $selection;
141136
}
142137

143138
protected function captureTextFieldInput(PublicationField $field): TextField
144139
{
145-
$this->line(InputStreamHandler::formatMessage($field->name, 'lines'));
140+
$this->infoComment('Enter lines for field', $field->name, '</>(end with an empty line)');
146141

147142
return new TextField(implode("\n", InputStreamHandler::call()));
148143
}
149144

150145
protected function captureArrayFieldInput(PublicationField $field): ArrayField
151146
{
152-
$this->line(InputStreamHandler::formatMessage($field->name));
147+
$this->infoComment('Enter values for field', $field->name, '</>(end with an empty line)');
153148

154149
return new ArrayField(InputStreamHandler::call());
155150
}
@@ -163,10 +158,7 @@ protected function captureImageFieldInput(PublicationField $field): ?ImageField
163158
return $this->handleEmptyOptionsCollection($field, 'media file', "No media files found in directory _media/{$this->publicationType->getIdentifier()}/");
164159
}
165160

166-
$filesArray = $mediaFiles->toArray();
167-
$selection = (int) $this->choice('Which file would you like to use?', $filesArray);
168-
169-
return new ImageField($filesArray[$selection]);
161+
return new ImageField($this->choice('Which file would you like to use?', $mediaFiles->toArray()));
170162
}
171163

172164
protected function captureTagFieldInput(PublicationField $field): ?TagField
@@ -189,6 +181,24 @@ protected function captureTagFieldInput(PublicationField $field): ?TagField
189181
return new TagField($choice);
190182
}
191183

184+
protected function captureOtherFieldInput(PublicationField $field): ?PublicationFieldValue
185+
{
186+
$selection = $this->askForFieldData($field->name, $field->getValidationRules()->toArray());
187+
if (empty($selection)) {
188+
return null;
189+
}
190+
191+
$namespace = Str::beforeLast(PublicationFieldValue::class, '\\');
192+
$className = "$namespace\\{$field->type->name}Field";
193+
194+
return new $className($selection);
195+
}
196+
197+
protected function askForFieldData(string $name, array $rules): string
198+
{
199+
return $this->askWithValidation($name, "Enter data for field </>[<comment>$name</comment>]", $rules);
200+
}
201+
192202
/** @return null */
193203
protected function handleEmptyOptionsCollection(PublicationField $field, string $type, string $message)
194204
{
@@ -197,7 +207,7 @@ protected function handleEmptyOptionsCollection(PublicationField $field, string
197207
}
198208

199209
$this->newLine();
200-
$this->warn(" <fg=red>Warning:</> $message");
210+
$this->warn("<fg=red>Warning:</> $message");
201211
if ($this->confirm('Would you like to skip this field?', true)) {
202212
return null;
203213
} else {

packages/framework/src/Framework/Actions/CreatesNewPublicationPage.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CreatesNewPublicationPage extends CreateAction implements CreateActionCont
3434
*/
3535
public function __construct(PublicationType $pubType, Collection $fieldData, bool $force = false)
3636
{
37-
$fieldData->prepend(new DatetimeField(Carbon::now()->format('Y-m-d H:i:s')), '__createdAt');
37+
$fieldData->prepend(new DatetimeField((string) Carbon::now()), '__createdAt');
3838

3939
$this->pubType = $pubType;
4040
$this->fieldData = $fieldData;
@@ -56,11 +56,11 @@ protected function getCanonicalValue(): string
5656
{
5757
$canonicalFieldName = $this->pubType->canonicalField;
5858
if ($canonicalFieldName === '__createdAt') {
59-
return $this->getFieldValue('__createdAt')->getValue()->format('Y-m-d H:i:s');
59+
return $this->getFieldFromCollection('__createdAt')->getValue()->format('Y-m-d H:i:s');
6060
}
6161

6262
if ($this->fieldData->has($canonicalFieldName)) {
63-
$field = $this->getFieldValue($canonicalFieldName);
63+
$field = $this->getFieldFromCollection($canonicalFieldName);
6464

6565
return (string) $field->getValue(); // TODO here we can check if field has interface allowing it to be canonical, else throw exception
6666
} else {
@@ -87,7 +87,7 @@ protected function normalizeData(Collection $data): array
8787
})->toArray();
8888
}
8989

90-
protected function getFieldValue(string $key): PublicationFieldValue
90+
protected function getFieldFromCollection(string $key): PublicationFieldValue
9191
{
9292
return $this->fieldData->get($key);
9393
}

packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php

-5
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,4 @@ public static function canonicable(): array
7777
self::Text,
7878
];
7979
}
80-
81-
public function fieldClass(): string
82-
{
83-
return "Hyde\\Framework\\Features\\Publications\\Models\\PublicationFieldValues\\{$this->name}Field";
84-
}
8580
}

0 commit comments

Comments
 (0)