|
4 | 4 |
|
5 | 5 | namespace Hyde\Console\Commands;
|
6 | 6 |
|
| 7 | +use function array_merge; |
| 8 | +use Hyde\Console\Commands\Helpers\InputStreamHandler; |
7 | 9 | use Hyde\Console\Commands\Interfaces\CommandHandleInterface;
|
8 | 10 | use Hyde\Console\Concerns\ValidatingCommand;
|
| 11 | +use Hyde\Facades\Filesystem; |
9 | 12 | use Hyde\Framework\Features\Publications\PublicationService;
|
| 13 | +use Hyde\Framework\Services\DiscoveryService; |
10 | 14 | use Hyde\Hyde;
|
11 |
| -use Illuminate\Support\Str; |
| 15 | +use function implode; |
12 | 16 | use LaravelZero\Framework\Commands\Command;
|
13 |
| -use function Safe\file_put_contents; |
| 17 | +use RuntimeException; |
14 | 18 | use function Safe\json_encode;
|
| 19 | +use function sprintf; |
15 | 20 |
|
16 | 21 | /**
|
17 | 22 | * Hyde Command to create a new publication type.
|
18 | 23 | *
|
19 |
| - * @see \Hyde\Framework\Testing\Feature\Commands\MakePublicationTypeCommandTest |
| 24 | + * @see \Hyde\Framework\Testing\Feature\Commands\MakePublicationTagCommandTest |
20 | 25 | */
|
21 | 26 | class MakePublicationTagCommand extends ValidatingCommand implements CommandHandleInterface
|
22 | 27 | {
|
23 | 28 | /** @var string */
|
24 |
| - protected $signature = 'make:publicationTag'; |
| 29 | + protected $signature = 'make:publicationTag {tagName? : The name of the tag to create}'; |
25 | 30 |
|
26 | 31 | /** @var string */
|
27 | 32 | protected $description = 'Create a new publication type tag definition';
|
28 | 33 |
|
29 |
| - public function handle(): int |
| 34 | + protected array $tags; |
| 35 | + protected string $tagName; |
| 36 | + |
| 37 | + public function safeHandle(): int |
30 | 38 | {
|
31 | 39 | $this->title('Creating a new Publication Type Tag!');
|
32 | 40 |
|
33 |
| - $filename = Hyde::pathToRelative('tags.json'); |
34 |
| - $tags = PublicationService::getAllTags(); |
35 |
| - $tagName = $this->askWithValidation('name', 'Tag name', ['required', 'string']); |
36 |
| - if (isset($tags[$tagName])) { |
37 |
| - $this->output->error("Tag [$tagName] already exists"); |
| 41 | + $this->getTagName(); |
38 | 42 |
|
39 |
| - return Command::FAILURE; |
40 |
| - } |
| 43 | + $this->validateTagName(); |
41 | 44 |
|
42 |
| - $lines = []; |
43 |
| - $this->output->writeln('<bg=magenta;fg=white>Enter the tag values (end with an empty line):</>'); |
44 |
| - do { |
45 |
| - $line = Str::replace(["\n", "\r"], '', fgets(STDIN)); |
46 |
| - if ($line === '') { |
47 |
| - break; |
48 |
| - } |
49 |
| - $lines[] = trim($line); |
50 |
| - } while (true); |
51 |
| - $tags[$tagName] = $lines; |
| 45 | + $this->collectTags(); |
52 | 46 |
|
53 |
| - $this->output->writeln(sprintf('Saving tag data to [%s]', $filename)); |
54 |
| - file_put_contents($filename, json_encode($tags, JSON_PRETTY_PRINT)); |
| 47 | + $this->printSelectionInformation(); |
| 48 | + |
| 49 | + $this->saveTagsToDisk(); |
55 | 50 |
|
56 | 51 | return Command::SUCCESS;
|
57 | 52 | }
|
| 53 | + |
| 54 | + protected function getTagName(): void |
| 55 | + { |
| 56 | + $this->tagName = $this->getTagNameFromArgument($this->argument('tagName')) |
| 57 | + ?? $this->askWithValidation('name', 'Tag name', ['required', 'string']); |
| 58 | + } |
| 59 | + |
| 60 | + protected function getTagNameFromArgument(?string $value): ?string |
| 61 | + { |
| 62 | + if ($value) { |
| 63 | + $this->infoComment('Using tag name', $value, 'from command line argument'); |
| 64 | + $this->newLine(); |
| 65 | + |
| 66 | + return $value; |
| 67 | + } |
| 68 | + |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + protected function validateTagName(): void |
| 73 | + { |
| 74 | + if (PublicationService::getAllTags()->has($this->tagName)) { |
| 75 | + throw new RuntimeException("Tag [$this->tagName] already exists"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + protected function collectTags(): void |
| 80 | + { |
| 81 | + $this->info('Enter the tag values: (end with an empty line)'); |
| 82 | + $this->tags = [$this->tagName => InputStreamHandler::call()]; |
| 83 | + } |
| 84 | + |
| 85 | + protected function printSelectionInformation(): void |
| 86 | + { |
| 87 | + $this->line('Adding the following tags:'); |
| 88 | + foreach ($this->tags as $tag => $values) { |
| 89 | + $this->line(sprintf(' <comment>%s</comment>: %s', $tag, implode(', ', $values))); |
| 90 | + } |
| 91 | + $this->newLine(); |
| 92 | + } |
| 93 | + |
| 94 | + protected function saveTagsToDisk(): void |
| 95 | + { |
| 96 | + $this->infoComment('Saving tag data to', |
| 97 | + DiscoveryService::createClickableFilepath(Hyde::path('tags.json')) |
| 98 | + ); |
| 99 | + |
| 100 | + Filesystem::putContents('tags.json', json_encode(array_merge( |
| 101 | + PublicationService::getAllTags()->toArray(), $this->tags |
| 102 | + ), JSON_PRETTY_PRINT)); |
| 103 | + } |
58 | 104 | }
|
0 commit comments