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) |
|
31 | { |
||
32 | 4 | $this->io = $io; |
|
33 | 4 | $this->config = $config; |
|
34 | 4 | } |
|
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 | $multi = new CurlMulti($pluginConfig['maxConnections']); |
|
44 | 3 | $multi->setupShareHandler($pluginConfig['pipeline']); |
|
45 | |||
46 | 3 | $this->totalCnt = count($packages); |
|
47 | 3 | $this->successCnt = $this->skippedCnt = $this->failureCnt = 0; |
|
48 | 3 | $this->io->write(" Prefetch start: <comment>total: $this->totalCnt</comment>"); |
|
49 | |||
50 | 3 | $multi->setTargets($this->filterPackages($packages, $pluginConfig)); |
|
51 | |||
52 | do { |
||
53 | 3 | $multi->setupEventLoop(); |
|
54 | 3 | $multi->wait(); |
|
55 | |||
56 | 3 | $result = $multi->getFinishedResults(); |
|
57 | 3 | $this->successCnt += $result['successCnt']; |
|
58 | 3 | $this->failureCnt += $result['failureCnt']; |
|
59 | 3 | foreach ($result['results'] as $url) { |
|
60 | 1 | $this->io->write($this->makeDownloadingText($url)); |
|
61 | 3 | } |
|
62 | 3 | } while ($multi->remain()); |
|
63 | |||
64 | 3 | $this->io->write( |
|
65 | 3 | " Finished: <comment>success:$this->successCnt," |
|
66 | 3 | . " skipped:$this->skippedCnt, failure:$this->failureCnt," |
|
67 | 3 | . " total: $this->totalCnt</comment>" |
|
68 | 3 | ); |
|
69 | 3 | } |
|
70 | |||
71 | /** |
||
72 | * @param Package\PackageInterface[] $packages |
||
73 | * @param string[] $pluginConfig |
||
74 | * @return array [{src: Aspects\HttpGetRequest, dest: OutputFile}] |
||
75 | */ |
||
76 | 3 | private function filterPackages(array $packages, array $pluginConfig) |
|
103 | |||
104 | 3 | private function getUrlFromPackage(Package\PackageInterface $package) |
|
121 | |||
122 | /** |
||
123 | * @param string $url |
||
124 | * @return string |
||
125 | */ |
||
126 | 1 | private function makeDownloadingText($url) |
|
132 | } |
||
133 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: