You are here

protected function PackageDownloader::extractArchive in Ludwig 8

Extracts a downloaded archive file.

Parameters

string $file: The filename of the archive.

Return value

\Drupal\Core\Archiver\ArchiverInterface The used archiver.

Throws

\Exception

1 call to PackageDownloader::extractArchive()
PackageDownloader::download in src/PackageDownloader.php
Downloads and places packages into their modules.

File

src/PackageDownloader.php, line 168

Class

PackageDownloader
Download packages defined in ludwig.json files.

Namespace

Drupal\ludwig

Code

protected function extractArchive($file) {

  /** @var \Drupal\Core\Archiver\ArchiverInterface $archiver */
  $archiver = $this->archiverManager
    ->getInstance([
    'filepath' => $this->fileSystem
      ->realpath($file),
  ]);
  if (!$archiver) {
    throw new \Exception(sprintf('Cannot extract %file, not a valid archive.', [
      '%file' => $file,
    ]));
  }

  // Unfortunately, we can only use the directory name
  // to determine the package name. Some archivers
  // list the first file as the directory (i.e., MODULE/)
  // and others list an actual file (i.e., MODULE/README.TXT).
  $files = $archiver
    ->listContents();
  $package = strtok($files[0], '/\\');

  // Remove the directory if it exists, otherwise it might contain
  // a mixture of old files mixed with the new files (e.g. in cases
  // where files were removed from a later release).
  $extract_location = $this->extractionDir . '/' . $package;
  if (file_exists($extract_location)) {
    $this->fileSystem
      ->deleteRecursive($extract_location);
  }
  return $archiver
    ->extract($this->extractionDir);
}