initial not complete

This commit is contained in:
Tyler Cosgrove
2016-03-07 17:01:52 -05:00
parent e9c46e7ace
commit 06d9abaede
10 changed files with 560 additions and 10 deletions

View File

@@ -17,8 +17,9 @@ if (!file_exists(__DIR__ . '/../vendor')){
}
use Symfony\Component\Console\Application;
use Grav\Common\Grav;
require_once __DIR__ . '/../vendor/autoload.php';
$autoload = require_once __DIR__ . '/../vendor/autoload.php';
if (version_compare($ver = PHP_VERSION, $req = GRAV_PHP_MIN, '<')) {
exit(sprintf("You are running PHP %s, but Grav needs at least PHP %s to run.\n", $ver, $req));
@@ -32,15 +33,26 @@ if (!file_exists(ROOT_DIR . 'index.php')) {
exit('FATAL: Must be run from ROOT directory of Grav!');
}
$app = new Application('Grav CLI Application', '0.1.0');
if (!function_exists('curl_version')) {
exit('FATAL: CLI requires PHP Curl module to be installed');
}
$grav = Grav::instance(array('loader' => $autoload));
$grav['config']->init();
$grav['uri']->init();
$grav['streams'];
$app = new Application('Grav CLI Application', GRAV_VERSION);
$app->addCommands(array(
new Grav\Console\Cli\InstallCommand(),
new Grav\Console\Cli\ComposerCommand(),
new Grav\Console\Cli\SandboxCommand(),
new Grav\Console\Cli\CleanCommand(),
new Grav\Console\Cli\ClearCacheCommand(),
new Grav\Console\Cli\BackupCommand(),
new Grav\Console\Cli\NewProjectCommand(),
new Grav\Console\Cli\NewUserCommand(),
new \Grav\Console\Cli\InstallCommand(),
new \Grav\Console\Cli\ComposerCommand(),
new \Grav\Console\Cli\SandboxCommand(),
new \Grav\Console\Cli\CleanCommand(),
new \Grav\Console\Cli\ClearCacheCommand(),
new \Grav\Console\Cli\BackupCommand(),
new \Grav\Console\Cli\NewProjectCommand(),
new \Grav\Console\Cli\NewUserCommand(),
new \Grav\Console\Cli\DevTools\NewPluginCommand(),
new \Grav\Console\Cli\DevTools\NewThemeCommand(),
));
$app->run();

View File

@@ -0,0 +1,165 @@
<?php
namespace Grav\Console\Cli\DevTools;
use Grav\Common\Data;
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\Filesystem\Folder;
use Grav\Common\Inflector;
use Grav\Console\ConsoleCommand;
/**
* Class DevToolsCommand
* @package Grav\Console\Cli\
*/
class DevToolsCommand extends ConsoleCommand
{
/**
* @var array
*/
protected $component = [];
/**
* @var Inflector
*/
protected $inflector;
/**
* @var Locator
*/
protected $locator;
/**
*
*/
protected function init()
{
$this->inflector = self::getGrav()['inflector'];
$this->locator = self::getGrav()['locator'];
}
/**
*
*/
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);
//dump($file);
$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');
}
// @todo check for existing plugin/theme
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;
}
}

View File

@@ -0,0 +1,131 @@
<?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>: ');
// @todo set validator
$this->component['name'] = $helper->ask($this->input, $this->output, $question);
}
if (!$this->options['description']) {
$question = new Question('Enter <yellow>Plugin 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);
}
$this->copyComponent();
$this->renameComponent();
}
}

View File

@@ -0,0 +1,143 @@
<?php
namespace Grav\Console\Cli\DevTools;
use Grav\Common\Inflector;
use Grav\Common\GPM\GPM;
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 = [];
/**
* @var gpm
*/
protected $gpm;
/**
*
*/
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();
$this->gpm = new GPM(true);
/**
* @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);
}
if ($this->component['template'] == 'blank') {
//$themesObject = self::getGrav()['themes'];
$themes = $this->gpm->getInstalledThemes();
$installedThemes = [];
foreach($themes as $key => $theme) {
array_push($installedThemes, $key);
}
$question = new ChoiceQuestion(
'Please choose a theme to extend: ',
$installedThemes
);
//dump($this->gpm->getInstalledThemes());
$this->component['extends'] = $helper->ask($this->input, $this->output, $question);
}
//$this->copyComponent();
//$this->renameComponent();
}
}

View File

@@ -0,0 +1,5 @@
# @@NAME@@
The **@@NAME@@** Plugin is for [Grav](http://github.com/getgrav/grav)
@@DESCRIPTION@@

View File

@@ -0,0 +1,3 @@
cosgrove: "my field"
form:
validation: loose

View File

@@ -0,0 +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?
}
}

View File

@@ -0,0 +1 @@
enabled: true