class ConfigExportCommand
Export configuration.
@internal
Attributes
#[AsCommand(name: 'config:export', description: 'Export configuration.')]
Hierarchy
- class \Drupal\Core\Config\Command\ConfigExportCommand uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\Config\StorageCopyTrait extends \Symfony\Component\Console\Command\Command
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\CommandView source
final class ConfigExportCommand extends Command {
use StringTranslationTrait;
use StorageCopyTrait;
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;
}
/**
* Prints the change list of a storage comparer as a table.
*
* @param \Drupal\Core\Config\StorageComparer $comparer
* The comparer holding the change list to render.
* @param \Symfony\Component\Console\Style\SymfonyStyle $io
* The input and output style.
*/
protected function renderTable(StorageComparer $comparer, SymfonyStyle $io) : void {
$translations = [
'delete' => (string) $this->t('Delete'),
'update' => (string) $this->t('Update'),
'create' => (string) $this->t('Create'),
'rename' => (string) $this->t('Rename'),
];
$styles = [
'delete' => 'fg=white;bg=red',
'update' => 'fg=black;bg=yellow',
'create' => 'fg=white;bg=green',
'rename' => 'fg=black;bg=cyan',
];
$header = [
(string) $this->t('Collection'),
(string) $this->t('Config'),
(string) $this->t('Operation'),
];
$rows = [];
foreach ($comparer->getAllCollectionNames() as $collection) {
foreach ($comparer->getChangelist(NULL, $collection) as $op => $names) {
foreach ($names as $name) {
$rows[] = [
$collection,
$op !== 'rename' ? $name : str_replace('::', ' -> ', $name),
\sprintf('<%s>%s</>', $styles[$op], $translations[$op]),
];
}
}
}
$io->table($header, $rows);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.