From a7e5521957e1c20daa2771c4157ce49b77ef1a45 Mon Sep 17 00:00:00 2001 From: Bevan Wishart Date: Sun, 3 Mar 2024 23:18:38 +1100 Subject: [PATCH] Create DrushCisCommands.php First go at a drush command to import a single configuration file. --- src/Commands/DrushCisCommands.php | 106 ++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/Commands/DrushCisCommands.php diff --git a/src/Commands/DrushCisCommands.php b/src/Commands/DrushCisCommands.php new file mode 100644 index 0000000000..17b4fc7505 --- /dev/null +++ b/src/Commands/DrushCisCommands.php @@ -0,0 +1,106 @@ + ['default']]) { + $configStorage = \Drupal::service('config.storage'); + $sourceStorage = new StorageReplaceDataWrapper($configStorage); + $names = []; + + foreach ($options['file'] as $configFile) { + if (!file_exists($configFile)) { + $this->logger()->error(dt('Error : config file does not exist') . " : '$configFile'"); + return 1; + } + $name = Path::getFilenameWithoutExtension($configFile); + $ymlFile = new Parser(); + $value = $ymlFile->parse(file_get_contents($configFile)); + $sourceStorage->replaceData($name, $value); + $names[] = $name; + } + + $storageComparer = new StorageComparer( + $sourceStorage, + $configStorage, + \Drupal::service('config.manager') + ); + + $configImporter = new ConfigImporter( + $storageComparer, + \Drupal::service('event_dispatcher'), + \Drupal::service('config.manager'), + \Drupal::lock(), + \Drupal::service('config.typed'), + \Drupal::moduleHandler(), + \Drupal::service('module_installer'), + \Drupal::service('theme_handler'), + \Drupal::service('string_translation'), + \Drupal::service('extension.list.module') + ); + + if ($configImporter->alreadyImporting()) { + $this->logger()->warning(dt('Already importing.')); + return 0; + } + + try { + if ($configImporter->validate()) { + $sync_steps = $configImporter->initialize(); + foreach ($sync_steps as $step) { + $context = []; + do { + $configImporter->doSyncStep($step, $context); + } + while ($context['finished'] < 1); + } + } + } + catch (ConfigImporterException $e) { + $feedback = "Error: unable to import specified config file(s)." + . PHP_EOL + . strip_tags(implode(PHP_EOL, $configImporter->getErrors())) + . PHP_EOL; + $this->logger()->error($feedback); + return 2; + } + catch (\Exception $e) { + $this->logger()->error($e->getMessage()); + return 3; + } + + $this->logger()->success( + dt('Config file(s) successfully imported. Config names imported :') + . " " + . join(', ', $names) + ); + } + +}