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

Breadcrumb

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

function PharArchiver::archive

@inheritDoc

Overrides ArchiverInterface::archive

File

vendor/composer/composer/src/Composer/Package/Archiver/PharArchiver.php, line 39

Class

PharArchiver
@author Till Klampaeckel <till@php.net> @author Nils Adermann <naderman@naderman.de> @author Matthieu Moquet <matthieu@moquet.net>

Namespace

Composer\Package\Archiver

Code

public function archive(string $sources, string $target, string $format, array $excludes = [], bool $ignoreFilters = false) : string {
    $sources = realpath($sources);
    // Phar would otherwise load the file which we don't want
    if (file_exists($target)) {
        unlink($target);
    }
    try {
        $filename = substr($target, 0, strrpos($target, $format) - 1);
        // Check if compress format
        if (isset(static::$compressFormats[$format])) {
            // Current compress format supported base on tar
            $target = $filename . '.tar';
        }
        $phar = new \PharData($target, \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO, '', static::$formats[$format]);
        $files = new ArchivableFilesFinder($sources, $excludes, $ignoreFilters);
        $filesOnly = new ArchivableFilesFilter($files);
        $phar->buildFromIterator($filesOnly, $sources);
        $filesOnly->addEmptyDir($phar, $sources);
        if (isset(static::$compressFormats[$format])) {
            // Check can be compressed?
            if (!$phar->canCompress(static::$compressFormats[$format])) {
                throw new \RuntimeException(sprintf('Can not compress to %s format', $format));
            }
            // Delete old tar
            unlink($target);
            // Compress the new tar
            $phar->compress(static::$compressFormats[$format]);
            // Make the correct filename
            $target = $filename . '.' . $format;
        }
        return $target;
    } catch (\UnexpectedValueException $e) {
        $message = sprintf("Could not create archive '%s' from '%s': %s", $target, $sources, $e->getMessage());
        throw new \RuntimeException($message, $e->getCode(), $e);
    }
}
RSS feed
Powered by Drupal