Skip to content

Commit

Permalink
[config:import:single] Remove name option. Fix #3223. (#3369)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas authored Jul 2, 2017
1 parent 0bc0abb commit 0e22bcb
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions src/Command/Config/ImportSingleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use Drupal\Core\Config\ConfigManager;
use Drupal\Core\Config\StorageComparer;
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;
use Webmozart\PathUtil\Path;

class ImportSingleCommand extends Command
{
Expand Down Expand Up @@ -59,14 +59,9 @@ protected function configure()
->setName('config:import:single')
->setDescription($this->trans('commands.config.import.single.description'))
->addOption(
'name',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.config.import.single.options.name')
)->addOption(
'file',
null,
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.config.import.single.options.file')
)->addOption(
'directory',
Expand All @@ -83,35 +78,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$name = $input->getOption('name');
$name = is_array($name) ? $name : [$name];
$directory = $input->getOption('directory');
$file = $input->getOption('file');
$directory = $input->getOption('directory');

if (!$file) {
$io->error('File option is missing.');

return 1;
}

if (!$file && !$directory) {
$directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
if ($directory) {
$directory = Path::canonicalize($directory);
}

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

foreach ($name as $nameItem) {
// Allow for accidental .yml extension.
if (substr($nameItem, -4) === '.yml') {
$nameItem = substr($nameItem, 0, -4);
foreach ($file as $fileItem) {
$configFile = $fileItem;
if ($directory) {
$configFile = Path::canonicalize($directory) . '/' . $fileItem;
}

$configFile = count($name) == 1 ?
$file :
$directory.DIRECTORY_SEPARATOR.$nameItem.'.yml';

if (file_exists($configFile)) {
$name = Path::getFilenameWithoutExtension($configFile);
$ymlFile = new Parser();
$value = $ymlFile->parse(file_get_contents($configFile));
$source_storage->delete($nameItem);
$source_storage->write($nameItem, $value);
$source_storage->delete($name);
$source_storage->write($name, $value);
$names[] = $name;
continue;
}

Expand All @@ -125,14 +123,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->configManager
);


if ($this->configImport($io, $storageComparer)) {
$io->success(
sprintf(
$this->trans(
'commands.config.import.single.messages.success'
),
implode(", ", $name)
implode(',', $names)
)
);
}
Expand Down Expand Up @@ -199,30 +196,22 @@ private function configImport($io, StorageComparer $storageComparer)
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$name = $input->getOption('name');
$file = $input->getOption('file');
$directory = $input->getOption('directory');

if (!$name) {
$name = $io->ask(
$this->trans('commands.config.import.single.questions.name')
);
$input->setOption('name', $name);
}

if (!$directory && !$file) {
$file = $io->askEmpty(
if (!$file) {
$file = $io->ask(
$this->trans('commands.config.import.single.questions.file')
);
$input->setOption('file', $file);
}
$input->setOption('file', [$file]);

if (!$directory && !Path::isAbsolute($file)) {
$directory = $io->ask(
$this->trans('commands.config.import.single.questions.directory')
);

if (!$file && !$directory) {
$directory = $io->askEmpty(
$this->trans('commands.config.import.single.questions.directory')
);
$input->setOption('directory', $directory);
$input->setOption('directory', $directory);
}
}
}
}

0 comments on commit 0e22bcb

Please sign in to comment.