class ConfigExportCommand

Export configuration.

@internal

Attributes

#[AsCommand(name: 'config:export', description: 'Export configuration.')]

Hierarchy

Expanded class hierarchy of ConfigExportCommand

1 file declares its use of ConfigExportCommand
ConfigExportCommandTest.php in core/tests/Drupal/KernelTests/Core/Config/ConfigExportCommandTest.php

File

core/lib/Drupal/Core/Config/Command/ConfigExportCommand.php, line 25

Namespace

Drupal\Core\Config\Command
View source
final class ConfigExportCommand extends Command {
  use StringTranslationTrait;
  use StorageCopyTrait;
  use ConfigCommandTrait;
  public function __construct(#[Autowire(service: 'config.storage.export')] private readonly StorageInterface $export, private readonly SyncFactory $syncFactory) {
    parent::__construct();
  }
  
  /**
   * Export the configuration.
   *
   * @param \Symfony\Component\Console\Style\SymfonyStyle $io
   *   The symfony style.
   * @param bool $yes
   *   The command option to skip the confirmation question.
   *
   * @return int
   *   The command result.
   */
  public function __invoke(SymfonyStyle $io, #[Option(description: 'Answer the confirmation question with yes.', shortcut: 'y')] bool $yes = FALSE) : int {
    // Resolve the sync storage here rather than in the constructor: the
    // factory throws when no sync directory is configured, and a throwing
    // constructor would break `dr list` for every command.
    try {
      $sync = $this->syncFactory
        ->get();
    } catch (ConfigDirectoryNotDefinedException $e) {
      $io->error($e->getMessage());
      return self::FAILURE;
    }
    $comparer = new StorageComparer($this->export, $sync);
    $comparer->createChangelist();
    if (!$comparer->hasChanges()) {
      $io->success((string) $this->t('The active configuration to export matches the configuration in the sync directory.'));
      return self::SUCCESS;
    }
    $this->renderTable($comparer, $io);
    if (!$yes && !$io->confirm((string) $this->t('Do you want to export the configuration?'))) {
      throw new UserAbortException('Cancelled');
    }
    self::replaceStorageContents($this->export, $sync);
    $io->success((string) $this->t('Configuration successfully exported'));
    return self::SUCCESS;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ConfigCommandTrait::renderTable protected function Prints the change list of a storage comparer as a table.
ConfigExportCommand::__construct public function
ConfigExportCommand::__invoke public function Export the configuration.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.