-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakePublicationCommand.php
193 lines (154 loc) · 7.17 KB
/
MakePublicationCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace Hyde\Console\Commands;
use function array_merge;
use Hyde\Console\Commands\Helpers\InputStreamHandler;
use Hyde\Console\Concerns\ValidatingCommand;
use Hyde\Framework\Actions\CreatesNewPublicationPage;
use Hyde\Framework\Features\Publications\Models\PublicationField;
use Hyde\Framework\Features\Publications\Models\PublicationType;
use Hyde\Framework\Features\Publications\PublicationFieldTypes;
use Hyde\Framework\Features\Publications\PublicationService;
use Illuminate\Support\Collection;
use function implode;
use function in_array;
use InvalidArgumentException;
use LaravelZero\Framework\Commands\Command;
/**
* Hyde Command to create a new publication for a given publication type.
*
* @see \Hyde\Framework\Actions\CreatesNewPublicationPage
* @see \Hyde\Framework\Testing\Feature\Commands\MakePublicationCommandTest
*/
class MakePublicationCommand extends ValidatingCommand
{
/** @var string */
protected $signature = 'make:publication
{publicationType? : The name of the publication type to create a publication for}
{--force : Should the generated file overwrite existing publications with the same filename?}';
/** @var string */
protected $description = 'Create a new publication item';
protected PublicationType $publicationType;
public function safeHandle(): int
{
$this->title('Creating a new publication!');
$this->publicationType = $this->getPublicationTypeSelection();
$fieldData = $this->collectFieldData();
$creator = new CreatesNewPublicationPage($this->publicationType, $fieldData, (bool) $this->option('force'));
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?')) {
$creator->force();
} else {
$this->info('Exiting without overwriting existing publication file!');
return ValidatingCommand::USER_EXIT;
}
}
$creator->create();
$this->info("Created file {$creator->getOutputPath()}");
return Command::SUCCESS;
}
protected function getPublicationTypeSelection(): PublicationType
{
$publicationTypes = $this->getPublicationTypes();
$publicationTypeSelection = $this->argument('publicationType') ?? $publicationTypes->keys()->get(
(int) $this->choice(
'Which publication type would you like to create a publication item for?',
$publicationTypes->keys()->toArray()
)
);
if ($publicationTypes->has($publicationTypeSelection)) {
$this->line("<info>Creating a new publication of type</info> [<comment>$publicationTypeSelection</comment>]");
return $publicationTypes->get($publicationTypeSelection);
}
throw new InvalidArgumentException("Unable to locate publication type [$publicationTypeSelection]");
}
/** @return \Illuminate\Support\Collection<string, PublicationType> */
protected function getPublicationTypes(): Collection
{
$publicationTypes = PublicationService::getPublicationTypes();
if ($publicationTypes->isEmpty()) {
throw new InvalidArgumentException('Unable to locate any publication types. Did you create any?');
}
return $publicationTypes;
}
/** @return \Illuminate\Support\Collection<string, string|array|null> */
protected function collectFieldData(): Collection
{
$this->newLine();
$this->info('Now please enter the field data:');
$data = new Collection();
/** @var PublicationField $field */
foreach ($this->publicationType->getFields() as $field) {
$this->newLine();
$data->put($field->name, $this->captureFieldInput($field));
}
return $data;
}
protected function captureFieldInput(PublicationField $field): string|array|null
{
return match ($field->type) {
PublicationFieldTypes::Text => $this->captureTextFieldInput($field),
PublicationFieldTypes::Array => $this->captureArrayFieldInput($field),
PublicationFieldTypes::Image => $this->captureImageFieldInput($field),
PublicationFieldTypes::Tag => $this->captureTagFieldInput($field),
default => $this->askWithValidation($field->name, $field->name, $field->type->rules()),
};
}
protected function captureTextFieldInput(PublicationField $field): string
{
$this->line(InputStreamHandler::formatMessage($field->name));
return implode("\n", InputStreamHandler::call());
}
protected function captureArrayFieldInput(PublicationField $field): array
{
$this->line(InputStreamHandler::formatMessage($field->name));
return InputStreamHandler::call();
}
protected function captureImageFieldInput(PublicationField $field): string|null
{
$this->infoComment('Select file for image field', $field->name);
$mediaFiles = PublicationService::getMediaForPubType($this->publicationType);
if ($mediaFiles->isEmpty()) {
return $this->handleEmptyOptionsCollection('media file', "No media files found in directory _media/{$this->publicationType->getIdentifier()}/");
}
$filesArray = $mediaFiles->toArray();
$selection = (int) $this->choice('Which file would you like to use?', $filesArray);
return $filesArray[$selection];
}
protected function captureTagFieldInput(PublicationField $field): array|string|null
{
$this->infoComment('Select a tag for field', $field->name, "from the {$this->publicationType->getIdentifier()} group");
$options = PublicationService::getValuesForTagName($this->publicationType->getIdentifier());
if ($options->isEmpty()) {
return $this->handleEmptyOptionsCollection('tag', 'No tags for this publication type found in tags.json');
}
$this->tip('You can enter multiple tags separated by commas');
$reloadMessage = '<fg=bright-blue>[Reload tags.json]</>';
do {
$options = PublicationService::getValuesForTagName($this->publicationType->getIdentifier());
$selection = $this->choice(
'Which tag would you like to use?',
array_merge([$reloadMessage], $options->toArray()),
multiple: true
);
} while (in_array($reloadMessage, (array) $selection));
return $selection;
}
/** @return null */
protected function handleEmptyOptionsCollection(string $type, string $message)
{
$this->newLine();
$this->warn("Warning: $message");
// TODO we might want to check if the field has a required rule which should jump straight to the exception
if ($this->confirm('Would you like to skip this field?', true)) {
return null;
} else {
throw new InvalidArgumentException("Unable to locate any {$type}s for this publication type");
}
}
protected function tip(string $message): void
{
$this->line("<fg=bright-blue>Tip:</> $message");
}
}