-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPrestissimoFileFetcher.php
99 lines (87 loc) · 2.86 KB
/
PrestissimoFileFetcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace DrupalComposer\DrupalScaffold;
use Composer\Util\RemoteFilesystem;
use Composer\Config;
use Composer\IO\IOInterface;
use Hirak\Prestissimo\CopyRequest;
use Hirak\Prestissimo\CurlMulti;
/**
* Extends the default FileFetcher and uses hirak/prestissimo for parallel
* downloads.
*/
class PrestissimoFileFetcher extends FileFetcher {
/**
* @var \Composer\Config
*/
protected $config;
/**
* Constructs this PrestissimoFileFetcher object.
*/
public function __construct(RemoteFilesystem $remoteFilesystem, IOInterface $io, $progress = TRUE, Config $config) {
parent::__construct($remoteFilesystem, $io, $progress);
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public function fetch($version, $destination, $override) {
if (class_exists(CurlMulti::class)) {
return $this->fetchWithPrestissimo($version, $destination, $override);
}
return parent::fetch($version, $destination, $override);
}
/**
* Fetch files in parallel.
*/
protected function fetchWithPrestissimo($version, $destination, $override) {
$requests = [];
foreach ($this->filenames as $sourceFilename => $filename) {
$target = "$destination/$filename";
if ($override || !file_exists($target)) {
$url = $this->getUri($sourceFilename, $version);
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
$requests[] = new CopyRequest($url, $target, FALSE, $this->io, $this->config);
}
}
$successCnt = $failureCnt = 0;
$errors = [];
$totalCnt = count($requests);
if ($totalCnt == 0) {
return TRUE;
}
$multi = new CurlMulti();
$multi->setRequests($requests);
do {
$multi->setupEventLoop();
$multi->wait();
$result = $multi->getFinishedResults();
$successCnt += $result['successCnt'];
$failureCnt += $result['failureCnt'];
if (isset($result['errors'])) {
$errors += $result['errors'];
foreach ($result['errors'] as $url => $error) {
$this->io->writeError(" - Downloading <comment>$successCnt</comment>/<comment>$totalCnt</comment>: <info>$url</info> (<error>failed</error>)", TRUE);
}
}
if ($this->progress) {
foreach ($result['urls'] as $url) {
$this->io->writeError(" - Downloading <comment>$successCnt</comment>/<comment>$totalCnt</comment>: <info>$url</info>", TRUE);
}
}
} while ($multi->remain());
if ($errors) {
$this->addError('Failed to download: ' . "\r\n" . implode("\r\n", array_keys($errors)));
$errors_extra = [];
foreach($errors as $error) {
if ($error !== "0: " && !isset($errors_extra[$error])) {
$errors_extra[$error] = $error;
}
}
if ($errors_extra) {
$this->addError(implode("\r\n", $errors_extra));
}
return FALSE;
}
return TRUE;
}
}