You are here

function drush_config_import_export in Config Importer and Tools 8

Same name and namespace in other branches
  1. 8.2 config_import.drush.inc \drush_config_import_export()
  2. 8.0 config_import.drush.inc \drush_config_import_export()

Implements drush_COMMAND().

File

./config_import.drush.inc, line 141
Drush integration.

Code

function drush_config_import_export() {
  try {

    // Do not forget:
    // - the "name" option will be prefixed by configuration prefix;
    $options = _drush_config_import_get_options();
    $config = Drupal::service('config.storage')
      ->read($options['name']);
    if (FALSE === $config) {
      throw new RuntimeException(dt('No configuration with "@name" name!', [
        '@name' => $options['name'],
      ]));
    }
    $content = Yaml::encode($config);
    if (NULL === $options['destination']) {
      drush_print($content);
    }
    else {
      $export_confirmed = TRUE;

      // Ask before override the file.
      if (file_exists($options['destination'])) {
        $export_confirmed = drush_confirm(dt('Configuration already exists in the "@destination" file. Do you want to override it?', [
          '@destination' => $options['destination'],
        ]));
      }
      if ($export_confirmed) {
        if (!\Drupal::service('file_system')
          ->saveData($content, $options['destination'], FileSystemInterface::EXISTS_REPLACE)) {
          throw new RuntimeException(dt('Configuration cannot be saved into the "@destination" file! Check system log.', [
            '@destination' => $options['destination'],
          ]));
        }
        $message = dt('Configuration has been saved into the "@destination" file.', [
          '@destination' => $options['destination'],
        ]);
      }
      else {
        $message = dt('Export of the "@name" configuration has been canceled.', [
          '@name' => $options['name'],
        ]);
      }
      drush_log($message, 'status');
    }
  } catch (Exception $e) {
    drush_log($e
      ->getMessage(), 'error');
  }
}