function FileDownloader::download
@inheritDoc
Overrides DownloaderInterface::download
3 calls to FileDownloader::download()
- FileDownloader::getLocalChanges in vendor/
composer/ composer/ src/ Composer/ Downloader/ FileDownloader.php - @inheritDoc
- ZipDownloader::download in vendor/
composer/ composer/ src/ Composer/ Downloader/ ZipDownloader.php - @inheritDoc
- ZipDownloader::download in vendor/
composer/ composer/ src/ Composer/ Downloader/ ZipDownloader.php - @inheritDoc
2 methods override FileDownloader::download()
- PathDownloader::download in vendor/
composer/ composer/ src/ Composer/ Downloader/ PathDownloader.php - @inheritDoc
- ZipDownloader::download in vendor/
composer/ composer/ src/ Composer/ Downloader/ ZipDownloader.php - @inheritDoc
File
-
vendor/
composer/ composer/ src/ Composer/ Downloader/ FileDownloader.php, line 122
Class
- FileDownloader
- Base downloader for files
Namespace
Composer\DownloaderCode
public function download(PackageInterface $package, string $path, ?PackageInterface $prevPackage = null, bool $output = true) : PromiseInterface {
if (null === $package->getDistUrl()) {
throw new \InvalidArgumentException('The given package is missing url information');
}
$cacheKeyGenerator = static function (PackageInterface $package, $key) : string {
$cacheKey = hash('sha1', $key);
return $package->getName() . '/' . $cacheKey . '.' . $package->getDistType();
};
$retries = 3;
$distUrls = $package->getDistUrls();
/** @var array<array{base: non-empty-string, processed: non-empty-string, cacheKey: string}> $urls */
$urls = [];
foreach ($distUrls as $index => $url) {
$processedUrl = $this->processUrl($package, $url);
$urls[$index] = [
'base' => $url,
'processed' => $processedUrl,
// we use the complete download url here to avoid conflicting entries
// from different packages, which would potentially allow a given package
// in a third party repo to pre-populate the cache for the same package in
// packagist for example.
'cacheKey' => $cacheKeyGenerator($package, $processedUrl),
];
}
assert(count($urls) > 0);
$fileName = $this->getFileName($package, $path);
$this->filesystem
->ensureDirectoryExists($path);
$this->filesystem
->ensureDirectoryExists(dirname($fileName));
$accept = null;
$reject = null;
$download = function () use ($output, $cacheKeyGenerator, $package, $fileName, &$urls, &$accept, &$reject) {
$url = reset($urls);
$index = key($urls);
if ($this->eventDispatcher !== null) {
$preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $url['processed'], 'package', $package);
$this->eventDispatcher
->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent);
if ($preFileDownloadEvent->getCustomCacheKey() !== null) {
$url['cacheKey'] = $cacheKeyGenerator($package, $preFileDownloadEvent->getCustomCacheKey());
}
elseif ($preFileDownloadEvent->getProcessedUrl() !== $url['processed']) {
$url['cacheKey'] = $cacheKeyGenerator($package, $preFileDownloadEvent->getProcessedUrl());
}
$url['processed'] = $preFileDownloadEvent->getProcessedUrl();
}
$urls[$index] = $url;
$checksum = $package->getDistSha1Checksum();
$cacheKey = $url['cacheKey'];
// use from cache if it is present and has a valid checksum or we have no checksum to check against
if ($this->cache !== null && ($checksum === null || $checksum === '' || $checksum === $this->cache
->sha1($cacheKey)) && $this->cache
->copyTo($cacheKey, $fileName)) {
if ($output) {
$this->io
->writeError(" - Loading <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>) from cache", true, IOInterface::VERY_VERBOSE);
}
// mark the file as having been written in cache even though it is only read from cache, so that if
// the cache is corrupt the archive will be deleted and the next attempt will re-download it
// see https://github.com/composer/composer/issues/10028
if (!$this->cache
->isReadOnly()) {
$this->lastCacheWrites[$package->getName()] = $cacheKey;
}
$result = \React\Promise\resolve($fileName);
}
else {
if ($output) {
$this->io
->writeError(" - Downloading <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)");
}
$result = $this->httpDownloader
->addCopy($url['processed'], $fileName, $package->getTransportOptions())
->then($accept, $reject);
}
return $result->then(function ($result) use ($fileName, $checksum, $url, $package) : string {
// in case of retry, the first call's Promise chain finally calls this twice at the end,
// once with $result being the returned $fileName from $accept, and then once for every
// failed request with a null result, which can be skipped.
if (null === $result) {
return $fileName;
}
if (!file_exists($fileName)) {
throw new \UnexpectedValueException($url['base'] . ' could not be saved to ' . $fileName . ', make sure the' . ' directory is writable and you have internet connectivity');
}
if ($checksum !== null && $checksum !== '' && hash_file('sha1', $fileName) !== $checksum) {
throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from ' . $url['base'] . ')');
}
if ($this->eventDispatcher !== null) {
$postFileDownloadEvent = new PostFileDownloadEvent(PluginEvents::POST_FILE_DOWNLOAD, $fileName, $checksum, $url['processed'], 'package', $package);
$this->eventDispatcher
->dispatch($postFileDownloadEvent->getName(), $postFileDownloadEvent);
}
return $fileName;
});
};
$accept = function (Response $response) use ($package, $fileName, &$urls) : string {
$url = reset($urls);
$cacheKey = $url['cacheKey'];
$fileSize = @filesize($fileName);
if (false === $fileSize) {
$fileSize = $response->getHeader('Content-Length') ?? '?';
}
FileDownloader::$downloadMetadata[$package->getName()] = $fileSize;
if (Platform::getEnv('GITHUB_ACTIONS') !== false && Platform::getEnv('COMPOSER_TESTS_ARE_RUNNING') === false) {
FileDownloader::$responseHeaders[$package->getName()] = $response->getHeaders();
}
if ($this->cache !== null && !$this->cache
->isReadOnly()) {
$this->lastCacheWrites[$package->getName()] = $cacheKey;
$this->cache
->copyFrom($cacheKey, $fileName);
}
$response->collect();
return $fileName;
};
$reject = function ($e) use (&$urls, $download, $fileName, $package, &$retries) {
// clean up
if (file_exists($fileName)) {
$this->filesystem
->unlink($fileName);
}
$this->clearLastCacheWrite($package);
if ($e instanceof IrrecoverableDownloadException) {
throw $e;
}
if ($e instanceof MaxFileSizeExceededException) {
throw $e;
}
if ($e instanceof TransportException) {
// if we got an http response with a proper code, then requesting again will probably not help, abort
if (0 !== $e->getCode() && !in_array($e->getCode(), [
500,
502,
503,
504,
], true)) {
$retries = 0;
}
}
// special error code returned when network is being artificially disabled
if ($e instanceof TransportException && $e->getStatusCode() === 499) {
$retries = 0;
$urls = [];
}
if ($retries > 0) {
usleep(500000);
$retries--;
return $download();
}
array_shift($urls);
if (\count($urls) > 0) {
if ($this->io
->isDebug()) {
$this->io
->writeError(' Failed downloading ' . $package->getName() . ': [' . get_class($e) . '] ' . $e->getCode() . ': ' . $e->getMessage());
$this->io
->writeError(' Trying the next URL for ' . $package->getName());
}
else {
$this->io
->writeError(' Failed downloading ' . $package->getName() . ', trying the next URL (' . $e->getCode() . ': ' . $e->getMessage() . ')');
}
$retries = 3;
usleep(100000);
return $download();
}
throw $e;
};
return $download();
}