Completed
Push — master ( eb9b51...1f6bdc )
by Hiraku
02:15
created

ParallelDownloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/*
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo;
8
9
use Composer\Package;
10
use Composer\IO;
11
use Composer\Config;
12
13
/**
14
 *
15
 */
16
class ParallelDownloader
17
{
18
    /** @var IO/IOInterface */
19
    protected $io;
20
21
    /** @var Config */
22
    protected $config;
23
24
    /** @var int */
25
    protected $totalCnt = 0;
26
    protected $successCnt = 0;
27
    protected $failureCnt = 0;
28
29 1
    public function __construct(IO\IOInterface $io, Config $config)
30
    {
31 1
        $this->io = $io;
32 1
        $this->config = $config;
33 1
    }
34
35
    /**
36
     * @param Package\PackageInterface[] $packages
37
     * @param array $pluginConfig
38
     * @return void
39
     */
40
    public function download(array $packages, array $pluginConfig)
41
    {
42
        $mh = curl_multi_init();
43
        $unused = array();
44
        $maxConns = $pluginConfig['maxConnections'];
45
        for ($i = 0; $i < $maxConns; ++$i) {
46
            $unused[] = curl_init();
47
        }
48
49
        // @codeCoverageIgnoreStart
50
        if (function_exists('curl_share_init')) {
51
            $sh = curl_share_init();
52
            curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
53
54
            foreach ($unused as $ch) {
55
                curl_setopt($ch, CURLOPT_SHARE, $sh);
56
            }
57
        }
58
59
        if (function_exists('curl_multi_setopt')) {
60
            if ($pluginConfig['pipeline']) {
61
                curl_multi_setopt($mh, CURLMOPT_PIPELINING, true);
62
            }
63
        }
64
        // @codeCoverageIgnoreEnd
65
66
        $cachedir = rtrim($this->config->get('cache-files-dir'), '\/');
67
68
        $chFpMap = array();
69
        $running = 0; //ref type
70
        $remains = 0; //ref type
71
72
        $this->totalCnt = count($packages);
73
        $this->successCnt = 0;
74
        $this->failureCnt = 0;
75
        $this->io->write("    Prefetch start: <comment>success: $this->successCnt, failure: $this->failureCnt, total: $this->totalCnt</comment>");
76
        do {
77
            // prepare curl resources
78
            while (count($unused) > 0 && count($packages) > 0) {
79
                $package = array_pop($packages);
80
                $filepath = $cachedir . DIRECTORY_SEPARATOR . static::getCacheKey($package);
81
                if (file_exists($filepath)) {
82
                    ++$this->successCnt;
83
                    continue;
84
                }
85
                $ch = array_pop($unused);
86
87
                // make file resource
88
                $chFpMap[(int)$ch] = $outputFile = new OutputFile($filepath);
89
90
                // make url
91
                $url = $package->getDistUrl();
92
                $host = parse_url($url, PHP_URL_HOST) ?: '';
93
                $request = new Aspects\HttpGetRequest($host, $url, $this->io);
94
                $request->verbose = $pluginConfig['verbose'];
95
                if (in_array($package->getName(), $pluginConfig['privatePackages'])) {
96
                    $request->maybePublic = false;
97
                } else {
98
                    $request->maybePublic = (bool)preg_match('%^(?:https|git)://github\.com%', $package->getSourceUrl());
99
                }
100
                $onPreDownload = Factory::getPreEvent($request);
101
                $onPreDownload->notify();
102
103
                $opts = $request->getCurlOpts();
104
                if ($pluginConfig['insecure']) {
105
                    $opts[CURLOPT_SSL_VERIFYPEER] = false;
106
                }
107
                if (! empty($pluginConfig['capath'])) {
108
                    $opts[CURLOPT_CAPATH] = $pluginConfig['capath'];
109
                }
110
                unset($opts[CURLOPT_ENCODING]);
111
                unset($opts[CURLOPT_USERPWD]); // ParallelDownloader doesn't support private packages.
112
                curl_setopt_array($ch, $opts);
113
                curl_setopt($ch, CURLOPT_FILE, $outputFile->getPointer());
114
                curl_multi_add_handle($mh, $ch);
115
            }
116
117
            // wait for any event
118
            do {
119
                // start multi download
120
                while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running));
121
                $runningBefore = $running;
122
123
                $eventCount = curl_multi_select($mh, 5);
124
125
                if ($eventCount === -1) {
126
                    usleep(200 * 1000);
127
                    continue;
128
                }
129
130
                if ($eventCount === 0) {
131
                    continue;
132
                }
133
134
                while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running));
135
136
                if ($running === $runningBefore) {
137
                    continue;
138
                }
139
140
                do {
141
                    if ($raised = curl_multi_info_read($mh, $remains)) {
142
                        $ch = $raised['handle'];
143
                        $errno = curl_errno($ch);
144
                        $info = curl_getinfo($ch);
145
                        curl_setopt($ch, CURLOPT_FILE, STDOUT);
146
                        $index = (int)$ch;
147
                        $outputFile = $chFpMap[$index];
148
                        unset($chFpMap[$index]);
149
                        if (CURLE_OK === $errno && 200 === $info['http_code']) {
150
                            ++$this->successCnt;
151
                        } else {
152
                            ++$this->failureCnt;
153
                            $outputFile->setFailure();
154
                        }
155
                        unset($outputFile);
156
                        $this->io->write($this->makeDownloadingText($info['url']));
157
                        curl_multi_remove_handle($mh, $ch);
158
                        $unused[] = $ch;
159
                    }
160
                } while ($remains > 0);
161
162
                if (count($packages) > 0) {
163
                    break;
164
                }
165
            } while ($running);
166
        } while (count($packages) > 0);
167
        $this->io->write("    Finished: <comment>success: $this->successCnt, failure: $this->failureCnt, total: $this->totalCnt</comment>");
168
169
        foreach ($unused as $ch) {
170
            curl_close($ch);
171
        }
172
        curl_multi_close($mh);
173
    }
174
175
    /**
176
     * @param string $url
177
     * @return string
178
     */
179
    private function makeDownloadingText($url)
180
    {
181
        $request = new Aspects\HttpGetRequest('example.com', $url, $this->io);
182
        $request->query = array();
183
        return "    <comment>$this->successCnt/$this->totalCnt</comment>:    {$request->getURL()}";
184
    }
185
186
    public static function getCacheKey(Package\PackageInterface $p)
187
    {
188
        $distRef = $p->getDistReference();
189
        if (preg_match('{^[a-f0-9]{40}$}', $distRef)) {
190
            return "{$p->getName()}/$distRef.{$p->getDistType()}";
191
        }
192
193
        return "{$p->getName()}/{$p->getVersion()}-$distRef.{$p->getDistType()}";
194
    }
195
}
196