Skip to content

Commit

Permalink
Added new config:import:list and config:export:list commands. Fix no …
Browse files Browse the repository at this point in the history
…return on configImport method for config:import:single command (#2965)

* Fix configImport not returning boolean

* Added new config:import:list and config:export:list commands

* Small adjustements

* Added possibility to use absolute or relative to drupal root path for directory and config-list-file parameter/argument

* Added ability to use chain command for importing or exporting multiple configs with the config:import:single and config:export:single commands
  • Loading branch information
bpresles authored and jmolivas committed Dec 17, 2016
1 parent fb26c80 commit 259019c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 48 deletions.
6 changes: 6 additions & 0 deletions export-config-list-sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
commands:
- command: 'config:export:single'
options:
config-names:
- core.extension
- system.site
6 changes: 6 additions & 0 deletions import-config-list-sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
commands:
- command: 'config:import:single'
options:
config-names:
- system.site
- core.extension
42 changes: 26 additions & 16 deletions src/Command/Config/ExportSingleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ protected function configure()
->setDescription($this->trans('commands.config.export.single.description'))
->addArgument(
'config-name',
InputArgument::REQUIRED,
InputArgument::OPTIONAL,
$this->trans('commands.config.export.single.arguments.config-name')
)
->addOption(
'config-names',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.config.export.single.arguments.config-names')
)
->addOption(
'directory',
'',
Expand Down Expand Up @@ -229,28 +235,32 @@ protected function execute(InputInterface $input, OutputInterface $output)

$directory = $input->getOption('directory');
$module = $input->getOption('module');
$configName = $input->getArgument('config-name');
$configNames = $input->getOption('config-names');
$configNameArg = $input->getArgument('config-name');
$optionalConfig = $input->getOption('optional-config');
$removeUuid = $input->getOption('remove-uuid');
$removeHash = $input->getOption('remove-config-hash');

$config = $this->getConfiguration($configName, $removeUuid, $removeHash);

if ($config) {
if (!$directory) {
$directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
}

$this->configExport[$configName] = array('data' => $config, 'optional' => $optionalConfig);
if (empty($configNames) && isset($configNameArg)) {
$configNames = array($configNameArg);
}

if ($input->getOption('include-dependencies')) {
// Include config dependencies in export files
if ($dependencies = $this->fetchDependencies($config, 'config')) {
$this->resolveDependencies($dependencies, $optionalConfig);
foreach ($configNames as $configName) {
$config = $this->getConfiguration($configName, $removeUuid, $removeHash);
$config = $this->getConfiguration($configName, false);

if ($config) {
$this->configExport[$configName] = array('data' => $config, 'optional' => $optionalConfig);

if ($input->getOption('include-dependencies')) {
// Include config dependencies in export files
if ($dependencies = $this->fetchDependencies($config, 'config')) {
$this->resolveDependencies($dependencies, $optionalConfig);
}
}
} else {
$io->error($this->trans('commands.config.export.single.messages.config-not-found'));
}
} else {
$io->error($this->trans('commands.config.export.single.messages.config-not-found'));
}

if (!$module) {
Expand Down
104 changes: 72 additions & 32 deletions src/Command/Config/ImportSingleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
*/
namespace Drupal\Console\Command\Config;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Console\Command\Command;
use Drupal\Core\Config\CachedStorage;
use Drupal\Core\Config\ConfigManager;
use Drupal\config\StorageReplaceDataWrapper;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Style\DrupalStyle;
use Drupal\Core\Config\CachedStorage;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\ConfigImporterException;
use Drupal\Core\Config\ConfigManager;
use Drupal\Core\Config\StorageComparer;
use Drupal\config\StorageReplaceDataWrapper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Parser;

class ImportSingleCommand extends Command
{
Expand Down Expand Up @@ -53,12 +54,22 @@ protected function configure()
->setName('config:import:single')
->setDescription($this->trans('commands.config.import.single.description'))
->addArgument(
'name', InputArgument::REQUIRED,
$this->trans('commands.config.import.single.arguments.name')
'name', InputArgument::OPTIONAL,
$this->trans('commands.config.import.single.arguments.name')
)
->addArgument(
'file', InputArgument::REQUIRED,
$this->trans('commands.config.import.single.arguments.file')
'file', InputArgument::OPTIONAL,
$this->trans('commands.config.import.single.arguments.file')
)
->addOption(
'config-names', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.config.import.single.arguments.config-names')
)
->addOption(
'directory',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.config.import.arguments.directory')
);
}

Expand All @@ -69,39 +80,66 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$configName = $input->getArgument('name');
$configNames = $input->getOption('config-names');
$directory = $input->getOption('directory');

$configNameArg = $input->getArgument('name');
$fileName = $input->getArgument('file');

$ymlFile = new Parser();
$singleMode = FALSE;
if (empty($configNames) && isset($configNameArg)) {
$singleMode = TRUE;
$configNames = array($configNameArg);
}

if (!empty($fileName) && file_exists($fileName)) {
$value = $ymlFile->parse(file_get_contents($fileName));
} else {
$value = $ymlFile->parse(stream_get_contents(fopen("php://stdin", "r")));
if (!isset($fileName) && !$directory) {
$directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
}

$ymlFile = new Parser();
try {
$source_storage = new StorageReplaceDataWrapper(
$this->configStorage
);

if (empty($value)) {
$io->error($this->trans('commands.config.import.single.messages.empty-value'));
foreach ($configNames as $configName) {
// Allow for accidental .yml extension.
if (substr($configName, -4) === '.yml') {
$configName = substr($configName, 0, -4);
}

return;
}
if ($singleMode === FALSE) {
$fileName = $directory.DIRECTORY_SEPARATOR.$configName.'.yml';
}

$value = null;
if (!empty($fileName) && file_exists($fileName)) {
$value = $ymlFile->parse(file_get_contents($fileName));
}

if (empty($value)) {
$io->error($this->trans('commands.config.import.single.messages.empty-value'));

return;
}

$source_storage->replaceData($configName, $value);
}

try {
$source_storage = new StorageReplaceDataWrapper($this->configStorage);
$source_storage->replaceData($configName, $value);
$storage_comparer = new StorageComparer(
$source_storage,
$this->configStorage,
$this->configManager
$source_storage,
$this->configStorage,
$this->configManager
);

if ($this->configImport($io, $storage_comparer)) {
$io->success(
sprintf(
$this->trans('commands.config.import.single.messages.success'),
$configName
)
sprintf(
$this->trans(
'commands.config.import.single.messages.success'
),
implode(", ", $configNames)
)
);
}
} catch (\Exception $e) {
Expand Down Expand Up @@ -138,6 +176,8 @@ private function configImport($io, StorageComparer $storage_comparer)
$config_importer->doSyncStep($step, $context);
} while ($context['finished'] < 1);
}

return TRUE;
}
} catch (ConfigImporterException $e) {
$message = 'The import failed due for the following reasons:' . "\n";
Expand Down

0 comments on commit 259019c

Please sign in to comment.