|
4 | 4 |
|
5 | 5 | namespace Hyde\Console\Commands;
|
6 | 6 |
|
7 |
| -use Hyde\Framework\Actions\PublishesHomepageView; |
| 7 | +use Hyde\Console\Concerns\AsksToRebuildSite; |
| 8 | +use Hyde\Framework\Features\Templates\Homepages; |
| 9 | +use Hyde\Framework\Features\Templates\PublishableContract; |
8 | 10 | use Hyde\Framework\Services\ChecksumService;
|
9 | 11 | use Hyde\Hyde;
|
10 |
| -use Illuminate\Support\Facades\Artisan; |
| 12 | +use Illuminate\Support\Collection; |
11 | 13 | use LaravelZero\Framework\Commands\Command;
|
12 | 14 |
|
13 | 15 | /**
|
|
17 | 19 | */
|
18 | 20 | class PublishHomepageCommand extends Command
|
19 | 21 | {
|
| 22 | + use AsksToRebuildSite; |
| 23 | + |
20 | 24 | /** @var string */
|
21 | 25 | protected $signature = 'publish:homepage {homepage? : The name of the page to publish}
|
22 | 26 | {--force : Overwrite any existing files}';
|
23 | 27 |
|
24 | 28 | /** @var string */
|
25 | 29 | protected $description = 'Publish one of the default homepages to index.blade.php.';
|
26 | 30 |
|
27 |
| - protected string $selected; |
28 |
| - |
29 | 31 | public function handle(): int
|
30 | 32 | {
|
31 |
| - $this->selected = $this->argument('homepage') ?? $this->promptForHomepage(); |
| 33 | + $selected = $this->parseSelection(); |
32 | 34 |
|
33 |
| - if (! $this->canExistingIndexFileBeOverwritten()) { |
34 |
| - $this->error('A modified index.blade.php file already exists. Use --force to overwrite.'); |
| 35 | + if (! Homepages::exists($selected)) { |
| 36 | + $this->error("Homepage $selected does not exist."); |
35 | 37 |
|
36 |
| - return 409; |
| 38 | + return 404; |
37 | 39 | }
|
38 | 40 |
|
39 |
| - $returnValue = (new PublishesHomepageView( |
40 |
| - $this->selected |
41 |
| - ))->execute(); |
42 |
| - |
43 |
| - if (is_numeric($returnValue)) { |
44 |
| - if ($returnValue == 404) { |
45 |
| - $this->error('Homepage '.$this->selected.' does not exist.'); |
| 41 | + if (! $this->canExistingFileBeOverwritten()) { |
| 42 | + $this->error('A modified index.blade.php file already exists. Use --force to overwrite.'); |
46 | 43 |
|
47 |
| - return 404; |
48 |
| - } |
| 44 | + return 409; |
49 | 45 | }
|
50 | 46 |
|
51 |
| - $this->line("<info>Published page</info> [<comment>$this->selected</comment>]"); |
| 47 | + Homepages::get($selected)->publish(true); |
| 48 | + |
| 49 | + $this->line("<info>Published page</info> [<comment>$selected</comment>]"); |
52 | 50 |
|
53 | 51 | $this->askToRebuildSite();
|
54 | 52 |
|
55 | 53 | return Command::SUCCESS;
|
56 | 54 | }
|
57 | 55 |
|
| 56 | + protected function parseSelection(): string |
| 57 | + { |
| 58 | + return $this->argument('homepage') ?? $this->parseChoiceIntoKey($this->promptForHomepage()); |
| 59 | + } |
| 60 | + |
58 | 61 | protected function promptForHomepage(): string
|
59 | 62 | {
|
60 |
| - /** @var string $choice */ |
61 |
| - $choice = $this->choice( |
| 63 | + return $this->choice( |
62 | 64 | 'Which homepage do you want to publish?',
|
63 | 65 | $this->formatPublishableChoices(),
|
64 | 66 | 0
|
65 | 67 | );
|
66 |
| - |
67 |
| - return $this->parseChoiceIntoKey($choice); |
68 | 68 | }
|
69 | 69 |
|
70 | 70 | protected function formatPublishableChoices(): array
|
71 | 71 | {
|
72 |
| - $keys = []; |
73 |
| - foreach (PublishesHomepageView::$homePages as $key => $value) { |
74 |
| - $keys[] = "<comment>$key</comment>: {$value['description']}"; |
75 |
| - } |
| 72 | + return $this->getTemplateOptions()->map(function (array $option, string $key): string { |
| 73 | + return "<comment>$key</comment>: {$option['description']}"; |
| 74 | + })->values()->toArray(); |
| 75 | + } |
76 | 76 |
|
77 |
| - return $keys; |
| 77 | + protected function getTemplateOptions(): Collection |
| 78 | + { |
| 79 | + return Homepages::options()->map(fn (PublishableContract $page): array => $page::toArray()); |
78 | 80 | }
|
79 | 81 |
|
80 | 82 | protected function parseChoiceIntoKey(string $choice): string
|
81 | 83 | {
|
82 | 84 | return strstr(str_replace(['<comment>', '</comment>'], '', $choice), ':', true);
|
83 | 85 | }
|
84 | 86 |
|
85 |
| - protected function canExistingIndexFileBeOverwritten(): bool |
| 87 | + protected function canExistingFileBeOverwritten(): bool |
86 | 88 | {
|
87 |
| - if (! file_exists(Hyde::getBladePagePath('index.blade.php')) || $this->option('force')) { |
| 89 | + if ($this->option('force')) { |
88 | 90 | return true;
|
89 | 91 | }
|
90 | 92 |
|
91 |
| - return ChecksumService::checksumMatchesAny(ChecksumService::unixsumFile( |
92 |
| - Hyde::getBladePagePath('index.blade.php') |
93 |
| - )) || $this->option('force'); |
| 93 | + if (! file_exists(Hyde::getBladePagePath('index.blade.php'))) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + return $this->isTheExistingFileADefaultOne(); |
94 | 98 | }
|
95 | 99 |
|
96 |
| - protected function askToRebuildSite(): void |
| 100 | + protected function isTheExistingFileADefaultOne(): bool |
97 | 101 | {
|
98 |
| - if ($this->option('no-interaction')) { |
99 |
| - return; |
100 |
| - } |
101 |
| - |
102 |
| - if ($this->confirm('Would you like to rebuild the site?', 'Yes')) { |
103 |
| - $this->line('Okay, building site!'); |
104 |
| - Artisan::call('build'); |
105 |
| - $this->info('Site is built!'); |
106 |
| - } else { |
107 |
| - $this->line('Okay, you can always run the build later!'); |
108 |
| - } |
| 102 | + return ChecksumService::checksumMatchesAny(ChecksumService::unixsumFile( |
| 103 | + Hyde::getBladePagePath('index.blade.php') |
| 104 | + )); |
109 | 105 | }
|
110 | 106 | }
|
0 commit comments