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

Breadcrumb

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

class PharArchiver

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

Hierarchy

  • class \Composer\Package\Archiver\PharArchiver implements \Composer\Package\Archiver\ArchiverInterface

Expanded class hierarchy of PharArchiver

File

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

Namespace

Composer\Package\Archiver
View source
class PharArchiver implements ArchiverInterface {
    
    /** @var array<string, int> */
    protected static $formats = [
        'zip' => \Phar::ZIP,
        'tar' => \Phar::TAR,
        'tar.gz' => \Phar::TAR,
        'tar.bz2' => \Phar::TAR,
    ];
    
    /** @var array<string, int> */
    protected static $compressFormats = [
        'tar.gz' => \Phar::GZ,
        'tar.bz2' => \Phar::BZ2,
    ];
    
    /**
     * @inheritDoc
     */
    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);
        }
    }
    
    /**
     * @inheritDoc
     */
    public function supports(string $format, ?string $sourceType) : bool {
        return isset(static::$formats[$format]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
PharArchiver::$compressFormats protected static property @var array&lt;string, int&gt;
PharArchiver::$formats protected static property @var array&lt;string, int&gt;
PharArchiver::archive public function @inheritDoc Overrides ArchiverInterface::archive
PharArchiver::supports public function @inheritDoc Overrides ArchiverInterface::supports

API Navigation

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