|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Console\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Hyde\Console\Concerns\Command; |
|
9
|
|
|
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile; |
|
10
|
|
|
|
|
11
|
|
|
use function sprintf; |
|
12
|
|
|
use function ucwords; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Hyde Command to scaffold a new Markdown Post. |
|
16
|
|
|
*/ |
|
17
|
|
|
class MakePostCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $signature = 'make:post |
|
21
|
|
|
{title? : The title for the Post. Will also be used to generate the filename} |
|
22
|
|
|
{--force : Should the generated file overwrite existing posts with the same filename?}'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $description = 'Scaffold a new Markdown blog post file'; |
|
26
|
|
|
|
|
27
|
|
|
public function handle(): int |
|
28
|
|
|
{ |
|
29
|
|
|
$this->title('Creating a new post!'); |
|
30
|
|
|
|
|
31
|
|
|
$title = $this->getTitle(); |
|
32
|
|
|
|
|
33
|
|
|
[$description, $author, $category] = $this->getSelections(); |
|
34
|
|
|
|
|
35
|
|
|
$creator = new CreatesNewMarkdownPostFile($title, $description, $category, $author); |
|
36
|
|
|
|
|
37
|
|
|
$this->displaySelections($creator); |
|
38
|
|
|
|
|
39
|
|
|
if (! $this->confirm('Do you wish to continue?', true)) { |
|
40
|
|
|
$this->info('Aborting.'); |
|
41
|
|
|
|
|
42
|
|
|
return Command::USER_EXIT; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->createPostFile($creator); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function getTitle(): string |
|
49
|
|
|
{ |
|
50
|
|
|
$this->line($this->argument('title') |
|
51
|
|
|
? '<info>Selected title: '.$this->argument('title')."</info>\n" |
|
52
|
|
|
: 'Please enter the title of the post, it will be used to generate the filename.' |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
return $this->argument('title') |
|
56
|
|
|
?? $this->askForString('What is the title of the post?') |
|
57
|
|
|
?? 'My New Post'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** @return array<?string> */ |
|
61
|
|
|
protected function getSelections(): array |
|
62
|
|
|
{ |
|
63
|
|
|
$this->line('Tip: You can just hit return to use the defaults.'); |
|
64
|
|
|
|
|
65
|
|
|
$description = $this->askForString('Write a short post excerpt/description'); |
|
66
|
|
|
$author = $this->askForString('What is your (the author\'s) name?'); |
|
67
|
|
|
$category = $this->askForString('What is the primary category of the post?'); |
|
68
|
|
|
|
|
69
|
|
|
return [$description, $author, $category]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function displaySelections(CreatesNewMarkdownPostFile $creator): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->info('Creating a post with the following details:'); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($creator->toArray() as $key => $value) { |
|
77
|
|
|
$this->line(sprintf('%s: %s', ucwords($key), $value)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->line("Identifier: {$creator->getIdentifier()}"); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function createPostFile(CreatesNewMarkdownPostFile $creator): int |
|
84
|
|
|
{ |
|
85
|
|
|
try { |
|
86
|
|
|
$path = $creator->save($this->option('force')); |
|
|
|
|
|
|
87
|
|
|
$this->info("Post created! File is saved to $path"); |
|
88
|
|
|
|
|
89
|
|
|
return Command::SUCCESS; |
|
90
|
|
|
} catch (Exception $exception) { |
|
91
|
|
|
$this->error('Something went wrong when trying to save the file!'); |
|
92
|
|
|
$this->warn($exception->getMessage()); |
|
93
|
|
|
|
|
94
|
|
|
if ($exception->getCode() === 409) { |
|
95
|
|
|
$this->comment('If you want to overwrite the file supply the --force flag.'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return (int) $exception->getCode(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|