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