Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ArchiveManager.php

function ArchiveManager::archive

Create an archive of the specified package.

Parameters

CompletePackageInterface $package The package to archive:

string $format The format of the archive (zip, tar, ...):

string $targetDir The directory where to build the archive:

string|null $fileName The relative file name to use for the archive, or null to generate: the package name. Note that the format will be appended to this name

bool $ignoreFilters Ignore filters when looking for files in the package:

Return value

string The path of the created archive

Throws

\InvalidArgumentException

\RuntimeException

File

vendor/composer/composer/src/Composer/Package/Archiver/ArchiveManager.php, line 148

Class

ArchiveManager
@author Matthieu Moquet <matthieu@moquet.net> @author Till Klampaeckel <till@php.net>

Namespace

Composer\Package\Archiver

Code

public function archive(CompletePackageInterface $package, string $format, string $targetDir, ?string $fileName = null, bool $ignoreFilters = false) : string {
    if (empty($format)) {
        throw new \InvalidArgumentException('Format must be specified');
    }
    // Search for the most appropriate archiver
    $usableArchiver = null;
    foreach ($this->archivers as $archiver) {
        if ($archiver->supports($format, $package->getSourceType())) {
            $usableArchiver = $archiver;
            break;
        }
    }
    // Checks the format/source type are supported before downloading the package
    if (null === $usableArchiver) {
        throw new \RuntimeException(sprintf('No archiver found to support %s format', $format));
    }
    $filesystem = new Filesystem();
    if ($package instanceof RootPackageInterface) {
        $sourcePath = realpath('.');
    }
    else {
        // Directory used to download the sources
        $sourcePath = sys_get_temp_dir() . '/composer_archive' . bin2hex(random_bytes(5));
        $filesystem->ensureDirectoryExists($sourcePath);
        try {
            // Download sources
            $promise = $this->downloadManager
                ->download($package, $sourcePath);
            SyncHelper::await($this->loop, $promise);
            $promise = $this->downloadManager
                ->install($package, $sourcePath);
            SyncHelper::await($this->loop, $promise);
        } catch (\Exception $e) {
            $filesystem->removeDirectory($sourcePath);
            throw $e;
        }
        // Check exclude from downloaded composer.json
        if (file_exists($composerJsonPath = $sourcePath . '/composer.json')) {
            $jsonFile = new JsonFile($composerJsonPath);
            $jsonData = $jsonFile->read();
            if (!empty($jsonData['archive']['name'])) {
                $package->setArchiveName($jsonData['archive']['name']);
            }
            if (!empty($jsonData['archive']['exclude'])) {
                $package->setArchiveExcludes($jsonData['archive']['exclude']);
            }
        }
    }
    $supportedFormats = $this->getSupportedFormats();
    $packageNameParts = null === $fileName ? $this->getPackageFilenameParts($package) : [
        'base' => $fileName,
    ];
    $packageName = $this->getPackageFilenameFromParts($packageNameParts);
    $excludePatterns = $this->buildExcludePatterns($packageNameParts, $supportedFormats);
    // Archive filename
    $filesystem->ensureDirectoryExists($targetDir);
    $target = realpath($targetDir) . '/' . $packageName . '.' . $format;
    $filesystem->ensureDirectoryExists(dirname($target));
    if (!$this->overwriteFiles && file_exists($target)) {
        return $target;
    }
    // Create the archive
    $tempTarget = sys_get_temp_dir() . '/composer_archive' . bin2hex(random_bytes(5)) . '.' . $format;
    $filesystem->ensureDirectoryExists(dirname($tempTarget));
    $archivePath = $usableArchiver->archive($sourcePath, $tempTarget, $format, array_merge($excludePatterns, $package->getArchiveExcludes()), $ignoreFilters);
    $filesystem->rename($archivePath, $target);
    // cleanup temporary download
    if (!$package instanceof RootPackageInterface) {
        $filesystem->removeDirectory($sourcePath);
    }
    $filesystem->remove($tempTarget);
    return $target;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal