-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from RossJHagan/init-template
adds an init:template command (fix #40)
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Couscous\Application\Cli; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Initialize a new Couscous template. | ||
* | ||
* @author Ross J. Hagan <[email protected]> | ||
*/ | ||
class InitTemplateCommand extends Command { | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setName('init:template') | ||
->setDescription('Initialize a new Couscous template'); | ||
|
||
$this->addArgument( | ||
'template_name', | ||
InputArgument::REQUIRED, | ||
'Template name' | ||
) | ||
->addArgument( | ||
'directory', | ||
InputArgument::OPTIONAL, | ||
'Directory name', | ||
'website' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
|
||
$fileExtension = '.twig'; | ||
|
||
$dirName = $input->getArgument('directory'); | ||
$directory = getcwd() . '/' . $dirName . '/'; | ||
$templateName = $input->getArgument('template_name') . $fileExtension; | ||
|
||
$fileLocation = $directory . $templateName; | ||
$fileExists = file_exists($fileLocation); | ||
|
||
if (! file_exists(getcwd() . '/' . $dirName)) { | ||
$output->writeln('<comment>Creating directory.</comment>'); | ||
mkdir(getcwd() . '/' . $dirName); | ||
} | ||
|
||
if ($fileExists) { | ||
$output->writeln('<error>That template exists at ' . $fileLocation . ', so nothing has been changed.</error>'); | ||
$output->writeln('<error>Try another name!</error>'); | ||
return; | ||
} | ||
|
||
if (! $fileExists) { | ||
$output->writeln('<comment>Initialising template.</comment>'); | ||
$template = <<<HTML | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>My project!</title> | ||
</head> | ||
<body> | ||
{% block content %} | ||
<p> | ||
Don't forget you can add variables into your YAML front matter, or in your couscous.yml | ||
then use them inside double curly braces! | ||
</p> | ||
<p> | ||
Also, set up a baseUrl in your couscous.yml and use it | ||
to <a href="{{ baseUrl }}/link/in/the/site">link to another page</a> in your site | ||
</p> | ||
{{ content|raw }} | ||
{% endblock %} | ||
</body> | ||
</html> | ||
HTML; | ||
file_put_contents($fileLocation, $template); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters