-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HydePublishHomepageCommand.php
99 lines (76 loc) · 2.92 KB
/
HydePublishHomepageCommand.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
<?php
namespace Hyde\Framework\Commands;
use Hyde\Framework\Actions\PublishesHomepageView;
use Hyde\Framework\Concerns\Commands\AsksToRebuildSite;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\FileCacheService;
use LaravelZero\Framework\Commands\Command;
/**
* Publish one of the default homepages.
*/
class HydePublishHomepageCommand extends Command
{
use AsksToRebuildSite;
protected $signature = 'publish:homepage {homepage? : The name of the page to publish}
{--force : Overwrite any existing files}';
protected $description = 'Publish one of the default homepages to index.blade.php.';
protected string $selected;
public function handle(): int
{
$this->selected = $this->argument('homepage') ?? $this->promptForHomepage();
$returnValue = (new PublishesHomepageView(
$this->selected,
$this->canExistingIndexFileBeOverwritten()
))->execute();
// @deprecated version 0.10.0, can be removed as it should not be possible to select a homepage that does not exist, and we can make a pre-check for 409 case.
if ($returnValue === true) {
$this->info('Homepage published successfully!');
} else {
if (is_numeric($returnValue)) {
if ($returnValue == 404) {
$this->error('Homepage '.$this->selected.' does not exist.');
return 404;
}
if ($returnValue == 409) {
$this->error('A modified index.blade.php file already exists. Use --force to overwrite.');
return 409;
}
}
}
$this->askToRebuildSite();
return 0;
}
protected function promptForHomepage(): string
{
$choice = $this->choice(
'Which homepage do you want to publish?',
$this->formatPublishableChoices(),
0
);
$choice = $this->parseChoiceIntoKey($choice);
$this->line("<info>Selected page</info> [<comment>$choice</comment>]");
$this->newLine();
return $choice;
}
protected function formatPublishableChoices(): array
{
$keys = [];
foreach (PublishesHomepageView::$homePages as $key => $value) {
$keys[] = "<comment>$key</comment>: {$value['description']}";
}
return $keys;
}
protected function parseChoiceIntoKey(string $choice): string
{
return strstr(str_replace(['<comment>', '</comment>'], '', $choice), ':', true);
}
protected function canExistingIndexFileBeOverwritten(): bool
{
if (! file_exists(Hyde::path('_pages/index.blade.php')) || $this->option('force')) {
return true;
}
return FileCacheService::checksumMatchesAny(FileCacheService::unixsumFile(
Hyde::path('_pages/index.blade.php')
)) ?? $this->option('force');
}
}