/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $messageHelper = $this->getHelperSet()->get('message'); $directory = $input->getArgument('directory'); if (!$directory) { $config = $this->getConfigFactory()->get('system.file'); $directory = $config->get('path.temporary') ?: file_directory_temp(); $directory .= '/' . CONFIG_STAGING_DIRECTORY; } if (!is_dir($directory)) { mkdir($directory, 0777, true); } $config_export_file = $directory . '/config.tar.gz'; file_unmanaged_delete($config_export_file); try { $archiver = new ArchiveTar($config_export_file, 'gz'); $this->configManager = $this->getConfigManager(); // Get raw configuration data without overrides. foreach ($this->configManager->getConfigFactory()->listAll() as $name) { $archiver->addString("{$name}.yml", Yaml::encode($this->configManager->getConfigFactory()->get($name)->getRawData())); } $this->targetStorage = $this->getConfigStorage(); // Get all override data from the remaining collections. foreach ($this->targetStorage->getAllCollectionNames() as $collection) { $collection_storage = $this->targetStorage->createCollection($collection); foreach ($collection_storage->listAll() as $name) { $archiver->addString(str_replace('.', '/', $collection) . "/{$name}.yml", Yaml::encode($collection_storage->read($name))); } } } catch (\Exception $e) { $output->writeln('[+] <error>' . $e->getMessage() . '</error>'); return; } $messageHelper->addSuccessMessage(sprintf($this->trans('commands.config.export.messages.directory'), $config_export_file)); }
/** * Writes a file to the file system, creating its directory as needed. * * @param string $directory * The extension's directory. * @param array $file * Array with the following keys: * - 'filename': the name of the file. * - 'subdirectory': any subdirectory of the file within the extension * directory. * - 'string': the contents of the file. * @param ArchiveTar $archiver * The archiver. * * @throws Exception */ protected function generateFile($directory, array $file, ArchiveTar $archiver) { $filename = $directory; if (!empty($file['subdirectory'])) { $filename .= '/' . $file['subdirectory']; } $filename .= '/' . $file['filename']; // Set the mode to 0644 rather than the default of 0600. if ($archiver->addString($filename, $file['string'], FALSE, ['mode' => 0644]) === FALSE) { throw new \Exception($this->t('Failed to archive file @filename.', ['@filename' => $file['filename']])); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $directory = $input->getOption('directory'); $tar = $input->getOption('tar'); $removeUuid = $input->getOption('remove-uuid'); if (!$directory) { $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY); } if ($tar) { if (!is_dir($directory)) { mkdir($directory, 0777, true); } $dateTime = new \DateTime(); $archiveFile = sprintf('%s/config-%s.tar.gz', $directory, $dateTime->format('Y-m-d-H-i-s')); $archiveTar = new ArchiveTar($archiveFile, 'gz'); } try { // Get raw configuration data without overrides. foreach ($this->configManager->getConfigFactory()->listAll() as $name) { $configData = $this->configManager->getConfigFactory()->get($name)->getRawData(); $configName = sprintf('%s.yml', $name); // The _core is site-specific, so don't export it. unset($configData['_core']); if ($removeUuid) { unset($configData['uuid']); } $ymlData = Yaml::encode($configData); if ($tar) { $archiveTar->addString($configName, $ymlData); continue; } $configFileName = sprintf('%s/%s', $directory, $configName); $fileSystem = new Filesystem(); try { $fileSystem->mkdir($directory); } catch (IOExceptionInterface $e) { $io->error(sprintf($this->trans('commands.config.export.messages.error'), $e->getPath())); } file_put_contents($configFileName, $ymlData); } } catch (\Exception $e) { $io->error($e->getMessage()); } $io->info(sprintf($this->trans('commands.config.export.messages.directory'), $directory)); }
/** * Downloads a tarball of the site configuration. */ public function downloadExport() { file_unmanaged_delete(file_directory_temp() . '/config.tar.gz'); $archiver = new ArchiveTar(file_directory_temp() . '/config.tar.gz', 'gz'); // Get raw configuration data without overrides. foreach ($this->configManager->getConfigFactory()->listAll() as $name) { $archiver->addString("{$name}.yml", Yaml::encode($this->configManager->getConfigFactory()->get($name)->getRawData())); } // Get all override data from the remaining collections. foreach ($this->targetStorage->getAllCollectionNames() as $collection) { $collection_storage = $this->targetStorage->createCollection($collection); foreach ($collection_storage->listAll() as $name) { $archiver->addString(str_replace('.', '/', $collection) . "/{$name}.yml", Yaml::encode($collection_storage->read($name))); } } $request = new Request(array('file' => 'config.tar.gz')); return $this->fileDownloadController->download($request, 'temporary'); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $messageHelper = $this->getMessageHelper(); $directory = $input->getOption('directory'); $tar = $input->getOption('tar'); $archiveTar = new ArchiveTar(); if (!$directory) { $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY); } if ($tar) { if (!is_dir($directory)) { mkdir($directory, 0777, true); } $dateTime = new \DateTime(); $archiveFile = sprintf('%s/config-%s.tar.gz', $directory, $dateTime->format('Y-m-d-H-i-s')); $archiveTar = new ArchiveTar($archiveFile, 'gz'); } try { $configManager = $this->getConfigManager(); // Get raw configuration data without overrides. foreach ($configManager->getConfigFactory()->listAll() as $name) { $configData = $configManager->getConfigFactory()->get($name)->getRawData(); $configName = sprintf('%s.yml', $name); $ymlData = Yaml::encode($configData); if ($tar) { $archiveTar->addString($configName, $ymlData); continue; } $configFileName = sprintf('%s/%s', $directory, $configName); file_put_contents($configFileName, $ymlData); } } catch (\Exception $e) { $output->writeln('[+] <error>' . $e->getMessage() . '</error>'); return; } $messageHelper->addSuccessMessage(sprintf($this->trans('commands.config.export.messages.directory'))); $messageHelper->addSuccessMessage($directory); }
/** * Writes a file to the file system, creating its directory as needed. * * @param string $directory * The extension's directory. * @param array $file * Array with the following keys: * - 'filename': the name of the file. * - 'subdirectory': any subdirectory of the file within the extension * directory. * - 'string': the contents of the file. * @param ArchiveTar $archiver * The archiver. * * @throws Exception */ protected function generateFile($directory, array $file, ArchiveTar $archiver) { $filename = $directory; if (!empty($file['subdirectory'])) { $filename .= '/' . $file['subdirectory']; } $filename .= '/' . $file['filename']; if ($archiver->addString($filename, $file['string']) === FALSE) { throw new \Exception($this->t('Failed to archive file @filename.', ['@filename' => $file['filename']])); } }