Commit 16809b5b authored by Jon Pugh's avatar Jon Pugh

Reformat code according to Symfony2 guidelines.

parent e6f86ede
......@@ -27,39 +27,40 @@ use Symfony\Component\Console\Application as BaseApplication;
*/
class Application extends BaseApplication
{
/**
* @var string
*/
const NAME = 'Aegir Provision';
/**
* @var string
*/
const VERSION = '4.x';
/**
* @var string
*/
const CONSOLE_CONFIG = '.provision.yml';
/**
* @var string
*/
const DEFAULT_TIMEZONE = 'America/New_York';
public function __construct()
{
// If no timezone is set, set Default.
if (empty(ini_get('date.timezone'))) {
date_default_timezone_set($this::DEFAULT_TIMEZONE);
}
// Load Configs
$this->config = new Config();
/**
* @var string
*/
const NAME = 'Aegir Provision';
/**
* @var string
*/
const VERSION = '4.x';
/**
* @var string
*/
const CONSOLE_CONFIG = '.provision.yml';
/**
* @var string
*/
const DEFAULT_TIMEZONE = 'America/New_York';
public function __construct()
{
// If no timezone is set, set Default.
if (empty(ini_get('date.timezone'))) {
date_default_timezone_set($this::DEFAULT_TIMEZONE);
}
// Load Configs
$this->config = new Config();
parent::__construct($this::NAME, $this::VERSION);
}
parent::__construct($this::NAME, $this::VERSION);
}
/**
* @var Config
......@@ -88,27 +89,37 @@ class Application extends BaseApplication
$this->config = $config;
}
/**
* Initializes all the default commands.
*/
protected function getDefaultCommands() {
$commands[] = new HelpCommand();
$commands[] = new ListCommand();
$commands[] = new SaveCommand();
$commands[] = new ShellCommand();
$commands[] = new StatusCommand();
return $commands;
}
/**
* {@inheritdoc}
*
* Adds "--target" option.
*/
protected function getDefaultInputDefinition()
{
$inputDefinition = parent::getDefaultInputDefinition();
$inputDefinition->addOption(new InputOption('--target', '-t', InputOption::VALUE_NONE, 'The target context to act on.'));
return $inputDefinition;
}
/**
* Initializes all the default commands.
*/
protected function getDefaultCommands()
{
$commands[] = new HelpCommand();
$commands[] = new ListCommand();
$commands[] = new SaveCommand();
$commands[] = new ShellCommand();
$commands[] = new StatusCommand();
return $commands;
}
/**
* {@inheritdoc}
*
* Adds "--target" option.
*/
protected function getDefaultInputDefinition()
{
$inputDefinition = parent::getDefaultInputDefinition();
$inputDefinition->addOption(
new InputOption(
'--target',
'-t',
InputOption::VALUE_NONE,
'The target context to act on.'
)
);
return $inputDefinition;
}
}
......@@ -9,48 +9,53 @@ use Symfony\Component\Console\Output\OutputInterface;
/**
* Class Command
*
* @package Aegir\Provision\Command
*/
abstract class Command extends BaseCommand
{
use CommandTrait;
use CommandTrait;
/**
* @var \Symfony\Component\Console\Input\InputInterface
*/
protected $input;
/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;
/**
* @var \Symfony\Component\Console\Input\InputInterface
*/
protected $input;
/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;
/**
* @var Config
*/
private $config;
/**
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
/**
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*/
protected function initialize(
InputInterface $input,
OutputInterface $output
) {
$this->input = $input;
$this->output = $output;
$this->config = $this->getApplication()->getConfig();
}
}
/**
* Run a process.
*
* @param $cmd
*/
protected function process($cmd) {
$this->output->writeln(["Running: $cmd"]);
shell_exec($cmd);
}
/**
* Run a process.
*
* @param $cmd
*/
protected function process($cmd)
{
$this->output->writeln(["Running: $cmd"]);
shell_exec($cmd);
}
/**
* Gets the application instance for this command.
......
......@@ -16,33 +16,51 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
class SaveCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('save')
->setDescription('Save Provision Context.')
->setHelp('Saves a ProvisionContext object to file. Currently just passes to "drush provision-save".')
->setDefinition($this->getCommandDefinition())
;
->setName('save')
->setDescription('Save Provision Context.')
->setHelp(
'Saves a ProvisionContext object to file. Currently just passes to "drush provision-save".'
)
->setDefinition($this->getCommandDefinition());
}
/**
* Generate the list of options derived from ProvisionContextType classes.
*
* @return \Symfony\Component\Console\Input\InputDefinition
*/
protected function getCommandDefinition() {
$inputDefinition = [];
$inputDefinition[] = new InputArgument('context_name', InputArgument::REQUIRED, 'Context to save');
$inputDefinition[] = new InputOption('context_type', NULL, InputOption::VALUE_OPTIONAL, 'server, platform, or site; default server', 'server');
$inputDefinition[] = new InputOption('delete', NULL, InputOption::VALUE_OPTIONAL, 'Remove the alias.');
/**
* Generate the list of options derived from ProvisionContextType classes.
*
* @return \Symfony\Component\Console\Input\InputDefinition
*/
protected function getCommandDefinition()
{
$inputDefinition = [];
$inputDefinition[] = new InputArgument(
'context_name',
InputArgument::REQUIRED,
'Context to save'
);
$inputDefinition[] = new InputOption(
'context_type',
null,
InputOption::VALUE_OPTIONAL,
'server, platform, or site; default server',
'server'
);
$inputDefinition[] = new InputOption(
'delete',
null,
InputOption::VALUE_OPTIONAL,
'Remove the alias.'
);
// @TODO: Load up all ProvisionContextTypes and inject their options.
// @TODO: Load up all ProvisionContextTypes and inject their options.
return new InputDefinition($inputDefinition);
return new InputDefinition($inputDefinition);
}
/**
......@@ -50,9 +68,11 @@ class SaveCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Saving context: " . $input->getArgument('context_name'));
$output->writeln(
"Saving context: ".$input->getArgument('context_name')
);
$command = 'drush provision-save ' . $input->getArgument('context_name');
$this->process($command);
$command = 'drush provision-save '.$input->getArgument('context_name');
$this->process($command);
}
}
......@@ -15,15 +15,16 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
class ShellCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('shell')
->setDescription($this->trans('commands.shell.description'))
->setHelp($this->trans('commands.shell.help'));
->setName('shell')
->setDescription($this->trans('commands.shell.description'))
->setHelp($this->trans('commands.shell.help'));
}
/**
......
......@@ -17,16 +17,16 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
class StatusCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('status')
->setDescription('Display system status.')
->setHelp('Lists helpful information about your system.')
;
->setName('status')
->setDescription('Display system status.')
->setHelp('Lists helpful information about your system.');
}
/**
......
......@@ -16,12 +16,13 @@ use Symfony\Component\Yaml\Yaml;
*/
class Config implements ConfigurationInterface
{
/**
* Configuration values array.
*
* @var array
*/
private $config = array();
private $config = [];
/**
* Path to config YML file.
......@@ -42,7 +43,7 @@ class Config implements ConfigurationInterface
*/
public function __construct()
{
$this->config_path = $this->getHomeDir() . '/' . $this->config_filename;
$this->config_path = $this->getHomeDir().'/'.$this->config_filename;
try {
$processor = new Processor();
......@@ -52,7 +53,9 @@ class Config implements ConfigurationInterface
}
$this->config = $processor->processConfiguration($this, $configs);
} catch (\Exception $e) {
throw new \Exception('There is an error with your configuration: '.$e->getMessage());
throw new \Exception(
'There is an error with your configuration: '.$e->getMessage()
);
}
......@@ -66,18 +69,17 @@ class Config implements ConfigurationInterface
$tree_builder = new TreeBuilder();
$root_node = $tree_builder->root('aegir');
$root_node
->children()
->scalarNode('aegir_root')
->defaultValue('/var/aegir')
->end()
->scalarNode('script_user')
->defaultValue('aegir')
->end()
->scalarNode('config_path')
->defaultValue('/var/aegir/config')
->end()
->end();
;
->children()
->scalarNode('aegir_root')
->defaultValue('/var/aegir')
->end()
->scalarNode('script_user')
->defaultValue('aegir')
->end()
->scalarNode('config_path')
->defaultValue('/var/aegir/config')
->end()
->end();;
return $tree_builder;
}
......@@ -94,7 +96,10 @@ class Config implements ConfigurationInterface
public function get($key, $name = null)
{
if ($name) {
return array_key_exists($name, $this->config[$key]) ? $this->config[$key][$name] : null;
return array_key_exists(
$name,
$this->config[$key]
) ? $this->config[$key][$name] : null;
} else {
return $this->has($key) ? $this->config[$key] : null;
}
......@@ -119,7 +124,7 @@ class Config implements ConfigurationInterface
*
* @param string $key
* Key of the param to get.
* @param mixed $val
* @param mixed $val
* Value of the param to set.
*
* @return bool
......@@ -143,11 +148,11 @@ class Config implements ConfigurationInterface
/**
* Add a config param value to a config array.
*
* @param string $key
* @param string $key
* Key of the group to set to.
* @param string|array $names
* Name of the new object to set.
* @param mixed $val
* @param mixed $val
* Value of the new object to set.
*
* @return bool
......@@ -187,6 +192,7 @@ class Config implements ConfigurationInterface
/**
* Saves the config class to file.
*
* @return bool
*/
public function save()
......@@ -198,6 +204,7 @@ class Config implements ConfigurationInterface
try {
$fs->dumpFile($this->config_path, $dumper->dump($this->config, 10));
return true;
} catch (IOExceptionInterface $e) {
return false;
......@@ -208,6 +215,7 @@ class Config implements ConfigurationInterface
* Returns the appropriate home directory.
*
* Adapted from Terminus Package Manager by Ed Reel
*
* @author Ed Reel <@uberhacker>
* @url https://github.com/uberhacker/tpm
*
......@@ -225,6 +233,7 @@ class Config implements ConfigurationInterface
$home = getenv('HOMEPATH');
}
}
return $home;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment