addCommands([
new class extends Command
{
/**
* {@inheritdoc}
*/
protected static $defaultName = 'main';
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->addArgument('title', InputArgument::OPTIONAL);
$this->addArgument('excerpt', InputArgument::OPTIONAL);
$this->addArgument('tags', InputArgument::IS_ARRAY);
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
if (!$input->getArgument('title')) {
$input->setArgument('title', $io->askQuestion(
new Question('The post title')
));
}
if (!$input->getArgument('excerpt')) {
$input->setArgument('excerpt', $io->askQuestion(
new Question('The post excerpt')
));
}
$io->writeln("\nEnter any tags for the post.\nPress enter to continue.\n");
$tags = [];
while (true) {
$tag = $io->askQuestion(
new Question('Enter a tag')
);
if (empty($tag)) {
break;
}
$tags[] = $tag;
}
$input->setArgument('tags', $tags);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$title = $input->getArgument('title');
if (!$title) {
throw new \RuntimeException('No title');
}
$excerpt = $input->getArgument('excerpt') ?? $title;
$slug = (new Str($title))->slugify();
$tags = '[' . implode(', ', $input->getArgument('tags')) . ']';
$date = (new \DateTime())->format('Y-m-d');
$contents = $this->generateFileFromStub(
$title,
$excerpt,
$tags,
$date
);
$this->writeFile($slug, $contents);
$this->createImagesDirectory($slug);
$io->listing([
"{$slug}.md"
]);
}
private function generateFileFromStub(...$args): string
{
[$title, $excerpt, $tags, $date] = $args;
return <<setDefaultCommand('main');
$application->run();