5 * Contains \Drupal\Console\Command\Config\ExportCommand.
8 namespace Drupal\Console\Command\Config;
10 use Drupal\Core\Archiver\ArchiveTar;
11 use Drupal\Component\Serialization\Yaml;
12 use Drupal\Core\Config\ConfigManagerInterface;
13 use Drupal\Core\Config\StorageInterface;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Command\Command;
18 use Symfony\Component\Filesystem\Filesystem;
19 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 use Drupal\Console\Core\Style\DrupalStyle;
21 use Drupal\Core\Config\ConfigManager;
23 class ExportCommand extends Command
30 protected $configManager;
33 * @var StorageInterface
38 * ExportCommand constructor.
40 * @param ConfigManagerInterface $configManager
41 * @param StorageInterface $storage
43 public function __construct(ConfigManagerInterface $configManager, StorageInterface $storage)
45 parent::__construct();
46 $this->configManager = $configManager;
47 $this->storage = $storage;
53 protected function configure()
56 ->setName('config:export')
57 ->setDescription($this->trans('commands.config.export.description'))
61 InputOption::VALUE_OPTIONAL,
62 $this->trans('commands.config.export.arguments.directory')
67 InputOption::VALUE_NONE,
68 $this->trans('commands.config.export.arguments.tar')
72 InputOption::VALUE_NONE,
73 $this->trans('commands.config.export.single.options.remove-uuid')
77 InputOption::VALUE_NONE,
78 $this->trans('commands.config.export.single.options.remove-config-hash')
85 protected function execute(InputInterface $input, OutputInterface $output)
87 $io = new DrupalStyle($input, $output);
89 $directory = $input->getOption('directory');
90 $tar = $input->getOption('tar');
91 $removeUuid = $input->getOption('remove-uuid');
92 $removeHash = $input->getOption('remove-config-hash');
95 $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
98 $fileSystem = new Filesystem();
100 $fileSystem->mkdir($directory);
101 } catch (IOExceptionInterface $e) {
104 $this->trans('commands.config.export.messages.error'),
110 // Remove previous yaml files before creating new ones
111 array_map('unlink', glob($directory . '/*'));
114 $dateTime = new \DateTime();
116 $archiveFile = sprintf(
117 '%s/config-%s.tar.gz',
119 $dateTime->format('Y-m-d-H-i-s')
121 $archiveTar = new ArchiveTar($archiveFile, 'gz');
125 // Get raw configuration data without overrides.
126 foreach ($this->configManager->getConfigFactory()->listAll() as $name) {
127 $configName = "$name.yml";
128 $configData = $this->configManager->getConfigFactory()->get($name)->getRawData();
130 unset($configData['uuid']);
133 unset($configData['_core']['default_config_hash']);
135 $ymlData = Yaml::encode($configData);
138 $archiveTar->addString($configName, $ymlData);
140 file_put_contents("$directory/$configName", $ymlData);
143 // Get all override data from the remaining collections.
144 foreach ($this->storage->getAllCollectionNames() as $collection) {
145 $collection_storage = $this->storage->createCollection($collection);
146 foreach ($collection_storage->listAll() as $name) {
147 $configName = str_replace('.', '/', $collection) . "/$name.yml";
148 $configData = $collection_storage->read($name);
150 unset($configData['uuid']);
153 unset($configData['_core']['default_config_hash']);
156 $ymlData = Yaml::encode($configData);
158 $archiveTar->addString($configName, $ymlData);
160 file_put_contents("$directory/$configName", $ymlData);
164 } catch (\Exception $e) {
165 $io->error($e->getMessage());
170 $this->trans('commands.config.export.messages.directory'),