4 * Contains \Drupal\Console\Command\Config\ImportCommand.
7 namespace Drupal\Console\Command\Config;
9 use Symfony\Component\Console\Input\InputOption;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Command\Command;
13 use Drupal\Core\Config\CachedStorage;
14 use Drupal\Core\Config\ConfigManager;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Core\Config\ConfigImporterException;
18 use Drupal\Core\Config\ConfigImporter;
19 use Drupal\Core\Config\FileStorage;
20 use Drupal\Core\Config\StorageComparer;
22 class ImportCommand extends Command
29 protected $configStorage;
34 protected $configManager;
37 * ImportCommand constructor.
39 * @param CachedStorage $configStorage
40 * @param ConfigManager $configManager
42 public function __construct(
43 CachedStorage $configStorage,
44 ConfigManager $configManager
46 $this->configStorage = $configStorage;
47 $this->configManager = $configManager;
48 parent::__construct();
54 protected function configure()
57 ->setName('config:import')
58 ->setDescription($this->trans('commands.config.import.description'))
62 InputOption::VALUE_OPTIONAL,
63 $this->trans('commands.config.import.options.file')
68 InputOption::VALUE_OPTIONAL,
69 $this->trans('commands.config.import.options.directory')
74 InputOption::VALUE_NONE,
75 $this->trans('commands.config.import.options.remove-files')
82 protected function execute(InputInterface $input, OutputInterface $output)
84 $io = new DrupalStyle($input, $output);
85 $directory = $input->getOption('directory');
88 $configSyncDir = $directory;
90 $configSyncDir = config_get_config_directory(
95 $source_storage = new FileStorage($configSyncDir);
97 $storage_comparer = new StorageComparer($source_storage, $this->configStorage, $this->configManager);
99 if (!$storage_comparer->createChangelist()->hasChanges()) {
100 $io->success($this->trans('commands.config.import.messages.nothing-to-do'));
103 if ($this->configImport($io, $storage_comparer)) {
104 $io->success($this->trans('commands.config.import.messages.imported'));
111 private function configImport(DrupalStyle $io, StorageComparer $storage_comparer)
113 $config_importer = new ConfigImporter(
115 \Drupal::service('event_dispatcher'),
116 \Drupal::service('config.manager'),
118 \Drupal::service('config.typed'),
119 \Drupal::moduleHandler(),
120 \Drupal::service('module_installer'),
121 \Drupal::service('theme_handler'),
122 \Drupal::service('string_translation')
125 if ($config_importer->alreadyImporting()) {
126 $io->success($this->trans('commands.config.import.messages.already-imported'));
129 $io->info($this->trans('commands.config.import.messages.importing'));
130 $config_importer->import();
132 } catch (ConfigImporterException $e) {
133 $message = 'The import failed due to the following reasons:' . "\n";
134 $message .= implode("\n", $config_importer->getErrors());
137 $this->trans('commands.site.import.local.messages.error-writing'),
141 } catch (\Exception $e) {
144 $this->trans('commands.site.import.local.messages.error-writing'),