Skip to content

Commit

Permalink
Merge pull request consolidation#446 from consolidation-org/config-cl…
Browse files Browse the repository at this point in the history
…eanup

Clean up Config class initialization / global input option handling.
  • Loading branch information
greg-1-anderson committed Sep 12, 2016
2 parents fecc4e3 + 3099c48 commit a7205a5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct($name, $version)
);
$this->getDefinition()
->addOption(
new InputOption('--progress-delay', null, InputOption::VALUE_REQUIRED, 'Number of seconds before progress bar is displayed in long-running task collections. Default: 2s.')
new InputOption('--progress-delay', null, InputOption::VALUE_REQUIRED, 'Number of seconds before progress bar is displayed in long-running task collections. Default: 2s.', Config::DEFAULT_PROGRESS_DELAY)
);
$this->getDefinition()
->addOption(
Expand Down
45 changes: 32 additions & 13 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,66 @@
class Config
{
const PROGRESS_BAR_AUTO_DISPLAY_INTERVAL = 'progress-delay';
const DEFAULT_PROGRESS_DELAY = 2;
const SIMULATE = 'simulate';
const SUPRESS_MESSAGES = 'supress-messages';
const DECORATED = 'decorated';

protected $config = [];

public function get($key, $default = null)
/**
* Fet a configuration value
* @param string $key Which config item to look up
* @param string|null $defaultOverride Override usual default value with a different default
* @return mixed
*/
public function get($key, $defaultOverride = null)
{
return isset($this->config[$key]) ? $this->config[$key] : $default;
if (isset($this->config[$key])) {
return $this->config[$key];
}
return $this->getDefault($key, $defaultOverride);
}

/**
* Set a configu value
* @param string $key
* @param mixed $value
* @return Config
*/
public function set($key, $value)
{
$this->config[$key] = $value;
return $this;
}

/**
* Return an associative array containing all of the global configuration
* options and their default values.
* @return array
*/
public function getGlobalOptionDefaultValues()
{
$globalOptions =
[
self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => 2,
self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => self::DEFAULT_PROGRESS_DELAY,
self::SIMULATE => false,
self::SUPRESS_MESSAGES => false,
];

return $globalOptions;
}

public function setGlobalOptions($input)
/**
* Return the default value for a given configuration item.
* @param string $key
* @param string|null $defaultOverride
* @return mixed
*/
public function getDefault($key, $defaultOverride = null)
{
$globalOptions = $this->getGlobalOptionDefaultValues();

foreach ($globalOptions as $option => $default) {
$value = $input->hasOption($option) ? $input->getOption($option) : null;
// Unfortunately, the `?:` operator does not differentate between `0` and `null`
if (!isset($value)) {
$value = $default;
}
$this->set($option, $value);
}
return isset($globalOptions[$key]) ? $globalOptions[$key] : $defaultOverride;
}

public function isSimulated()
Expand Down
13 changes: 12 additions & 1 deletion src/GlobalOptionsEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public static function getSubscribedEvents()
*/
public function setGlobalOptions(ConsoleCommandEvent $event)
{
$this->getConfig()->setGlobalOptions($event->getInput());
$config = $this->getConfig();
$input = $event->getInput();
$globalOptions = $config->getGlobalOptionDefaultValues();

foreach ($globalOptions as $option => $default) {
$value = $input->hasOption($option) ? $input->getOption($option) : null;
// Unfortunately, the `?:` operator does not differentate between `0` and `null`
if (!isset($value)) {
$value = $default;
}
$config->set($option, $value);
}
}
}

0 comments on commit a7205a5

Please sign in to comment.