function HttpDownloader::addJob
@phpstan-param Request $request
@phpstan-return array{Job, PromiseInterface<Http\Response>}
Return value
array{Job, PromiseInterface}
4 calls to HttpDownloader::addJob()
- HttpDownloader::add in vendor/
composer/ composer/ src/ Composer/ Util/ HttpDownloader.php - Create an async download operation
- HttpDownloader::addCopy in vendor/
composer/ composer/ src/ Composer/ Util/ HttpDownloader.php - Create an async copy operation
- HttpDownloader::copy in vendor/
composer/ composer/ src/ Composer/ Util/ HttpDownloader.php - Copy a file synchronously
- HttpDownloader::get in vendor/
composer/ composer/ src/ Composer/ Util/ HttpDownloader.php - Download a file synchronously
File
-
vendor/
composer/ composer/ src/ Composer/ Util/ HttpDownloader.php, line 209
Class
- HttpDownloader
- @author Jordi Boggiano <j.boggiano@seld.be> @phpstan-type Request array{url: non-empty-string, options: mixed[], copyTo: string|null} @phpstan-type Job array{id: int, status: int, request: Request, sync: bool, origin: string, resolve?: callable,…
Namespace
Composer\UtilCode
private function addJob(array $request, bool $sync = false) : array {
$request['options'] = array_replace_recursive($this->options, $request['options']);
/** @var Job */
$job = [
'id' => $this->idGen++,
'status' => self::STATUS_QUEUED,
'request' => $request,
'sync' => $sync,
'origin' => Url::getOrigin($this->config, $request['url']),
];
if (!$sync && !$this->allowAsync) {
throw new \LogicException('You must use the HttpDownloader instance which is part of a Composer\\Loop instance to be able to run async http requests');
}
// capture username/password from URL if there is one
if (Preg::isMatchStrictGroups('{^https?://([^:/]+):([^@/]+)@([^/]+)}i', $request['url'], $match)) {
$this->io
->setAuthentication($job['origin'], rawurldecode($match[1]), rawurldecode($match[2]));
}
$rfs = $this->rfs;
if ($this->canUseCurl($job)) {
$resolver = static function ($resolve, $reject) use (&$job) : void {
$job['status'] = HttpDownloader::STATUS_QUEUED;
$job['resolve'] = $resolve;
$job['reject'] = $reject;
};
}
else {
$resolver = static function ($resolve, $reject) use (&$job, $rfs) : void {
// start job
$url = $job['request']['url'];
$options = $job['request']['options'];
$job['status'] = HttpDownloader::STATUS_STARTED;
if ($job['request']['copyTo']) {
$rfs->copy($job['origin'], $url, $job['request']['copyTo'], false, $options);
$headers = $rfs->getLastHeaders();
$response = new Http\Response($job['request'], $rfs->findStatusCode($headers), $headers, $job['request']['copyTo'] . '~');
$resolve($response);
}
else {
$body = $rfs->getContents($job['origin'], $url, false, $options);
$headers = $rfs->getLastHeaders();
$response = new Http\Response($job['request'], $rfs->findStatusCode($headers), $headers, $body);
$resolve($response);
}
};
}
$curl = $this->curl;
$canceler = static function () use (&$job, $curl) : void {
if ($job['status'] === HttpDownloader::STATUS_QUEUED) {
$job['status'] = HttpDownloader::STATUS_ABORTED;
}
if ($job['status'] !== HttpDownloader::STATUS_STARTED) {
return;
}
$job['status'] = HttpDownloader::STATUS_ABORTED;
if (isset($job['curl_id'])) {
$curl->abortRequest($job['curl_id']);
}
throw new IrrecoverableDownloadException('Download of ' . Url::sanitize($job['request']['url']) . ' canceled');
};
$promise = new Promise($resolver, $canceler);
$promise = $promise->then(function ($response) use (&$job) {
$job['status'] = HttpDownloader::STATUS_COMPLETED;
$job['response'] = $response;
$this->markJobDone();
return $response;
}, function ($e) use (&$job) : void {
$job['status'] = HttpDownloader::STATUS_FAILED;
$job['exception'] = $e;
$this->markJobDone();
throw $e;
});
$this->jobs[$job['id']] =& $job;
if ($this->runningJobs < $this->maxJobs) {
$this->startJob($job['id']);
}
return [
$job,
$promise,
];
}