5 * Contains \Drupal\Console\Command\Config\ImportSingleCommand.
7 namespace Drupal\Console\Command\Config;
9 use Drupal\config\StorageReplaceDataWrapper;
10 use Drupal\Core\Config\CachedStorage;
11 use Drupal\Core\Config\ConfigImporter;
12 use Drupal\Core\Config\ConfigImporterException;
13 use Drupal\Core\Config\ConfigManager;
14 use Drupal\Core\Config\StorageComparer;
15 use Drupal\Console\Core\Command\Command;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Input\InputOption;
18 use Symfony\Component\Console\Output\OutputInterface;
19 use Symfony\Component\Yaml\Parser;
20 use Webmozart\PathUtil\Path;
22 class ImportSingleCommand extends Command
27 protected $configStorage;
32 protected $configManager;
35 * ImportSingleCommand constructor.
37 * @param CachedStorage $configStorage
38 * @param ConfigManager $configManager
40 public function __construct(
41 CachedStorage $configStorage,
42 ConfigManager $configManager
44 $this->configStorage = $configStorage;
45 $this->configManager = $configManager;
46 parent::__construct();
52 protected function configure()
55 ->setName('config:import:single')
56 ->setDescription($this->trans('commands.config.import.single.description'))
60 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
61 $this->trans('commands.config.import.single.options.file')
65 InputOption::VALUE_OPTIONAL,
66 $this->trans('commands.config.import.arguments.directory')
68 ->setAliases(['cis']);
74 protected function execute(InputInterface $input, OutputInterface $output)
76 $file = $input->getOption('file');
77 $directory = $input->getOption('directory');
80 $this->getIo()->error($this->trans('commands.config.import.single..message.missing-file'));
86 $directory = Path::canonicalize($directory);
91 $source_storage = new StorageReplaceDataWrapper(
95 foreach ($file as $fileItem) {
96 $configFile = $fileItem;
98 $configFile = Path::canonicalize($directory) . '/' . $fileItem;
101 if (file_exists($configFile)) {
102 $name = Path::getFilenameWithoutExtension($configFile);
103 $ymlFile = new Parser();
104 $value = $ymlFile->parse(file_get_contents($configFile));
105 $source_storage->delete($name);
106 $source_storage->write($name, $value);
111 $this->getIo()->error($this->trans('commands.config.import.single.messages.empty-value'));
115 $storageComparer = new StorageComparer(
117 $this->configStorage,
121 if ($this->configImport($storageComparer)) {
122 $this->getIo()->success(
125 'commands.config.import.single.messages.success'
131 } catch (\Exception $e) {
132 $this->getIo()->error($e->getMessage());
138 private function configImport(StorageComparer $storageComparer)
140 $configImporter = new ConfigImporter(
142 \Drupal::service('event_dispatcher'),
143 \Drupal::service('config.manager'),
145 \Drupal::service('config.typed'),
146 \Drupal::moduleHandler(),
147 \Drupal::service('module_installer'),
148 \Drupal::service('theme_handler'),
149 \Drupal::service('string_translation')
152 if ($configImporter->alreadyImporting()) {
153 $this->getIo()->success($this->trans('commands.config.import.messages.already-imported'));
156 if ($configImporter->validate()) {
157 $sync_steps = $configImporter->initialize();
159 foreach ($sync_steps as $step) {
162 $configImporter->doSyncStep($step, $context);
163 } while ($context['finished'] < 1);
168 } catch (ConfigImporterException $e) {
169 $message = $this->trans('commands.config.import.messages.import-fail') . "\n";
170 $message .= implode("\n", $configImporter->getErrors());
171 $this->getIo()->error(
173 $this->trans('commands.site.import.local.messages.error-writing'),
177 } catch (\Exception $e) {
178 $this->getIo()->error(
180 $this->trans('commands.site.import.local.messages.error-writing'),
191 protected function interact(InputInterface $input, OutputInterface $output)
193 $file = $input->getOption('file');
194 $directory = $input->getOption('directory');
197 $file = $this->getIo()->ask(
198 $this->trans('commands.config.import.single.questions.file')
200 $input->setOption('file', [$file]);
202 if (!$directory && !Path::isAbsolute($file)) {
203 $directory = $this->getIo()->ask(
204 $this->trans('commands.config.import.single.questions.directory')
207 $input->setOption('directory', $directory);