Skip to content

Commit

Permalink
OPENEUROPA-1271: Allow valueless options to be used in configuration …
Browse files Browse the repository at this point in the history
…files.
  • Loading branch information
pfrenssen committed Oct 9, 2018
1 parent 88f2741 commit 314ad81
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 21 deletions.
2 changes: 0 additions & 2 deletions config/commands/drupal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ command:
database-password: ${drupal.database.password}
sites-subdir: ${drupal.site.sites_subdir}
config-dir: ${drupal.site.config_dir}
skip-permissions-setup: ${drupal.site.skip_permissions_setup}
drush-setup:
options:
config-dir: ${drupal.root}/drush
Expand All @@ -33,7 +32,6 @@ command:
sites-subdir: ${drupal.site.sites_subdir}
settings-override-file: ${drupal.site.settings_override_file}
force: ${drupal.site.force}
skip-permissions-setup: ${drupal.site.skip_permissions_setup}
permissions-setup:
options:
sites-subdir: ${drupal.site.sites_subdir}
62 changes: 61 additions & 1 deletion src/Commands/AbstractCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace OpenEuropa\TaskRunner\Commands;

use Consolidation\AnnotatedCommand\AnnotationData;
use Robo\Common\ConfigAwareTrait;
use Robo\Common\IO;
use Robo\Contract\BuilderAwareInterface;
use Robo\Contract\ConfigAwareInterface;
use Robo\Contract\IOAwareInterface;
use Robo\Contract\BuilderAwareInterface;
use Robo\Exception\TaskException;
use Robo\LoadAllTasks;
use Robo\Robo;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputInterface;

/**
* Class AbstractCommands
Expand Down Expand Up @@ -47,6 +49,64 @@ public function initializeRuntimeConfiguration(ConsoleCommandEvent $event)
Robo::loadConfiguration([$this->getConfigurationFile()], $this->getConfig());
}

/**
* Returns an array of valueless options and their corresponding config key.
*
* This provides the data for ::initializeValuelessOptions().
*
* @see \OpenEuropa\TaskRunner\Commands\AbstractCommands::initializeValuelessOptions()
*
* @return array
* An associative array, keyed by command name, with each value consisting
* of an associative array where the key is the valueless option name, and
* the value is the location where the option should be placed in the
* configuration array. Example:
* 'drupal:my-command' => ['my-option' => 'drupal.site.my_option']
*/
public function getValuelessConfigurationKeys()
{
return [];
}

/**
* Initializes valueless options.
*
* Valueless options (i.e. options of type `InputOption::VALUE_NONE`) cannot
* be defined in the usual way in a YAML configuration file such as
* `drupal.yml` since the library which is responsible for value
* substitution in config files only does straight string replacement. It
* does not support the notion that the absence of a valueless option means
* that it should be set to `FALSE`.
*
* This method uses the data from `::getValuelessConfigurationKeys()` to
* look up whether the option is present in the specified location in the
* configuration files, and will set the value to FALSE if the option is
* missing. If the option is present it will use its actual value.
*
* If the valueless option is passed on the command line this will take
* precedence over the value in the configuration files.
*
* @hook init *
*/
public function initializeValuelessOptions(InputInterface $input, AnnotationData $annotationData)
{
$command_name = $annotationData->get('command');
$keys = $this->getValuelessConfigurationKeys();
if (!array_key_exists($command_name, $keys)) {
return;
}
foreach ($keys[$command_name] as $option => $key) {
// Check if the option was passed on the command line. This takes
// precedence over the presence of the value in the configuration
// file.
if (!$input->getOption($option)) {
// If not, take the corresponding value from the configuration.
$value = (bool) $this->getConfig()->get($key);
$input->setOption($option, $value);
}
}
}

/**
* @param string $name
* @return string
Expand Down
49 changes: 31 additions & 18 deletions src/Commands/DrupalCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public function getConfigurationFile()
return __DIR__.'/../../config/commands/drupal.yml';
}

/**
* {@inheritdoc}
*/
public function getValuelessConfigurationKeys()
{
return [
'drupal:site-install' => [
'skip-permissions-setup' => 'drupal.site.skip_permissions_setup',
],
];
}

/**
* Set runtime configuration values.
*
Expand Down Expand Up @@ -87,24 +99,25 @@ public function validateSiteInstall(CommandData $commandData)
*
* @command drupal:site-install
*
* @option root Drupal root.
* @option site-name Site name.
* @option site-mail Site mail.
* @option site-profile Installation profile
* @option site-update Whereas to enable the update module or not.
* @option site-locale Default site locale.
* @option account-name Admin account name.
* @option account-password Admin account password.
* @option account-mail Admin email.
* @option database-type Deprecated, use "database-scheme"
* @option database-scheme Database scheme.
* @option database-host Database host.
* @option database-port Database port.
* @option database-name Database name.
* @option database-user Database username.
* @option database-password Database password.
* @option sites-subdir Sites sub-directory.
* @option config-dir Config export directory.
* @option root Drupal root.
* @option site-name Site name.
* @option site-mail Site mail.
* @option site-profile Installation profile
* @option site-update Whereas to enable the update module or not.
* @option site-locale Default site locale.
* @option account-name Admin account name.
* @option account-password Admin account password.
* @option account-mail Admin email.
* @option database-type Deprecated, use "database-scheme"
* @option database-scheme Database scheme.
* @option database-host Database host.
* @option database-port Database port.
* @option database-name Database name.
* @option database-user Database username.
* @option database-password Database password.
* @option sites-subdir Sites sub-directory.
* @option config-dir Config export directory.
* @option skip-permissions-setup Whether to skip making the settings file and folder writable during installation.
*
* @aliases drupal:si,dsi
*
Expand Down

0 comments on commit 314ad81

Please sign in to comment.