From 6669c726489947267b602201dbeb6e0059ca74ed Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 25 Jul 2018 23:12:51 +0100 Subject: [PATCH] Update new talk command --- src/WebsiteBundle/Command/NewTalkCommand.php | 52 ++++++++++---------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/WebsiteBundle/Command/NewTalkCommand.php b/src/WebsiteBundle/Command/NewTalkCommand.php index 9e941e4a..9587f382 100644 --- a/src/WebsiteBundle/Command/NewTalkCommand.php +++ b/src/WebsiteBundle/Command/NewTalkCommand.php @@ -5,10 +5,11 @@ namespace WebsiteBundle\Command; use Sculpin\Core\Console\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Style\SymfonyStyle; +/** + * Generate a new talk from a stub. + */ class NewTalkCommand extends ContainerAwareCommand { /** @@ -16,27 +17,9 @@ class NewTalkCommand extends ContainerAwareCommand */ protected function configure() { - $this - ->setName('content:new:talk') + $this->setName('content:new:talk') ->setDescription('Create a new talk') - ->addArgument( - 'title', - InputArgument::REQUIRED, - 'The title of the post' - ); - // ->addOption( - // 'filename', - // null, - // InputOption::VALUE_OPTIONAL, - // 'The name of the file to generate' - // ) - // ->addOption( - // 'force', - // 'f', - // InputOption::VALUE_NONE, - // 'Overwrite the file if it already exists' - // ) - ; + ->addArgument('title', InputArgument::REQUIRED, 'The title of the post'); } /** @@ -46,10 +29,29 @@ class NewTalkCommand extends ContainerAwareCommand { $title = $input->getArgument('title'); - $contents = file_get_contents(__DIR__.'/../Resources/stubs/talk.md'); + $filename = string($title)->slugify() . '.php'; - $contents = str_replace('{{ title }}', $title, $contents); + if (file_exists($file = __DIR__ . "/../../../source/_talks/{$filename}")) { + $output->writeln("{$filename} already exists."); + exit(1); + } - file_put_contents(__DIR__.'/../../../source/_talks/' . string($title)->slugify() .'.md', $contents); + file_put_contents($file, $this->compileTemplate($title)); + + $output->writeln("{$filename} was created."); + } + + /** + * Load and compile the template with the correct data. + * + * @param string $title The title of the talk + * + * @return bool|mixed|string + */ + private function compileTemplate($title) + { + $contents = file_get_contents(__DIR__ . '/../Resources/stubs/talk.md'); + + return str_replace('{{ title }}', $title, $contents); } }