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 Drupal\Console\Core\Command\Command;
18 use Symfony\Component\Filesystem\Filesystem;
19 use Drupal\Core\Config\ConfigManager;
21 class ExportCommand extends Command
26 protected $configManager;
29 * @var StorageInterface
34 * ExportCommand constructor.
36 * @param ConfigManagerInterface $configManager
37 * @param StorageInterface $storage
39 public function __construct(ConfigManagerInterface $configManager, StorageInterface $storage)
41 parent::__construct();
42 $this->configManager = $configManager;
43 $this->storage = $storage;
49 protected function configure()
52 ->setName('config:export')
53 ->setDescription($this->trans('commands.config.export.description'))
57 InputOption::VALUE_OPTIONAL,
58 $this->trans('commands.config.export.options.directory')
63 InputOption::VALUE_NONE,
64 $this->trans('commands.config.export.options.tar')
68 InputOption::VALUE_NONE,
69 $this->trans('commands.config.export.options.remove-uuid')
73 InputOption::VALUE_NONE,
74 $this->trans('commands.config.export.options.remove-config-hash')
82 protected function execute(InputInterface $input, OutputInterface $output)
84 $directory = $input->getOption('directory');
85 $tar = $input->getOption('tar');
86 $removeUuid = $input->getOption('remove-uuid');
87 $removeHash = $input->getOption('remove-config-hash');
90 $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
93 $fileSystem = new Filesystem();
95 $fileSystem->mkdir($directory);
96 } catch (IOExceptionInterface $e) {
97 $this->getIo()->error(
99 $this->trans('commands.config.export.messages.error'),
105 // Remove previous yaml files before creating new ones
106 array_map('unlink', glob($directory . '/*'));
109 $dateTime = new \DateTime();
111 $archiveFile = sprintf(
112 '%s/config-%s.tar.gz',
114 $dateTime->format('Y-m-d-H-i-s')
116 $archiveTar = new ArchiveTar($archiveFile, 'gz');
120 // Get raw configuration data without overrides.
121 foreach ($this->configManager->getConfigFactory()->listAll() as $name) {
122 $configName = "$name.yml";
123 $configData = $this->configManager->getConfigFactory()->get($name)->getRawData();
125 unset($configData['uuid']);
128 unset($configData['_core']['default_config_hash']);
129 if (empty($configData['_core'])) {
130 unset($configData['_core']);
133 $ymlData = Yaml::encode($configData);
136 $archiveTar->addString($configName, $ymlData);
138 file_put_contents("$directory/$configName", $ymlData);
141 // Get all override data from the remaining collections.
142 foreach ($this->storage->getAllCollectionNames() as $collection) {
143 $collection_storage = $this->storage->createCollection($collection);
144 $collection_path = str_replace('.', '/', $collection);
146 mkdir("$directory/$collection_path", 0755, true);
148 foreach ($collection_storage->listAll() as $name) {
149 $configName = "$collection_path/$name.yml";
150 $configData = $collection_storage->read($name);
152 unset($configData['uuid']);
155 unset($configData['_core']['default_config_hash']);
156 if (empty($configData['_core'])) {
157 unset($configData['_core']);
161 $ymlData = Yaml::encode($configData);
163 $archiveTar->addString($configName, $ymlData);
165 file_put_contents("$directory/$configName", $ymlData);
169 } catch (\Exception $e) {
170 $this->getIo()->error($e->getMessage());
173 $this->getIo()->info(
175 $this->trans('commands.config.export.messages.directory'),