function DownloadManager::download
Downloads package into target dir.
@phpstan-return PromiseInterface<void|null>
Parameters
PackageInterface $package package instance:
string $targetDir target dir:
PackageInterface|null $prevPackage previous package instance in case of updates:
Throws
\InvalidArgumentException if package have no urls to download from
\RuntimeException
File
-
vendor/
composer/ composer/ src/ Composer/ Downloader/ DownloadManager.php, line 182
Class
- DownloadManager
- Downloaders manager.
Namespace
Composer\DownloaderCode
public function download(PackageInterface $package, string $targetDir, ?PackageInterface $prevPackage = null) : PromiseInterface {
$targetDir = $this->normalizeTargetDir($targetDir);
$this->filesystem
->ensureDirectoryExists(dirname($targetDir));
$sources = $this->getAvailableSources($package, $prevPackage);
$io = $this->io;
$download = function ($retry = false) use (&$sources, $io, $package, $targetDir, &$download, $prevPackage) {
$source = array_shift($sources);
if ($retry) {
$io->writeError(' <warning>Now trying to download from ' . $source . '</warning>');
}
$package->setInstallationSource($source);
$downloader = $this->getDownloaderForPackage($package);
if (!$downloader) {
return \React\Promise\resolve(null);
}
$handleError = static function ($e) use ($sources, $source, $package, $io, $download) {
if ($e instanceof \RuntimeException && !$e instanceof IrrecoverableDownloadException) {
if (!$sources) {
throw $e;
}
$io->writeError(' <warning>Failed to download ' . $package->getPrettyName() . ' from ' . $source . ': ' . $e->getMessage() . '</warning>');
return $download(true);
}
throw $e;
};
try {
$result = $downloader->download($package, $targetDir, $prevPackage);
} catch (\Exception $e) {
return $handleError($e);
}
$res = $result->then(static function ($res) {
return $res;
}, $handleError);
return $res;
};
return $download();
}