@@ -40,13 +40,10 @@ public function exec($command, $quiet = false)
4040 : $ this ->execCommand ($ command , $ quiet );
4141 }
4242
43- public function getComposerSettings ($ package )
43+ public function getComposerSettings ($ package, $ platforms = null )
4444 {
4545 if (!isset ($ this ->composerSettings [$ package ])) {
46- $ file = new File ("https://repo.packagist.org/p/ $ package.json " );
47- $ this ->composerSettings [$ package ] = $ file ->isValid ()
48- ? (($ file ['packages ' ] ?? [])[$ package ] ?? null )
49- : $ this ->getFromLibrariesIo ($ package );
46+ $ this ->composerSettings [$ package ] = $ this ->getFromFirstValidPlatform ($ package , $ platforms );
5047 }
5148
5249 return $ this ->composerSettings [$ package ];
@@ -285,10 +282,88 @@ protected function error($message)
285282 new MultiTesterException ($ message );
286283 }
287284
288- private function getFromLibrariesIo ( string $ package ): ?File
285+ private function getSourceFromLibrariesIo ( $ package ): ?array
289286 {
290287 $ file = new File ('https://libraries.io/api/Packagist/ ' . urlencode ($ package ), 'json ' );
291288
292- return $ file ->isValid () ? $ file : null ;
289+ return $ file ->isValid () ? $ file ->toArray () : null ;
290+ }
291+
292+ private function getSourceFromPackagist ($ package , $ namespace = 'p2 ' ): ?array
293+ {
294+ $ file = new File ("https://repo.packagist.org/ $ namespace/ $ package.json " );
295+
296+ return $ file ->isValid ()
297+ ? (($ file ['packages ' ] ?? [])[$ package ] ?? null )
298+ : null ;
299+ }
300+
301+ private function getSourceFromPackagist2 ($ package ): ?array
302+ {
303+ $ list = $ this ->getSourceFromPackagist ($ package );
304+
305+ if (!is_array ($ list )) {
306+ return $ list ;
307+ }
308+
309+ $ listByVersion = [];
310+ $ previousItem = [];
311+
312+ foreach ($ list as $ item ) {
313+ if (isset ($ item ['version ' ])) {
314+ $ previousItem = array_merge ($ previousItem , $ item );
315+ $ listByVersion [$ item ['version ' ]] = $ previousItem ;
316+ }
317+ }
318+
319+ return $ listByVersion ;
320+ }
321+
322+ private function getSourceFromPlatform ($ package , $ platform ): ?array
323+ {
324+ switch ($ platform ) {
325+ case 'composer1 ' :
326+ case 'packagist1 ' :
327+ return $ this ->getSourceFromPackagist ($ package , 'p ' );
328+
329+ case 'composer ' :
330+ case 'packagist ' :
331+ case 'composer2 ' :
332+ case 'packagist2 ' :
333+ case 'packagist.org ' :
334+ return $ this ->getSourceFromPackagist2 ($ package );
335+
336+ case 'libraries ' :
337+ case 'libraries.io ' :
338+ return $ this ->getSourceFromLibrariesIo ($ package );
339+
340+ default :
341+ $ this ->error ("Unknown platform ' $ platform' " );
342+ }
343+ }
344+
345+ /**
346+ * @param mixed $platforms
347+ */
348+ private function parsePlatformList ($ platforms ): array
349+ {
350+ if (is_string ($ platforms )) {
351+ return array_values (array_filter (preg_split ('/[,\s]+/ ' , $ platforms )));
352+ }
353+
354+ return (array ) ($ platforms ?? ['packagist.org ' , 'libraries.io ' ]);
355+ }
356+
357+ private function getFromFirstValidPlatform ($ package , $ platforms ): ?array
358+ {
359+ foreach ($ this ->parsePlatformList ($ platforms ) as $ platform ) {
360+ $ result = $ this ->getSourceFromPlatform ($ package , $ platform );
361+
362+ if ($ result !== null ) {
363+ return $ result ;
364+ }
365+ }
366+
367+ return null ;
293368 }
294369}
0 commit comments