| 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 as CConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 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 | 5 |  |     public function __construct(IO\IOInterface $io, CConfig $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 5 |  |         $this->io = $io; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 5 |  |         $this->config = $config; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 | 5 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 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: 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 | 3 |  |                 usleep(200 * 1000); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |                 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($packages) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |                 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if (function_exists('curl_share_init')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $sh = curl_share_init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             foreach ($unused as $ch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 curl_setopt($ch, CURLOPT_SHARE, $sh); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if (function_exists('curl_multi_setopt')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             if ($pluginConfig['pipeline']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 curl_multi_setopt($mh, CURLMOPT_PIPELINING, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 3 |  |         $cachedir = rtrim($this->config->get('cache-files-dir'), '\/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 3 |  |         $zips = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 3 |  |         foreach ($packages as $p) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 3 |  |             $filepath = $cachedir . DIRECTORY_SEPARATOR . static::getCacheKey($p); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 3 |  |             if (file_exists($filepath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 ++$this->skippedCnt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 3 |  |             $url = $p->getDistUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 3 |  |             if (!$url) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |                 ++$this->skippedCnt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 1 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 2 |  |             if ($p->getDistMirrors()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 $url = current($p->getDistUrls()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 2 |  |             $host = parse_url($url, PHP_URL_HOST) ?: ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 2 |  |             $src = Factory::getHttpGetRequest($host, $url, $this->io, $this->config, $pluginConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 2 |  |             if (in_array($p->getName(), $pluginConfig['privatePackages'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 $src->maybePublic = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 2 |  |                 $src->maybePublic = (bool)preg_match('%^(?:https|git)://github\.com%', $p->getSourceUrl()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             // make file resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 2 |  |             $dest = new OutputFile($filepath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 2 |  |             $zips[] = compact('src', 'dest'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 3 |  |         return $zips; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |      * @param string $url | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 2 |  |     private function makeDownloadingText($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 2 |  |         $request = new Aspects\HttpGetRequest('example.com', $url, $this->io); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 2 |  |         $request->query = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 2 |  |         return "    <comment>$this->successCnt/$this->totalCnt</comment>:    {$request->getURL()}"; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 4 |  |     public static function getCacheKey(Package\PackageInterface $p) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 4 |  |         $distRef = $p->getDistReference(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 | 4 |  |         if (preg_match('{^[a-f0-9]{40}$}', $distRef)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 3 |  |             return "{$p->getName()}/$distRef.{$p->getDistType()}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 2 |  |         return "{$p->getName()}/{$p->getVersion()}-$distRef.{$p->getDistType()}"; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 206 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 207 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 208 |  |  |  | 
            
                        
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.