Skip to content

Commit

Permalink
API Add configuration loader, YAML configuration file for commands
Browse files Browse the repository at this point in the history
* Remove username fro member:create
* Move flush option to application instead of base command
  • Loading branch information
robbieaverill committed Jan 3, 2017
1 parent 52e4415 commit 7463be2
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 36 deletions.
3 changes: 2 additions & 1 deletion bin/ssconsole
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* @author Robbie Averill <robbie@averill.co.nz>
*/

require __DIR__ . '/../vendor/autoload.php';
define('CONSOLE_BASE_DIR', __DIR__ . '/..');
require CONSOLE_BASE_DIR . '/vendor/autoload.php';

// Get SilverStripe
$loaded = false;
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
"name": "silverleague/console",
"description": "A better console for SilverStripe applications",
"type": "silverstripe-module",
"require": {
"symfony/console": "^3.2"
},
"license": "BSD-3-Clause",
"license": "MIT",
"authors": [
{
"name": "Robbie Averill",
"email": "robbie@averill.co.nz"
}
],
"require": {
"symfony/console": "^3.2",
"symfony/yaml": "^3.2"
},
"bin": [
"bin/ssconsole"
],
Expand Down
59 changes: 57 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions console.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# silverleague/silverstripe-console: Command configuration
#
# - Add new commands to this file
#
# Note that BuildTask commands are added automatically.
#

Commands:
- SilverLeague\Console\Command\Member\ChangeGroupsCommand
- SilverLeague\Console\Command\Member\ChangePasswordCommand
- SilverLeague\Console\Command\Member\CreateCommand
7 changes: 3 additions & 4 deletions src/Command/Member/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$data = [
'Email' => $this->getOrAskForArgument($input, $output, 'email', 'Email address: '),
'Username' => $this->getOrAskForArgument($input, $output, 'username', 'Username: '),
'Password' => $this->getOrAskForArgument($input, $output, 'password', 'Password: '),
'FirstName' => $this->getOrAskForArgument($input, $output, 'firstname', 'First name: '),
'Surname' => $this->getOrAskForArgument($input, $output, 'surname', 'Surname: ')
];
if (empty($data['Email']) || empty($data['Username']) || empty($data['Password'])) {
if (empty($data['Email']) || empty($data['Password'])) {
$output->writeln('<error>Please enter an email, username and password.</error>');
return;
}
Expand All @@ -58,8 +57,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln('<info>Member created.</info>');

$setRoles = new Question('Do you want to set roles and groups now?', 'yes');
if ($this->getHelper('question')->ask($input, $output, $setRoles)) {
$setGroups = new Question('Do you want to assign groups now?', 'yes');
if ($this->getHelper('question')->ask($input, $output, $setGroups)) {
$command = $this->getApplication()->find('member:change-groups');
$command->run(
new ArrayInput([
Expand Down
14 changes: 0 additions & 14 deletions src/Command/SilverStripeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@
*/
class SilverStripeCommand extends Command
{
/**
* Add a set of default SilverStripe options to all commands
*
* {@inheritDoc}
*
* @param string $name
*/
public function __construct($name = null)
{
parent::__construct($name);

$this->addOption('flush', 'f', null, 'Flush SilverStripe cache and manifest.');
}

/**
* Retrieve an argument from the input interface, or use the Question helper to ask for input
* if it wasn't provided. Will automatically hide input for password fields.
Expand Down
40 changes: 40 additions & 0 deletions src/Framework/Loader/ConfigurationLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace SilverLeague\Console\Framework\Loader;

use RuntimeException;
use SilverLeague\Console\Framework\ConsoleBase;
use Symfony\Component\Yaml\Yaml;

/**
* The Configuration Loader is reponsible for loading the core configuration YAML file, and merging in any
* other configuration files from other locations
*
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
*/
class ConfigurationLoader extends ConsoleBase
{
/**
* The configuration file name to look for
*
* @var string
*/
const CONFIGURATION_FILE = 'console.yml';

/**
* Load the YAML configuration for the application and return it
*
* @return array
* @throws RuntimeException If the filecould not be loaded
*/
public function load()
{
$filename = CONSOLE_BASE_DIR . '/' . self::CONFIGURATION_FILE;
if (!file_exists($filename) || !is_readable($filename)) {
throw new RuntimeException('The configuration YAML file does not exist!');
}

return Yaml::parse($filename);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace SilverLeague\Console\Framework;
namespace SilverLeague\Console\Framework\Loader;

use SilverLeague\Console\Command\Factory;
use SilverLeague\Console\Framework\ConsoleBase;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector;

Expand Down
93 changes: 83 additions & 10 deletions src/Framework/Scaffold.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace SilverLeague\Console\Framework;

use SilverLeague\Console\Framework\Loader\ConfigurationLoader;
use SilverLeague\Console\Framework\Loader\SilverStripeLoader;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputOption;

/**
* The application scaffolder
Expand Down Expand Up @@ -35,15 +38,29 @@ class Scaffold extends ConsoleBase
* The SilverStripe Loader class
* @var SilverStripeLoader
*/
protected $loader;
protected $silverStripeLoader;

/**
* The Configuration Loader class
* @var ConfigurationLoader
*/
protected $configurationLoader;

/**
* The application configuration
*
* @var array
*/
protected $configuration;

/**
* Instantiate the console Application
*/
public function __construct()
{
parent::__construct(new Application);
$this->setLoader(new SilverStripeLoader($this->getApplication()));
$this->setSilverStripeLoader(new SilverStripeLoader($this->getApplication()));
$this->setConfigurationLoader(new ConfigurationLoader($this->getApplication()));
$this->scaffoldApplication();
}

Expand All @@ -64,9 +81,9 @@ public function run()
* @param SilverStripeLoader $loader
* @return self
*/
public function setLoader(SilverStripeLoader $loader)
public function setSilverStripeLoader(SilverStripeLoader $loader)
{
$this->loader = $loader;
$this->silverStripeLoader = $loader;
return $this;
}

Expand All @@ -75,9 +92,56 @@ public function setLoader(SilverStripeLoader $loader)
*
* @return SilverStripeLoader
*/
public function getLoader()
public function getSilverStripeLoader()
{
return $this->silverStripeLoader;
}

/**
* Get the console application's configuration
*
* @return array
*/
public function getConfiguration()
{
if (is_null($this->configuration)) {
$this->setConfiguration($this->getConfigurationLoader()->load());
}
return $this->configuration;
}

/**
* Set the console application's configuration
*
* @param array $configuration
* @return self
*/
public function setConfiguration(array $configuration)
{
$this->configuration = $configuration;
return $this;
}

/**
* Get the configuration loader class
*
* @return ConfigurationLoader
*/
public function getConfigurationLoader()
{
return $this->loader;
return $this->configurationLoader;
}

/**
* Set the configuration loader class
*
* @param ConfigurationLoader
* @return self
*/
public function setConfigurationLoader(ConfigurationLoader $loader)
{
$this->configurationLoader = $loader;
return $this;
}

/**
Expand All @@ -90,13 +154,22 @@ protected function scaffoldApplication()
$this->getApplication()->setName(self::APPLICATION_NAME);
$this->getApplication()->setVersion('Version ' . self::APPLICATION_VERSION);

foreach ($this->getLoader()->getTasks() as $command) {
$this->getApplication()->getDefinition()->addOption(
new InputOption(
'flush',
'f',
null,
'Flush SilverStripe cache and manifest'
)
);

foreach ($this->getSilverStripeLoader()->getTasks() as $command) {
$this->getApplication()->add($command);
}

$this->getApplication()->add(new \SilverLeague\Console\Command\Member\ChangeGroupsCommand);
$this->getApplication()->add(new \SilverLeague\Console\Command\Member\ChangePasswordCommand);
$this->getApplication()->add(new \SilverLeague\Console\Command\Member\CreateCommand);
foreach ($this->getConfiguration()['Commands'] as $commandClass) {
$this->getApplication()->add(new $commandClass);
}

return $this;
}
Expand Down

0 comments on commit 7463be2

Please sign in to comment.