1 | <?php |
||
16 | class ParallelDownloader |
||
17 | { |
||
18 | /** @var IO/IOInterface */ |
||
19 | protected $io; |
||
20 | |||
21 | /** @var CConfig */ |
||
22 | protected $config; |
||
23 | |||
24 | /** @var int */ |
||
25 | protected $totalCnt = 0; |
||
26 | protected $successCnt = 0; |
||
27 | protected $skippedCnt = 0; |
||
28 | protected $failureCnt = 0; |
||
29 | |||
30 | 4 | public function __construct(IO\IOInterface $io, CConfig $config) |
|
35 | |||
36 | /** |
||
37 | * @param Package\PackageInterface[] $packages |
||
38 | * @param array $pluginConfig |
||
39 | * @return void |
||
40 | */ |
||
41 | 3 | public function download(array $packages, array $pluginConfig) |
|
42 | { |
||
43 | 3 | $mh = curl_multi_init(); |
|
44 | 3 | $unused = array(); |
|
45 | 3 | for ($i = 0; $i < $pluginConfig['maxConnections']; ++$i) { |
|
46 | 3 | $unused[] = curl_init(); |
|
47 | 3 | } |
|
48 | |||
49 | 3 | $this->setupShareHandler($mh, $unused, $pluginConfig); |
|
50 | |||
51 | 3 | $using = array(); //memory pool |
|
52 | 3 | $running = $remains = 0; |
|
53 | 3 | $this->totalCnt = count($packages); |
|
54 | 3 | $this->successCnt = $this->skippedCnt = $this->failureCnt = 0; |
|
55 | 3 | $this->io->write(" Prefetch start: <comment>total: $this->totalCnt</comment>"); |
|
56 | |||
57 | 3 | $targets = $this->filterPackages($packages, $pluginConfig); |
|
58 | EVENTLOOP: |
||
59 | // prepare curl resources |
||
60 | 3 | while (count($unused) > 0 && count($targets) > 0) { |
|
61 | 2 | $target = array_pop($targets); |
|
62 | 2 | $ch = array_pop($unused); |
|
63 | |||
64 | 2 | $using[(int)$ch] = $target; |
|
65 | 2 | $onPreDownload = Factory::getPreEvent($target['src']); |
|
66 | 2 | $onPreDownload->notify(); |
|
67 | |||
68 | 2 | $opts = $target['src']->getCurlOpts(); |
|
69 | // ParallelDownloader doesn't support private packages. |
||
70 | 2 | unset($opts[CURLOPT_ENCODING], $opts[CURLOPT_USERPWD]); |
|
71 | 2 | curl_setopt_array($ch, $opts); |
|
72 | 2 | curl_setopt($ch, CURLOPT_FILE, $target['dest']->getPointer()); |
|
73 | 2 | curl_multi_add_handle($mh, $ch); |
|
74 | 2 | } |
|
75 | |||
76 | // wait for any event |
||
77 | do { |
||
78 | 3 | $runningBefore = $running; |
|
79 | 3 | while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running)); |
|
80 | |||
81 | SELECT: |
||
82 | 3 | if (-1 === curl_multi_select($mh, 5)) { |
|
83 | 2 | usleep(200 * 1000); |
|
84 | 2 | continue; |
|
85 | } |
||
86 | |||
87 | 2 | while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running)); |
|
88 | 2 | if ($running > 0 && $running === $runningBefore) { |
|
89 | goto SELECT; |
||
90 | } |
||
91 | |||
92 | do { |
||
93 | 2 | if ($raised = curl_multi_info_read($mh, $remains)) { |
|
94 | 2 | $ch = $raised['handle']; |
|
95 | 2 | $errno = curl_errno($ch); |
|
96 | 2 | $info = curl_getinfo($ch); |
|
97 | 2 | curl_setopt($ch, CURLOPT_FILE, STDOUT); |
|
98 | 2 | $index = (int)$ch; |
|
99 | 2 | $target = $using[$index]; |
|
100 | 2 | unset($using[$index]); |
|
101 | 2 | if (CURLE_OK === $errno && 200 === $info['http_code']) { |
|
102 | 1 | ++$this->successCnt; |
|
103 | 1 | $target['dest']->setSuccess(); |
|
104 | 1 | } else { |
|
105 | 1 | ++$this->failureCnt; |
|
106 | } |
||
107 | 2 | unset($target); |
|
108 | 2 | $this->io->write($this->makeDownloadingText($info['url'])); |
|
109 | 2 | curl_multi_remove_handle($mh, $ch); |
|
110 | 2 | $unused[] = $ch; |
|
111 | 2 | } |
|
112 | 2 | } while ($remains > 0); |
|
113 | |||
114 | 2 | if (count($targets) > 0) { |
|
115 | goto EVENTLOOP; |
||
116 | } |
||
117 | 3 | } while ($running > 0); |
|
118 | |||
119 | 3 | $this->io->write(" Finished: <comment>success: $this->successCnt, skipped: $this->skippedCnt, failure: $this->failureCnt, total: $this->totalCnt</comment>"); |
|
120 | |||
121 | 3 | foreach ($unused as $ch) { |
|
122 | 3 | curl_close($ch); |
|
123 | 3 | } |
|
124 | 3 | curl_multi_close($mh); |
|
125 | 3 | } |
|
126 | |||
127 | /** |
||
128 | * @codeCoverageIgnore |
||
129 | */ |
||
130 | private function setupShareHandler($mh, array $unused, array $pluginConfig) |
||
147 | |||
148 | /** |
||
149 | * @param Package\PackageInterface[] $packages |
||
150 | * @param string[] $pluginConfig |
||
151 | * @return [{src: Aspects\HttpGetRequest, dest: OutputFile}] |
||
|
|||
152 | */ |
||
153 | 3 | private function filterPackages(array $packages, array $pluginConfig) |
|
188 | |||
189 | /** |
||
190 | * @param string $url |
||
191 | * @return string |
||
192 | */ |
||
193 | 2 | private function makeDownloadingText($url) |
|
199 | } |
||
200 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.