From ee702af379b906c5598a4065355d5bfdf27347d5 Mon Sep 17 00:00:00 2001 From: Jesus Manuel Olivas Date: Mon, 19 Dec 2016 09:47:35 -0800 Subject: [PATCH] [config:*:single] Fix config single options (#3028) * [console] Show error on command registration error. * [config:export:single] Multiple fixes and improvements * [config:import:single] Multiple fixes and improvements --- src/Application.php | 3 +- src/Command/Config/ExportSingleCommand.php | 120 +++++++++++---------- src/Command/Config/ImportSingleCommand.php | 107 +++++++++--------- 3 files changed, 116 insertions(+), 114 deletions(-) diff --git a/src/Application.php b/src/Application.php index 7d6eba6f2..89188594b 100644 --- a/src/Application.php +++ b/src/Application.php @@ -148,6 +148,8 @@ private function registerCommands() try { $command = $this->container->get($name); } catch (\Exception $e) { + echo $name . ' - ' . $e->getMessage() . PHP_EOL; + continue; } @@ -189,7 +191,6 @@ public function getData() 'help', 'init', 'list', - // 'self-update', 'server' ]; $languages = $this->container->get('console.configuration_manager') diff --git a/src/Command/Config/ExportSingleCommand.php b/src/Command/Config/ExportSingleCommand.php index 4a63b1d7f..ee628f1d5 100644 --- a/src/Command/Config/ExportSingleCommand.php +++ b/src/Command/Config/ExportSingleCommand.php @@ -63,37 +63,31 @@ protected function configure() $this ->setName('config:export:single') ->setDescription($this->trans('commands.config.export.single.description')) - ->addArgument( - 'config-name', - InputArgument::OPTIONAL, - $this->trans('commands.config.export.single.arguments.config-name') - ) ->addOption( - 'config-names', - null, + 'name', + '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - $this->trans('commands.config.export.single.arguments.config-names') - ) - ->addOption( + $this->trans('commands.config.export.single.options.name') + )->addOption( 'directory', '', InputOption::VALUE_OPTIONAL, $this->trans('commands.config.export.arguments.directory') - ) - ->addOption( + )->addOption( + 'module', + '', + InputOption::VALUE_OPTIONAL, + $this->trans('commands.common.options.module') + )->addOption( 'include-dependencies', '', InputOption::VALUE_NONE, $this->trans('commands.config.export.single.options.include-dependencies') )->addOption( - 'module', '', - InputOption::VALUE_OPTIONAL, - $this->trans('commands.common.options.module') - )->addOption( - 'optional-config', + 'optional', '', - InputOption::VALUE_OPTIONAL, - $this->trans('commands.config.export.single.options.optional-config') + InputOption::VALUE_NONE, + $this->trans('commands.config.export.single.options.optional') )->addOption( 'remove-uuid', '', @@ -136,7 +130,7 @@ function ($definition) { */ protected function getConfigNames($config_type) { - + $names = []; // For a given entity type, load all entities. if ($config_type && $config_type !== 'system.simple') { $entity_storage = $this->entityTypeManager->getStorage($config_type); @@ -179,40 +173,39 @@ protected function interact(InputInterface $input, OutputInterface $output) $config_types = $this->getConfigTypes(); - $config_name = $input->getArgument('config-name'); - if (!$config_name) { - $config_type = $io->choiceNoList( + $name = $input->getOption('name'); + if (!$name) { + $type = $io->choiceNoList( $this->trans('commands.config.export.single.questions.config-type'), array_keys($config_types), - $this->trans('commands.config.export.single.options.simple-configuration') + 'system.simple' ); - $config_names = $this->getConfigNames($config_type); + $names = $this->getConfigNames($type); - $config_name = $io->choiceNoList( - $this->trans('commands.config.export.single.questions.config-name'), - array_keys($config_names) + $name = $io->choiceNoList( + $this->trans('commands.config.export.single.questions.name'), + array_keys($names) ); - if ($config_type !== 'system.simple') { - $definition = $this->entityTypeManager->getDefinition($config_type); - $config_name = $definition->getConfigPrefix() . '.' . $config_name; + if ($type !== 'system.simple') { + $definition = $this->entityTypeManager->getDefinition($type); + $name = $definition->getConfigPrefix() . '.' . $name; } - - $input->setArgument('config-name', $config_name); + $input->setOption('name', $name); } $module = $input->getOption('module'); - if ($module) { - $optionalConfig = $input->getOption('optional-config'); + $optionalConfig = $input->getOption('optional'); if (!$optionalConfig) { $optionalConfig = $io->confirm( - $this->trans('commands.config.export.single.questions.optional-config'), + $this->trans('commands.config.export.single.questions.optional'), true ); - $input->setOption('optional-config', $optionalConfig); + $input->setOption('optional', $optionalConfig); } } + if (!$input->getOption('remove-uuid')) { $removeUuid = $io->confirm( $this->trans('commands.config.export.single.questions.remove-uuid'), @@ -239,27 +232,28 @@ protected function execute(InputInterface $input, OutputInterface $output) $directory = $input->getOption('directory'); $module = $input->getOption('module'); - $configNames = $input->getOption('config-names'); - $configNameArg = $input->getArgument('config-name'); - $optionalConfig = $input->getOption('optional-config'); + $ame = $input->getOption('name'); + $optional = $input->getOption('optional'); $removeUuid = $input->getOption('remove-uuid'); $removeHash = $input->getOption('remove-config-hash'); - - if (empty($configNames) && isset($configNameArg)) { - $configNames = array($configNameArg); - } - foreach ($configNames as $configName) { - $config = $this->getConfiguration($configName, $removeUuid, $removeHash); - $config = $this->getConfiguration($configName, false); + foreach ($ame as $nameItem) { + $config = $this->getConfiguration( + $nameItem, + $removeUuid, + $removeHash + ); if ($config) { - $this->configExport[$configName] = array('data' => $config, 'optional' => $optionalConfig); + $this->configExport[$nameItem] = [ + 'data' => $config, + 'optional' => $optional + ]; if ($input->getOption('include-dependencies')) { // Include config dependencies in export files if ($dependencies = $this->fetchDependencies($config, 'config')) { - $this->resolveDependencies($dependencies, $optionalConfig); + $this->resolveDependencies($dependencies, $optional); } } } else { @@ -267,14 +261,28 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - if (!$module) { - if (!$directory) { - $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY); - } + if ($module) { + $this->exportConfigToModule( + $module, + $io, + $this->trans( + 'commands.config.export.single.messages.config_exported' + ) + ); + + return 0; + } - $this->exportConfig($directory, $io, $this->trans('commands.config.export.single.messages.config_exported')); - } else { - $this->exportConfigToModule($module, $io, $this->trans('commands.config.export.single.messages.config_exported')); + if (!$directory) { + $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY); } + + $this->exportConfig( + $directory, + $io, + $this->trans('commands.config.export.single.messages.config_exported') + ); + + return 0; } } diff --git a/src/Command/Config/ImportSingleCommand.php b/src/Command/Config/ImportSingleCommand.php index 3b0f7e8b4..752f9ef8c 100644 --- a/src/Command/Config/ImportSingleCommand.php +++ b/src/Command/Config/ImportSingleCommand.php @@ -57,19 +57,17 @@ protected function configure() $this ->setName('config:import:single') ->setDescription($this->trans('commands.config.import.single.description')) - ->addArgument( - 'name', InputArgument::OPTIONAL, - $this->trans('commands.config.import.single.arguments.name') - ) - ->addArgument( - '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( + 'name', + null, + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + $this->trans('commands.config.import.single.options.name') + )->addOption( + 'file', + null, + InputOption::VALUE_OPTIONAL, + $this->trans('commands.config.import.single.options.file') + )->addOption( 'directory', '', InputOption::VALUE_OPTIONAL, @@ -84,19 +82,11 @@ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); - $configNames = $input->getOption('config-names'); + $name = $input->getOption('name'); $directory = $input->getOption('directory'); + $file = $input->getOption('file'); - $configNameArg = $input->getArgument('name'); - $fileName = $input->getArgument('file'); - - $singleMode = false; - if (empty($configNames) && isset($configNameArg)) { - $singleMode = true; - $configNames = array($configNameArg); - } - - if (!isset($fileName) && !$directory) { + if (!$file && !$directory) { $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY); } @@ -106,43 +96,36 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->configStorage ); - foreach ($configNames as $configName) { + foreach ($name as $nameItem) { // Allow for accidental .yml extension. - if (substr($configName, -4) === '.yml') { - $configName = substr($configName, 0, -4); - } - - 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 (substr($nameItem, -4) === '.yml') { + $nameItem = substr($nameItem, 0, -4); } - if (empty($value)) { - $io->error($this->trans('commands.config.import.single.messages.empty-value')); - - return; + $configFile = $directory.DIRECTORY_SEPARATOR.$nameItem.'.yml'; + if (file_exists($configFile)) { + $value = $ymlFile->parse(file_get_contents($configFile)); + $source_storage->replaceData($nameItem, $value); + continue; } - $source_storage->replaceData($configName, $value); + $io->error($this->trans('commands.config.import.single.messages.empty-value')); + return 1; } - $storage_comparer = new StorageComparer( + $storageComparer = new StorageComparer( $source_storage, $this->configStorage, $this->configManager ); - if ($this->configImport($io, $storage_comparer)) { + if ($this->configImport($io, $storageComparer)) { $io->success( sprintf( $this->trans( 'commands.config.import.single.messages.success' ), - implode(", ", $configNames) + implode(", ", $name) ) ); } @@ -153,10 +136,10 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - private function configImport($io, StorageComparer $storage_comparer) + private function configImport($io, StorageComparer $storageComparer) { - $config_importer = new ConfigImporter( - $storage_comparer, + $configImporter = new ConfigImporter( + $storageComparer, \Drupal::service('event_dispatcher'), \Drupal::service('config.manager'), \Drupal::lock(), @@ -167,17 +150,17 @@ private function configImport($io, StorageComparer $storage_comparer) \Drupal::service('string_translation') ); - if ($config_importer->alreadyImporting()) { + if ($configImporter->alreadyImporting()) { $io->success($this->trans('commands.config.import.messages.already-imported')); } else { try { - if ($config_importer->validate()) { - $sync_steps = $config_importer->initialize(); + if ($configImporter->validate()) { + $sync_steps = $configImporter->initialize(); foreach ($sync_steps as $step) { $context = array(); do { - $config_importer->doSyncStep($step, $context); + $configImporter->doSyncStep($step, $context); } while ($context['finished'] < 1); } @@ -185,7 +168,7 @@ private function configImport($io, StorageComparer $storage_comparer) } } catch (ConfigImporterException $e) { $message = 'The import failed due for the following reasons:' . "\n"; - $message .= implode("\n", $config_importer->getErrors()); + $message .= implode("\n", $configImporter->getErrors()); $io->error( sprintf( $this->trans('commands.site.import.local.messages.error-writing'), @@ -209,20 +192,30 @@ private function configImport($io, StorageComparer $storage_comparer) protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); - $name = $input->getArgument('name'); + $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->setArgument('name', $name); + $input->setOption('name', $name); } - $file = $input->getArgument('file'); - if (!$file) { - $file = $io->ask( + if (!$directory && !$file) { + $file = $io->askEmpty( $this->trans('commands.config.import.single.questions.file') ); - $input->setArgument('file', $file); + $input->setOption('file', $file); + } + + + if (!$file && !$directory) { + $directory = $io->askEmpty( + $this->trans('commands.config.import.single.questions.directory') + ); + $input->setOption('directory', $directory); } } }