<commit_before><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Package\Archiver;

use Composer\Downloader\DownloadManager;
use Composer\Factory;
use Composer\IO\NullIO;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ArchiveManager
{
    protected $buildDir;

    protected $downloadManager;

    protected $archivers = array();

    /**
     * @param string          $buildDir        The directory used to build the archive
     * @param DownloadManager $downloadManager A manager used to download package sources
     */
    public function __construct($buildDir, DownloadManager $downloadManager = null)
    {
        $this->buildDir = $buildDir;

        if (null !== $downloadManager) {
            $this->downloadManager = $downloadManager;
        } else {
            $factory = new Factory();
            $this->downloadManager = $factory->createDownloadManager(new NullIO());
        }
    }

    /**
     * @param ArchiverInterface $archiver
     */
    public function addArchiver(ArchiverInterface $archiver)
    {
        $this->archivers[] = $archiver;
    }

    /**
     * Create an archive of the specified package.
     *
     * @param PackageInterface $package The package to archive
     * @param string           $format  The format of the archive (zip, tar, ...)
     *
     * @return string The path of the created archive
     */
    public function archive(PackageInterface $package, $format)
    {
        if (empty($format)) {
            throw new \InvalidArgumentException('Format must be specified');
        }

        $filesystem = new Filesystem();
        $packageName = str_replace('/', DIRECTORY_SEPARATOR, $package->getUniqueName());

        // Directory used to download the sources
        $sources = sys_get_temp_dir().DIRECTORY_SEPARATOR.$packageName;
        $filesystem->ensureDirectoryExists($sources);

        // Archive filename
        $target = $this->buildDir.DIRECTORY_SEPARATOR.$packageName.'.'.$format;
        $filesystem->ensureDirectoryExists(dirname($this->buildDir.$target));

        // Download sources
        $this->downloadManager->download($package, $sources, true);

        $sourceType = $package->getSourceType();
        $sourceRef  = $package->getSourceReference();
        foreach ($this->archivers as $archiver) {
            if ($archiver->supports($format, $sourceType)) {
                $archiver->archive($sources, $target, $format, $sourceRef);

                return $target;
            }
        }

        throw new \RuntimeException(sprintf('No archiver found to support %s format', $format));
    }
}<commit_msg>Checks support before downloading the package<commit_after><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Package\Archiver;

use Composer\Downloader\DownloadManager;
use Composer\Factory;
use Composer\IO\NullIO;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ArchiveManager
{
    protected $buildDir;

    protected $downloadManager;

    protected $archivers = array();

    /**
     * @param string          $buildDir        The directory used to build the archive
     * @param DownloadManager $downloadManager A manager used to download package sources
     */
    public function __construct($buildDir, DownloadManager $downloadManager = null)
    {
        $this->buildDir = $buildDir;

        if (null !== $downloadManager) {
            $this->downloadManager = $downloadManager;
        } else {
            $factory = new Factory();
            $this->downloadManager = $factory->createDownloadManager(new NullIO());
        }
    }

    /**
     * @param ArchiverInterface $archiver
     */
    public function addArchiver(ArchiverInterface $archiver)
    {
        $this->archivers[] = $archiver;
    }

    /**
     * Create an archive of the specified package.
     *
     * @param PackageInterface $package The package to archive
     * @param string           $format  The format of the archive (zip, tar, ...)
     *
     * @return string The path of the created archive
     */
    public function archive(PackageInterface $package, $format)
    {
        if (empty($format)) {
            throw new \InvalidArgumentException('Format must be specified');
        }

        $usableArchiver = null;
        $sourceType = $package->getSourceType();

        foreach ($this->archivers as $archiver) {
            if ($archiver->supports($format, $package->getSourceType())) {
                $usableArchiver = $archiver;
            }
        }

        // 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();
        $packageName = str_replace('/', DIRECTORY_SEPARATOR, $package->getUniqueName());

        // Directory used to download the sources
        $sources = sys_get_temp_dir().DIRECTORY_SEPARATOR.$packageName;
        $filesystem->ensureDirectoryExists($sources);

        // Archive filename
        $target = $this->buildDir.DIRECTORY_SEPARATOR.$packageName.'.'.$format;
        $filesystem->ensureDirectoryExists(dirname($this->buildDir.$target));

        // Download sources
        $this->downloadManager->download($package, $sources, true);

        $sourceRef = $package->getSourceReference();
        $usableArchiver->archive($sources, $target, $format, $sourceRef);
    }
}
{'fraction_non_alphanumeric': 0.11911495591415738, 'fraction_numerical': 0.0, 'mean_word_length': 2.672571777642028, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2133889', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1569, 'n_words': 430}