class ConfigImportCommand

Import configuration.

@internal

Attributes

#[AsCommand(name: 'config:import', description: 'Import configuration', aliases: [ 'cim', ])]

Hierarchy

Expanded class hierarchy of ConfigImportCommand

1 file declares its use of ConfigImportCommand
ConfigImportCommandTest.php in core/tests/Drupal/FunctionalTests/Core/Config/ConfigImportCommandTest.php

File

core/lib/Drupal/Core/Config/Command/ConfigImportCommand.php, line 23

Namespace

Drupal\Core\Config\Command
View source
class ConfigImportCommand extends Command {
  use StringTranslationTrait;
  use ConfigCommandTrait;
  public function __construct(#[Autowire(service: 'config.storage')] private readonly StorageInterface $active, #[Autowire(service: 'config.storage.sync')] private readonly StorageInterface $sync, private readonly ImportStorageTransformer $transformer, private readonly ConfigImporterFactory $importerFactory) {
    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 {
    // Do the import storage transformation.
    $import = $this->transformer
      ->transform($this->sync);
    $comparer = new StorageComparer($import, $this->active);
    $comparer->createChangelist();
    if (!$comparer->hasChanges()) {
      $io->success((string) $this->t('There are no changes to import.'));
      return self::SUCCESS;
    }
    $this->renderTable($comparer, $io);
    if (!$yes && !$io->confirm((string) $this->t('Import the listed configuration changes?'))) {
      throw new UserAbortException('Cancelled');
    }
    $importer = $this->importerFactory
      ->get($comparer);
    if ($importer->alreadyImporting()) {
      $io->warning((string) $this->t('Another request may be synchronizing configuration already.'));
      return Command::FAILURE;
    }
    try {
      $importer->validate();
      $importer->import();
    } catch (ConfigImporterException $e) {
      $io->error($e->getMessage());
      return Command::FAILURE;
    }
    $errors = $importer->getErrors();
    if (empty($errors)) {
      $io->success((string) $this->t('The configuration was imported successfully.'));
      return Command::SUCCESS;
    }
    $io->error((string) $this->t('The configuration was imported with errors.'));
    foreach ($errors as $error) {
      $io->error((string) $error);
    }
    return Command::FAILURE;
  }

}

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