mirror of
https://github.com/getgrav/grav.git
synced 2026-05-06 12:36:30 +02:00
Use twig to process
Currently broken as twig loading does not work... test functionality by changing `theme://` to `themes://` in Grav\Common\Twig\Twig
This commit is contained in:
@@ -1,189 +1,162 @@
|
||||
<?php
|
||||
namespace Grav\Console\Cli\DevTools;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Data;
|
||||
use Grav\Common\File\CompiledYamlFile;
|
||||
use Grav\Common\Filesystem\Folder;
|
||||
use Grav\Common\GPM\GPM;
|
||||
use Grav\Common\Inflector;
|
||||
use Grav\Console\ConsoleCommand;
|
||||
|
||||
/**
|
||||
* Class DevToolsCommand
|
||||
* @package Grav\Console\Cli\
|
||||
*/
|
||||
class DevToolsCommand extends ConsoleCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $component = [];
|
||||
|
||||
/**
|
||||
* @Grav
|
||||
*/
|
||||
protected $grav;
|
||||
|
||||
/**
|
||||
* @var Inflector
|
||||
*/
|
||||
protected $inflector;
|
||||
|
||||
/**
|
||||
* @var Locator
|
||||
*/
|
||||
protected $locator;
|
||||
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @var gpm
|
||||
*/
|
||||
protected $gpm;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function init()
|
||||
{
|
||||
$autoload = require_once GRAV_ROOT . '/vendor/autoload.php';
|
||||
if (!function_exists('curl_version')) {
|
||||
exit('FATAL: CLI requires PHP Curl module to be installed');
|
||||
}
|
||||
|
||||
$this->grav = Grav::instance(array('loader' => $autoload));
|
||||
$this->grav['config']->init();
|
||||
$this->grav['uri']->init();
|
||||
$this->grav['streams'];
|
||||
$this->inflector = $this->grav['inflector'];
|
||||
$this->locator = $this->grav['locator'];
|
||||
$this->gpm = new GPM(true);
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function copyComponent()
|
||||
{
|
||||
$name = $this->component['name'];
|
||||
$folderName = $this->inflector->hyphenize($name);
|
||||
$type = $this->component['type'];
|
||||
$template = $this->component['template'];
|
||||
|
||||
|
||||
$templateFolder = __DIR__ . '/components/' . $type . DS . $template;
|
||||
$componentFolder = $this->locator->findResource($type . 's://') . DS . $folderName;
|
||||
|
||||
Folder::copy($templateFolder, $componentFolder);
|
||||
return;
|
||||
}
|
||||
|
||||
protected function renameComponent()
|
||||
{
|
||||
$name = $this->component['name'];
|
||||
$className = $this->inflector->camelize($name);
|
||||
$folderName = $this->inflector->hyphenize($name);
|
||||
$titleName = $this->inflector->titleize($name);
|
||||
$description = $this->component['description'];
|
||||
$type = $this->component['type'];
|
||||
$template = $this->component['template'];
|
||||
|
||||
unset($this->component['type']);
|
||||
unset($this->component['template']);
|
||||
|
||||
$componentFolder = $this->locator->findResource($type . 's://') . DS . $folderName;
|
||||
|
||||
rename($componentFolder . '/' . $type . '.php' , $componentFolder . DS . $folderName . PLUGIN_EXT);
|
||||
rename($componentFolder . '/' . $type . '.yaml' , $componentFolder . DS . $folderName . YAML_EXT);
|
||||
|
||||
//PHP File
|
||||
|
||||
$data = file_get_contents($componentFolder . DS . $folderName . PLUGIN_EXT);
|
||||
|
||||
$data = str_replace('@@CLASSNAME@@', $className, $data); // @todo dynamic renaming
|
||||
$data = str_replace('@@HYPHENNAME@@', $folderName, $data);
|
||||
|
||||
file_put_contents($componentFolder . DS . $folderName . PLUGIN_EXT, $data);
|
||||
|
||||
//README File
|
||||
|
||||
$data = file_get_contents($componentFolder . DS . 'README.md');
|
||||
|
||||
$data = str_replace('@@NAME@@', $titleName, $data); // @todo dynamic renaming
|
||||
$data = str_replace('@@DESCRIPTION@@', $description, $data);
|
||||
|
||||
file_put_contents($componentFolder . DS . 'README.md', $data);
|
||||
|
||||
//Blueprints File
|
||||
$filename = $componentFolder . '/blueprints' . YAML_EXT;
|
||||
$file = CompiledYamlFile::instance($filename);
|
||||
|
||||
$blueprints = new Data\Blueprints($type . 's://');
|
||||
$blueprint = $blueprints->get($folderName . '/blueprints');
|
||||
|
||||
$obj = new Data\Data([], $blueprint);
|
||||
$obj->merge($this->component);
|
||||
$obj->file($file);
|
||||
$obj->save();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function validateOptions()
|
||||
{
|
||||
foreach (array_filter($this->options) as $type => $value) {
|
||||
$this->validate($type, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @param $value
|
||||
* @param string $extra
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function validate($type, $value, $extra = '')
|
||||
{
|
||||
switch ($type) {
|
||||
case 'name':
|
||||
//Check If name
|
||||
if ($value == null || trim($value) == '') {
|
||||
throw new \RuntimeException('Plugin Name cannot be empty');
|
||||
}
|
||||
if (false != $this->gpm->findPackage($value) || false != $this->gpm->isPluginInstalled($value) || false != $this->gpm->isThemeInstalled($value)) {
|
||||
throw new \RuntimeException('Package exists (either as theme or plugin)');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'description':
|
||||
if($value == null || trim($value) == '') {
|
||||
throw new \RuntimeException('Description cannot be empty');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'developer':
|
||||
if ($value === null || trim($value) == '') {
|
||||
throw new \RuntimeException('Developer\'s Name cannot be empty');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'email':
|
||||
if (!preg_match('/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/', $value)) {
|
||||
throw new \RuntimeException('Not a valid email address');
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace Grav\Console\Cli\DevTools;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Data;
|
||||
use Grav\Common\Filesystem\Folder;
|
||||
use Grav\Common\GPM\GPM;
|
||||
use Grav\Common\Inflector;
|
||||
use Grav\Common\Twig\Twig;
|
||||
use Grav\Common\Utils;
|
||||
use RocketTheme\Toolbox\File\File;
|
||||
use Grav\Console\ConsoleCommand;
|
||||
|
||||
/**
|
||||
* Class DevToolsCommand
|
||||
* @package Grav\Console\Cli\
|
||||
*/
|
||||
class DevToolsCommand extends ConsoleCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $component = [];
|
||||
|
||||
/**
|
||||
* @Grav
|
||||
*/
|
||||
protected $grav;
|
||||
|
||||
/**
|
||||
* @var Inflector
|
||||
*/
|
||||
protected $inflector;
|
||||
|
||||
/**
|
||||
* @var Locator
|
||||
*/
|
||||
protected $locator;
|
||||
|
||||
/**
|
||||
* @var Twig
|
||||
*/
|
||||
protected $twig;
|
||||
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @var gpm
|
||||
*/
|
||||
protected $gpm;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function init()
|
||||
{
|
||||
$autoload = require_once GRAV_ROOT . '/vendor/autoload.php';
|
||||
if (!function_exists('curl_version')) {
|
||||
exit('FATAL: DEVTOOLS requires PHP Curl module to be installed');
|
||||
}
|
||||
|
||||
$this->grav = Grav::instance(array('loader' => $autoload));
|
||||
$this->grav['config']->init();
|
||||
$this->grav['uri']->init();
|
||||
$this->grav['streams'];
|
||||
$this->inflector = $this->grav['inflector'];
|
||||
$this->locator = $this->grav['locator'];
|
||||
$this->twig = new Twig($this->grav);
|
||||
$this->gpm = new GPM(true);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function createComponent()
|
||||
{
|
||||
$name = $this->component['name'];
|
||||
$folderName = $this->inflector->hyphenize($name);
|
||||
$type = $this->component['type'];
|
||||
|
||||
$template = $this->component['template'];
|
||||
$templateFolder = __DIR__ . '/components/' . $type . DS . $template;
|
||||
$componentFolder = $this->locator->findResource($type . 's://') . DS . $folderName;
|
||||
|
||||
//Copy All files to component folder
|
||||
Folder::copy($templateFolder, $componentFolder);
|
||||
|
||||
//Add Twig vars and templates then initialize
|
||||
$this->twig->twig_vars['component'] = $this->component;
|
||||
$this->twig->twig_paths[] = $templateFolder;
|
||||
$this->twig->init();
|
||||
|
||||
//Get all templates of component then process each with twig and save
|
||||
$templates = Folder::all($componentFolder);
|
||||
foreach($templates as $templateFile) {
|
||||
if (Utils::endsWith($templateFile, '.twig') && !Utils::endsWith($templateFile, '.html.twig')) {
|
||||
$content = $this->twig->processTemplate($templateFile);
|
||||
$file = File::instance($componentFolder . DS . str_replace('.twig', '', $templateFile));
|
||||
$file->content($content);
|
||||
$file->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function validateOptions()
|
||||
{
|
||||
foreach (array_filter($this->options) as $type => $value) {
|
||||
$this->validate($type, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @param $value
|
||||
* @param string $extra
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function validate($type, $value, $extra = '')
|
||||
{
|
||||
switch ($type) {
|
||||
case 'name':
|
||||
//Check If name
|
||||
if ($value == null || trim($value) == '') {
|
||||
throw new \RuntimeException('Plugin Name cannot be empty');
|
||||
}
|
||||
if (false != $this->gpm->findPackage($value) || false != $this->gpm->isPluginInstalled($value) || false != $this->gpm->isThemeInstalled($value)) {
|
||||
throw new \RuntimeException('Package exists (either as theme or plugin)');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'description':
|
||||
if($value == null || trim($value) == '') {
|
||||
throw new \RuntimeException('Description cannot be empty');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'developer':
|
||||
if ($value === null || trim($value) == '') {
|
||||
throw new \RuntimeException('Developer\'s Name cannot be empty');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'email':
|
||||
if (!preg_match('/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/', $value)) {
|
||||
throw new \RuntimeException('Not a valid email address');
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,139 +1,138 @@
|
||||
<?php
|
||||
namespace Grav\Console\Cli\DevTools;
|
||||
|
||||
use Grav\Common\Inflector;
|
||||
use Grav\Console\ConsoleCommand;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
/**
|
||||
* Class NewPluginCommand
|
||||
* @package Grav\Console\Cli\DevTools
|
||||
*/
|
||||
class NewPluginCommand extends DevToolsCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* @var Inflector
|
||||
*/
|
||||
//protected $inflector;
|
||||
|
||||
/**
|
||||
* @var Locator
|
||||
*/
|
||||
//protected $locator;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('new-plugin')
|
||||
->setAliases(['newplugin'])
|
||||
->addOption(
|
||||
'name',
|
||||
'pn',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The name of your new Grav plugin'
|
||||
)
|
||||
->addOption(
|
||||
'description',
|
||||
'd',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'A description of your new Grav plugin'
|
||||
)
|
||||
->addOption(
|
||||
'developer',
|
||||
'dv',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The name/username of the developer'
|
||||
)
|
||||
->addOption(
|
||||
'email',
|
||||
'e',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The developer\'s email'
|
||||
)
|
||||
->setDescription('Creates a new Grav plugin with the basic required files')
|
||||
->setHelp('The <info>new-plugin</info> command creates a new Grav instance and performs the creation of a plugin.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null|void
|
||||
*/
|
||||
protected function serve()
|
||||
{
|
||||
$this->init();
|
||||
|
||||
/**
|
||||
* @var array DevToolsCommand $component
|
||||
*/
|
||||
$this->component['type'] = 'plugin';
|
||||
$this->component['template'] = 'blank'; // @todo add prompt for template type when more templates are added
|
||||
$this->component['version'] = '0.1.0'; // @todo add optional non prompting version argument
|
||||
|
||||
$this->options = [
|
||||
'name' => $this->input->getOption('name'),
|
||||
'description' => $this->input->getOption('description'),
|
||||
'author' => [
|
||||
'name' => $this->input->getOption('developer'),
|
||||
'email' => $this->input->getOption('email')
|
||||
]
|
||||
];
|
||||
|
||||
$this->validateOptions();
|
||||
|
||||
$this->component = array_replace($this->component, $this->options);
|
||||
|
||||
$helper = $this->getHelper('question');
|
||||
|
||||
if (!$this->options['name']) {
|
||||
$question = new Question('Enter <yellow>Plugin Name</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('name', $value);
|
||||
});
|
||||
|
||||
$this->component['name'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['description']) {
|
||||
$question = new Question('Enter <yellow>Plugin Description</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('description', $value);
|
||||
});
|
||||
|
||||
$this->component['description'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['author']['name']) {
|
||||
$question = new Question('Enter <yellow>Developer Name</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('developer', $value);
|
||||
});
|
||||
|
||||
$this->component['author']['name'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['author']['email']) {
|
||||
$question = new Question('Enter <yellow>Developer Email</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('email', $value);
|
||||
});
|
||||
|
||||
$this->component['author']['email'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
$this->copyComponent();
|
||||
$this->renameComponent();
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
namespace Grav\Console\Cli\DevTools;
|
||||
|
||||
use Grav\Common\Inflector;
|
||||
use Grav\Console\ConsoleCommand;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
/**
|
||||
* Class NewPluginCommand
|
||||
* @package Grav\Console\Cli\DevTools
|
||||
*/
|
||||
class NewPluginCommand extends DevToolsCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* @var Inflector
|
||||
*/
|
||||
//protected $inflector;
|
||||
|
||||
/**
|
||||
* @var Locator
|
||||
*/
|
||||
//protected $locator;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('new-plugin')
|
||||
->setAliases(['newplugin'])
|
||||
->addOption(
|
||||
'name',
|
||||
'pn',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The name of your new Grav plugin'
|
||||
)
|
||||
->addOption(
|
||||
'description',
|
||||
'd',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'A description of your new Grav plugin'
|
||||
)
|
||||
->addOption(
|
||||
'developer',
|
||||
'dv',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The name/username of the developer'
|
||||
)
|
||||
->addOption(
|
||||
'email',
|
||||
'e',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The developer\'s email'
|
||||
)
|
||||
->setDescription('Creates a new Grav plugin with the basic required files')
|
||||
->setHelp('The <info>new-plugin</info> command creates a new Grav instance and performs the creation of a plugin.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null|void
|
||||
*/
|
||||
protected function serve()
|
||||
{
|
||||
$this->init();
|
||||
|
||||
/**
|
||||
* @var array DevToolsCommand $component
|
||||
*/
|
||||
$this->component['type'] = 'plugin';
|
||||
$this->component['template'] = 'blank'; // @todo add prompt for template type when more templates are added
|
||||
$this->component['version'] = '0.1.0'; // @todo add optional non prompting version argument
|
||||
|
||||
$this->options = [
|
||||
'name' => $this->input->getOption('name'),
|
||||
'description' => $this->input->getOption('description'),
|
||||
'author' => [
|
||||
'name' => $this->input->getOption('developer'),
|
||||
'email' => $this->input->getOption('email')
|
||||
]
|
||||
];
|
||||
|
||||
$this->validateOptions();
|
||||
|
||||
$this->component = array_replace($this->component, $this->options);
|
||||
|
||||
$helper = $this->getHelper('question');
|
||||
|
||||
if (!$this->options['name']) {
|
||||
$question = new Question('Enter <yellow>Plugin Name</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('name', $value);
|
||||
});
|
||||
|
||||
$this->component['name'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['description']) {
|
||||
$question = new Question('Enter <yellow>Plugin Description</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('description', $value);
|
||||
});
|
||||
|
||||
$this->component['description'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['author']['name']) {
|
||||
$question = new Question('Enter <yellow>Developer Name</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('developer', $value);
|
||||
});
|
||||
|
||||
$this->component['author']['name'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['author']['email']) {
|
||||
$question = new Question('Enter <yellow>Developer Email</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('email', $value);
|
||||
});
|
||||
|
||||
$this->component['author']['email'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
$this->createComponent();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,140 +1,147 @@
|
||||
<?php
|
||||
namespace Grav\Console\Cli\DevTools;
|
||||
|
||||
use Grav\Common\Inflector;
|
||||
use Grav\Console\ConsoleCommand;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
/**
|
||||
* Class NewThemeCommand
|
||||
* @package Grav\Console\Cli\DevTools
|
||||
*/
|
||||
class NewThemeCommand extends DevToolsCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('new-theme')
|
||||
->setAliases(['newtheme'])
|
||||
->addOption(
|
||||
'name',
|
||||
'pn',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The name of your new Grav theme'
|
||||
)
|
||||
->addOption(
|
||||
'description',
|
||||
'd',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'A description of your new Grav theme'
|
||||
)
|
||||
->addOption(
|
||||
'developer',
|
||||
'dv',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The name/username of the developer'
|
||||
)
|
||||
->addOption(
|
||||
'email',
|
||||
'e',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The developer\'s email'
|
||||
)
|
||||
->setDescription('Creates a new Grav theme with the basic required files')
|
||||
->setHelp('The <info>new-theme</info> command creates a new Grav instance and performs the creation of a theme.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null|void
|
||||
*/
|
||||
protected function serve()
|
||||
{
|
||||
$this->init();
|
||||
|
||||
/**
|
||||
* @var array DevToolsCommand $component
|
||||
*/
|
||||
$this->component['type'] = 'theme';
|
||||
$this->component['template'] = 'blank'; // @todo add prompt for template type when more templates are added
|
||||
$this->component['version'] = '0.1.0'; // @todo add optional non prompting version argument
|
||||
|
||||
$this->options = [
|
||||
'name' => $this->input->getOption('name'),
|
||||
'description' => $this->input->getOption('description'),
|
||||
'author' => [
|
||||
'name' => $this->input->getOption('developer'),
|
||||
'email' => $this->input->getOption('email')
|
||||
]
|
||||
];
|
||||
|
||||
$this->validateOptions();
|
||||
|
||||
$this->component = array_replace($this->component, $this->options);
|
||||
|
||||
$helper = $this->getHelper('question');
|
||||
|
||||
if (!$this->options['name']) {
|
||||
$question = new Question('Enter <yellow>Theme Name</yellow>: ');
|
||||
// @todo set validator
|
||||
|
||||
$this->component['name'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['description']) {
|
||||
$question = new Question('Enter <yellow>Theme Description</yellow>: ');
|
||||
// @todo set validator
|
||||
|
||||
$this->component['description'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['author']['name']) {
|
||||
$question = new Question('Enter <yellow>Developer Name</yellow>: ');
|
||||
// @todo set validator
|
||||
|
||||
$this->component['author']['name'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['author']['email']) {
|
||||
$question = new Question('Enter <yellow>Developer Email</yellow>: ');
|
||||
// @todo set validator
|
||||
|
||||
$this->component['author']['email'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
$question = new ChoiceQuestion(
|
||||
'Please choose a template type',
|
||||
array('bootstrap', 'inheritence')
|
||||
);
|
||||
$this->component['template'] = $helper->ask($this->input, $this->output, $question);
|
||||
|
||||
if ($this->component['template'] == 'inheritence') {
|
||||
$themes = $this->gpm->getInstalledThemes();
|
||||
$installedThemes = [];
|
||||
foreach($themes as $key => $theme) {
|
||||
array_push($installedThemes, $key);
|
||||
}
|
||||
$question = new ChoiceQuestion(
|
||||
'Please choose a theme to extend: ',
|
||||
$installedThemes
|
||||
);
|
||||
$this->component['extends'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
$this->copyComponent();
|
||||
$this->renameComponent();
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
namespace Grav\Console\Cli\DevTools;
|
||||
|
||||
use Grav\Common\Inflector;
|
||||
use Grav\Console\ConsoleCommand;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
/**
|
||||
* Class NewThemeCommand
|
||||
* @package Grav\Console\Cli\DevTools
|
||||
*/
|
||||
class NewThemeCommand extends DevToolsCommand
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('new-theme')
|
||||
->setAliases(['newtheme'])
|
||||
->addOption(
|
||||
'name',
|
||||
'pn',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The name of your new Grav theme'
|
||||
)
|
||||
->addOption(
|
||||
'description',
|
||||
'd',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'A description of your new Grav theme'
|
||||
)
|
||||
->addOption(
|
||||
'developer',
|
||||
'dv',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The name/username of the developer'
|
||||
)
|
||||
->addOption(
|
||||
'email',
|
||||
'e',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'The developer\'s email'
|
||||
)
|
||||
->setDescription('Creates a new Grav theme with the basic required files')
|
||||
->setHelp('The <info>new-theme</info> command creates a new Grav instance and performs the creation of a theme.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null|void
|
||||
*/
|
||||
protected function serve()
|
||||
{
|
||||
$this->init();
|
||||
|
||||
/**
|
||||
* @var array DevToolsCommand $component
|
||||
*/
|
||||
$this->component['type'] = 'theme';
|
||||
$this->component['template'] = 'blank'; // @todo add prompt for template type when more templates are added
|
||||
$this->component['version'] = '0.1.0'; // @todo add optional non prompting version argument
|
||||
|
||||
$this->options = [
|
||||
'name' => $this->input->getOption('name'),
|
||||
'description' => $this->input->getOption('description'),
|
||||
'author' => [
|
||||
'name' => $this->input->getOption('developer'),
|
||||
'email' => $this->input->getOption('email')
|
||||
]
|
||||
];
|
||||
|
||||
$this->validateOptions();
|
||||
|
||||
$this->component = array_replace($this->component, $this->options);
|
||||
|
||||
$helper = $this->getHelper('question');
|
||||
|
||||
if (!$this->options['name']) {
|
||||
$question = new Question('Enter <yellow>Theme Name</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('name', $value);
|
||||
});
|
||||
|
||||
$this->component['name'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['description']) {
|
||||
$question = new Question('Enter <yellow>Theme Description</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('description', $value);
|
||||
});
|
||||
|
||||
$this->component['description'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['author']['name']) {
|
||||
$question = new Question('Enter <yellow>Developer Name</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('developer', $value);
|
||||
});
|
||||
|
||||
$this->component['author']['name'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
if (!$this->options['author']['email']) {
|
||||
$question = new Question('Enter <yellow>Developer Email</yellow>: ');
|
||||
$question->setValidator(function ($value) {
|
||||
return $this->validate('email', $value);
|
||||
});
|
||||
|
||||
$this->component['author']['email'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
$question = new ChoiceQuestion(
|
||||
'Please choose a template type',
|
||||
array('bootstrap', 'inheritence')
|
||||
);
|
||||
$this->component['template'] = $helper->ask($this->input, $this->output, $question);
|
||||
|
||||
if ($this->component['template'] == 'inheritence') {
|
||||
$themes = $this->gpm->getInstalledThemes();
|
||||
$installedThemes = [];
|
||||
foreach($themes as $key => $theme) {
|
||||
array_push($installedThemes, $key);
|
||||
}
|
||||
$question = new ChoiceQuestion(
|
||||
'Please choose a theme to extend: ',
|
||||
$installedThemes
|
||||
);
|
||||
$this->component['extends'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
$this->createComponent();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# v0.1.0
|
||||
## {{ "now"|date("m/d/Y") }}
|
||||
|
||||
1. [](#new)
|
||||
* ChangeLog started...
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) {{ "now"|date("Y") }} {{ component.author.name }}
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,5 +0,0 @@
|
||||
# @@NAME@@
|
||||
|
||||
The **@@NAME@@** Plugin is for [Grav](http://github.com/getgrav/grav)
|
||||
|
||||
@@DESCRIPTION@@
|
||||
@@ -0,0 +1,5 @@
|
||||
# {{ component.name|titleize }}
|
||||
|
||||
The **{{ component.name|titleize }}** Plugin is for [Grav](http://github.com/getgrav/grav)
|
||||
|
||||
{{ component.description }}
|
||||
@@ -1,3 +0,0 @@
|
||||
cosgrove: "my field"
|
||||
form:
|
||||
validation: loose
|
||||
@@ -0,0 +1,8 @@
|
||||
name: {{ component.name|titleize }}
|
||||
version: 0.1.0
|
||||
description: {{ component.description }}
|
||||
icon: sign-in
|
||||
author:
|
||||
name: {{ component.author.name }}
|
||||
email: {{ component.author.email }}
|
||||
license: MIT
|
||||
@@ -1,90 +1,90 @@
|
||||
<?php
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use Grav\Common\Plugin;
|
||||
|
||||
/**
|
||||
* Class @@NAME@@Plugin
|
||||
* @package Grav\Plugin
|
||||
*/
|
||||
class @@CLASSNAME@@Plugin extends Plugin
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*
|
||||
* Often used to preven unecessary code from running if
|
||||
* certain criteria are not met (such as a matching
|
||||
* URI).
|
||||
*/
|
||||
protected $active = false;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*
|
||||
* The getSubscribedEvents() gives the core a list of events
|
||||
* that the plugin wants to listen to. The key of each
|
||||
* array section is the event that the plugin listens to
|
||||
* and the value (in the form of an array) contains the
|
||||
* callable (or function) as well as the priority. The
|
||||
* higher the number the higher the priority.
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 0]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the plugin
|
||||
*/
|
||||
public function onPluginsInitialized()
|
||||
{
|
||||
if ($this->grav['config']->get('plugins.@@HYPHENNAME@@.enabled')) { // Check if enabled in configuration.
|
||||
|
||||
$this->active = true; // Set's the plugin state to active.
|
||||
|
||||
$this->enable([
|
||||
'onPageInitialized' => ['onPageInitialized', 0] // Adds the onPageInitialized event listener
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Does something with pages
|
||||
*/
|
||||
public function onPageInitialized()
|
||||
{
|
||||
// Exit the function if plugin is not active
|
||||
if (!$this->active){
|
||||
return;
|
||||
}
|
||||
|
||||
// Do things with the page
|
||||
$somevariable = $this->returnTrue(); // Call a protected function and set a variable to it's value
|
||||
|
||||
$this->doSomeProcesses(); // Call a protected function that runs some processes
|
||||
}
|
||||
|
||||
protected function returnTrue()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function doSomeProcesses()
|
||||
{
|
||||
// Generate a random 10 letter string
|
||||
$result = "";
|
||||
$chars = "abcdefghijklmnopqrstuvwxyz";
|
||||
$charArray = str_split($chars);
|
||||
for($i = 0; $i < 10; $i++){
|
||||
$randItem = array_rand($charArray);
|
||||
$result .= "".$charArray[$randItem];
|
||||
}
|
||||
|
||||
// Do some math
|
||||
$two = 8*4;
|
||||
$twentyone = 7*3; // anyone up for poker?
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use Grav\Common\Plugin;
|
||||
|
||||
/**
|
||||
* Class {{ component.name|variableize }}Plugin
|
||||
* @package Grav\Plugin
|
||||
*/
|
||||
class {{ component.name|variableize }}Plugin extends Plugin
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*
|
||||
* Often used to preven unecessary code from running if
|
||||
* certain criteria are not met (such as a matching
|
||||
* URI).
|
||||
*/
|
||||
protected $active = false;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*
|
||||
* The getSubscribedEvents() gives the core a list of events
|
||||
* that the plugin wants to listen to. The key of each
|
||||
* array section is the event that the plugin listens to
|
||||
* and the value (in the form of an array) contains the
|
||||
* callable (or function) as well as the priority. The
|
||||
* higher the number the higher the priority.
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 0]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the plugin
|
||||
*/
|
||||
public function onPluginsInitialized()
|
||||
{
|
||||
if ($this->grav['config']->get('plugins.{{ component.name|hyphenize }}.enabled')) { // Check if enabled in configuration.
|
||||
|
||||
$this->active = true; // Set's the plugin state to active.
|
||||
|
||||
$this->enable([
|
||||
'onPageInitialized' => ['onPageInitialized', 0] // Adds the onPageInitialized event listener
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Does something with pages
|
||||
*/
|
||||
public function onPageInitialized()
|
||||
{
|
||||
// Exit the function if plugin is not active
|
||||
if (!$this->active){
|
||||
return;
|
||||
}
|
||||
|
||||
// Do things with the page
|
||||
$somevariable = $this->returnTrue(); // Call a protected function and set a variable to it's value
|
||||
|
||||
$this->doSomeProcesses(); // Call a protected function that runs some processes
|
||||
}
|
||||
|
||||
protected function returnTrue()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function doSomeProcesses()
|
||||
{
|
||||
// Generate a random 10 letter string
|
||||
$result = "";
|
||||
$chars = "abcdefghijklmnopqrstuvwxyz";
|
||||
$charArray = str_split($chars);
|
||||
for($i = 0; $i < 10; $i++){
|
||||
$randItem = array_rand($charArray);
|
||||
$result .= "".$charArray[$randItem];
|
||||
}
|
||||
|
||||
// Do some math
|
||||
$two = 8*4;
|
||||
$twentyone = 7*3; // anyone up for poker?
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
enabled: true
|
||||
enabled: true
|
||||
@@ -1,5 +0,0 @@
|
||||
# @@NAME@@
|
||||
|
||||
The **@@NAME@@** Theme is for [Grav](http://github.com/getgrav/grav)
|
||||
|
||||
@@DESCRIPTION@@
|
||||
@@ -0,0 +1,5 @@
|
||||
# {{ component.name|titleize }}
|
||||
|
||||
The **{{ component.name|titleize }}** Plugin is for [Grav](http://github.com/getgrav/grav)
|
||||
|
||||
{{ component.description }}
|
||||
@@ -0,0 +1,8 @@
|
||||
name: {{ component.name|titleize }}
|
||||
version: 0.1.0
|
||||
description: {{ component.description }}
|
||||
icon: sign-in
|
||||
author:
|
||||
name: {{ component.author.name }}
|
||||
email: {{ component.author.email }}
|
||||
license: MIT
|
||||
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
namespace Grav\Theme;
|
||||
|
||||
use Grav\Common\Theme;
|
||||
|
||||
class @@CLASSNAME@@Theme extends Theme
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace Grav\Theme;
|
||||
|
||||
use Grav\Common\Theme;
|
||||
|
||||
class {{ component.name|variableize }}Theme extends Theme
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
enabled: true
|
||||
enabled: true
|
||||
Reference in New Issue
Block a user