oliverdavies.uk/src/WebsiteBundle/Command/NewTalkCommand.php

58 lines
1.5 KiB
PHP
Raw Normal View History

2018-07-25 22:44:11 +01:00
<?php
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\Output\OutputInterface;
2018-07-25 23:12:51 +01:00
/**
* Generate a new talk from a stub.
*/
2018-07-25 22:44:11 +01:00
class NewTalkCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
2018-07-25 23:12:51 +01:00
$this->setName('content:new:talk')
2018-07-25 22:44:11 +01:00
->setDescription('Create a new talk')
2018-07-25 23:12:51 +01:00
->addArgument('title', InputArgument::REQUIRED, 'The title of the post');
2018-07-25 22:44:11 +01:00
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$title = $input->getArgument('title');
2018-07-25 23:12:51 +01:00
$filename = string($title)->slugify() . '.php';
2018-07-25 22:44:11 +01:00
2018-07-25 23:12:51 +01:00
if (file_exists($file = __DIR__ . "/../../../source/_talks/{$filename}")) {
$output->writeln("<error>{$filename} already exists.</error>");
exit(1);
}
2018-07-25 22:44:11 +01:00
2018-07-25 23:12:51 +01:00
file_put_contents($file, $this->compileTemplate($title));
$output->writeln("<info>{$filename} was created.</info>");
}
/**
* 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);
2018-07-25 22:44:11 +01:00
}
}