You are here

public function ConfigDownloadController::downloadExport in Demonstration site (Sandbox / Snapshot) 8

Downloads a tarball of the site configuration.

1 string reference to 'ConfigDownloadController::downloadExport'
demo.routing.yml in ./demo.routing.yml
demo.routing.yml

File

src/Controller/ConfigDownloadController.php, line 94

Class

ConfigDownloadController
Returns responses for config module routes.

Namespace

Drupal\demo\Controller

Code

public function downloadExport() {
  $fileconfig = 'private://' . \Drupal::config('demo.settings')
    ->get('demo_dump_path', 'demo');
  $request = \Drupal::request();
  $date_string = date("Y-m-d-H-i");
  $hostname = str_replace('.', '-', $request
    ->getHttpHost());
  $filename = '/config' . '-' . $hostname . '-' . $date_string . '.tar.gz';
  $archiver = new ArchiveTar($fileconfig . $filename, '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([
    'file' => $filename,
  ]);
  if (!\Drupal::service('file_system')
    ->prepareDirectory($fileconfig, FileSystemInterface::CREATE_DIRECTORY)) {
    return FALSE;
  }

  // ----------------------------------------------------------------------.
  $scheme = 'private';
  $target = $request->query
    ->get('file');

  // Merge remaining path arguments into relative file path.
  $uri = $scheme . '://demo' . $target;
  if (file_stream_wrapper_valid_scheme($scheme)) {

    // Let other modules provide headers and controls access to the file.
    $headers = $this
      ->moduleHandler()
      ->invoke('demo', 'file_download', [
      $uri,
    ]);
    foreach ($headers as $result) {
      if ($result == -1) {
        throw new AccessDeniedHttpException();
      }
    }
    if (count($headers)) {

      // \Drupal\Core\EventSubscriber\FinishResponseSubscriber::onRespond()
      // sets response as not cacheable if the Cache-Control header is not
      // already modified. We pass in FALSE for non-private schemes for the
      // $public parameter to make sure we don't change the headers.
      \Drupal::messenger()
        ->addMessage(t('Snapshot has been created.'));
      return $this
        ->redirect('demo.manage_config');
    }
    throw new AccessDeniedHttpException();
  }
  throw new NotFoundHttpException();

  // ----------------------------------------------------------------------.
}