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