88
99use Composer \Composer ;
1010use Composer \DependencyResolver \Pool ;
11- use Composer \IO \IOInterface ;
1211use Composer \Package \BasePackage ;
1312use Composer \Package \PackageInterface ;
1413use Composer \Package \RootPackageInterface ;
1514use Composer \Package \Version \VersionParser ;
1615use Composer \Package \Version \VersionSelector ;
17- use Composer \Plugin \PluginInterface ;
1816use Composer \Repository \CompositeRepository ;
17+ use Composer \Repository \PlatformRepository ;
1918use Composer \Repository \RepositorySet ;
2019use Composer \Repository \VcsRepository ;
2120use Magento \ComposerRootUpdatePlugin \ComposerReimplementation \AccessibleRootPackageLoader ;
@@ -251,20 +250,15 @@ protected function fetchMageRootFromRepo(
251250 $ phpVersion = null ,
252251 $ preferredStability = 'stable '
253252 ) {
254- $ apiMajorVersion = explode ('. ' , PluginInterface::PLUGIN_API_VERSION )[0 ];
255253 $ packageName = $ this ->pkgUtils ->getProjectPackageName ($ edition );
256- $ parsedConstraint = ( new VersionParser ())-> parseConstraints ( $ constraint ) ;
254+ $ phpVersion = $ ignorePlatformReqs ? null : $ phpVersion ;
257255
258256 $ minStability = $ this ->composer ->getPackage ()->getMinimumStability ();
259257 if (!$ minStability ) {
260258 $ minStability = 'stable ' ;
261259 }
262260 $ rootPackageLoader = new AccessibleRootPackageLoader ();
263261 $ stabilityFlags = $ rootPackageLoader ->extractStabilityFlags ($ packageName , $ constraint , $ minStability );
264- $ stability = key_exists ($ packageName , $ stabilityFlags )
265- ? array_search ($ stabilityFlags [$ packageName ], BasePackage::$ stabilities )
266- : $ minStability ;
267- $ this ->console ->comment ("Minimum stability for \"$ packageName: $ constraint \": $ stability " , IOInterface::DEBUG );
268262
269263 $ metapackageName = $ this ->pkgUtils ->getMetapackageName ($ edition );
270264 if ($ edition != PackageUtils::CLOUD_PKG_EDITION && !$ this ->pkgUtils ->isConstraintStrict ($ constraint )) {
@@ -275,76 +269,187 @@ protected function fetchMageRootFromRepo(
275269 );
276270 }
277271
278- $ phpVersion = $ ignorePlatformReqs ? null : $ phpVersion ;
279- $ versionSelector = null ;
280- $ result = null ;
281- if ($ apiMajorVersion == '1 ' ) {
282- $ pool = new Pool (
283- $ stability ,
284- $ stabilityFlags ,
285- [$ packageName => $ parsedConstraint ]
286- );
287- if ($ edition == PackageUtils::CLOUD_PKG_EDITION ) {
288- // magento/magento-cloud-template exists on github, not the composer repo
289- $ repoConfig = [
290- 'url ' => 'https://github.com/magento/magento-cloud ' ,
291- 'type ' => 'vcs '
292- ];
293- $ pool ->addRepository (new VcsRepository (
294- $ repoConfig ,
295- $ this ->console ->getIO (),
296- $ this ->composer ->getConfig ()
297- ));
298- } else {
299- $ pool ->addRepository (new CompositeRepository ($ this ->composer ->getRepositoryManager ()->getRepositories ()));
272+ $ bestCandidate = $ this ->findBestCandidate (
273+ $ packageName ,
274+ $ edition ,
275+ $ constraint ,
276+ $ minStability ,
277+ $ stabilityFlags ,
278+ $ preferredStability ,
279+ $ ignorePlatformReqs ,
280+ $ phpVersion
281+ );
282+
283+ if (!$ bestCandidate ) {
284+ $ err = "Could not find a Magento project package matching \"$ metapackageName $ constraint \"" ;
285+ if ($ phpVersion ) {
286+ $ err = "$ err for PHP version $ phpVersion " ;
300287 }
288+ $ this ->console ->error ($ err );
289+ }
301290
302- $ versionSelector = new VersionSelector ($ pool );
303- $ result = ($ versionSelector )->findBestCandidate (
291+ return $ bestCandidate ;
292+ }
293+
294+ /**
295+ * Wrapper functions around different versions of VersionSelector::findBestCandidate()
296+ *
297+ * @param string $packageName
298+ * @param string $edition
299+ * @param string $constraint
300+ * @param string $minStability
301+ * @param array $stabilityFlags
302+ * @param string $preferredStability
303+ * @param bool $ignorePlatformReqs
304+ * @param string $phpVersion
305+ * @return PackageInterface|false
306+ *
307+ * @see VersionSelector::findBestCandidate()
308+ */
309+ protected function findBestCandidate (
310+ $ packageName ,
311+ $ edition ,
312+ $ constraint ,
313+ $ minStability ,
314+ $ stabilityFlags ,
315+ $ preferredStability ,
316+ $ ignorePlatformReqs ,
317+ $ phpVersion
318+ ) {
319+ $ composerMajorVersion = explode ('. ' , Composer::VERSION )[0 ];
320+ $ bestCandidate = null ;
321+ if ($ composerMajorVersion == '1 ' ) {
322+ $ bestCandidate = $ this ->findBestCandidateComposer1 (
304323 $ packageName ,
324+ $ edition ,
305325 $ constraint ,
306- $ phpVersion ,
307- $ preferredStability
326+ $ minStability ,
327+ $ stabilityFlags ,
328+ $ preferredStability ,
329+ $ phpVersion
308330 );
309- } elseif ($ apiMajorVersion == '2 ' ) {
310- $ repositorySet = new RepositorySet ($ minStability , $ stabilityFlags );
311- if ($ edition == PackageUtils::CLOUD_PKG_EDITION ) {
312- // magento/magento-cloud-template exists on github, not the composer repo
313- $ repoConfig = [
314- 'url ' => 'https://github.com/magento/magento-cloud ' ,
315- 'type ' => 'vcs '
316- ];
317- $ repositorySet ->addRepository (new VcsRepository (
318- $ repoConfig ,
319- $ this ->console ->getIO (),
320- $ this ->composer ->getConfig (),
321- $ this ->composer ->getLoop ()->getHttpDownloader ()
322- ));
323- } else {
324- $ repositorySet ->addRepository (new CompositeRepository ($ this ->composer ->getRepositoryManager ()->getRepositories ()));
325- }
326-
327- $ versionSelector = new VersionSelector ($ repositorySet );
328- $ result = ($ versionSelector )->findBestCandidate (
331+ } elseif ($ composerMajorVersion == '2 ' ) {
332+ $ bestCandidate = $ this ->findBestCandidateComposer2 (
329333 $ packageName ,
334+ $ edition ,
330335 $ constraint ,
331- $ preferredStability
336+ $ minStability ,
337+ $ stabilityFlags ,
338+ $ preferredStability ,
339+ $ ignorePlatformReqs
332340 );
333341 } else {
334342 $ this ->console ->error (
335343 "Fetching Magento root composer failed; unrecognized composer plugin API version "
336344 );
337345 }
346+ return $ bestCandidate ;
347+ }
348+
349+ /**
350+ * Helper function to run VersionSelector::findBestCandidate() on Composer version 1.x.x
351+ *
352+ * @param string $packageName
353+ * @param string $edition
354+ * @param string $constraint
355+ * @param string $minStability
356+ * @param array $stabilityFlags
357+ * @param string $preferredStability
358+ * @param string $phpVersion
359+ * @return PackageInterface|false
360+ */
361+ private function findBestCandidateComposer1 (
362+ $ packageName ,
363+ $ edition ,
364+ $ constraint ,
365+ $ minStability ,
366+ $ stabilityFlags ,
367+ $ preferredStability ,
368+ $ phpVersion
369+ ) {
370+ $ parsedConstraint = (new VersionParser ())->parseConstraints ($ constraint );
371+ $ stability = key_exists ($ packageName , $ stabilityFlags )
372+ ? array_search ($ stabilityFlags [$ packageName ], BasePackage::$ stabilities )
373+ : $ minStability ;
374+
375+ $ pool = new Pool (
376+ $ stability ,
377+ $ stabilityFlags ,
378+ [$ packageName => $ parsedConstraint ]
379+ );
380+
381+ if ($ edition == PackageUtils::CLOUD_PKG_EDITION ) {
382+ // magento/magento-cloud-template exists on github, not the composer repo
383+ $ repoConfig = [
384+ 'url ' => 'https://github.com/magento/magento-cloud ' ,
385+ 'type ' => 'vcs '
386+ ];
387+ $ pool ->addRepository (new VcsRepository (
388+ $ repoConfig ,
389+ $ this ->console ->getIO (),
390+ $ this ->composer ->getConfig ()
391+ ));
392+ } else {
393+ $ pool ->addRepository (new CompositeRepository ($ this ->composer ->getRepositoryManager ()->getRepositories ()));
394+ }
338395
339- if (!$ result ) {
340- $ err = "Could not find a Magento project package matching \"$ metapackageName $ constraint \"" ;
341- if ($ phpVersion ) {
342- $ err = "$ err for PHP version $ phpVersion " ;
343- }
344- $ this ->console ->error ($ err );
396+ return (new VersionSelector ($ pool ))->findBestCandidate (
397+ $ packageName ,
398+ $ constraint ,
399+ $ phpVersion ,
400+ $ preferredStability
401+ );
402+ }
403+
404+ /**
405+ * Helper function to run VersionSelector::findBestCandidate() on Composer version 2.x.x
406+ *
407+ * @param string $packageName
408+ * @param string $edition
409+ * @param string $constraint
410+ * @param string $minStability
411+ * @param array $stabilityFlags
412+ * @param string $preferredStability
413+ * @param bool $ignorePlatformReqs
414+ * @return PackageInterface|false
415+ */
416+ private function findBestCandidateComposer2 (
417+ $ packageName ,
418+ $ edition ,
419+ $ constraint ,
420+ $ minStability ,
421+ $ stabilityFlags ,
422+ $ preferredStability ,
423+ $ ignorePlatformReqs
424+ ) {
425+ $ platformOverrides = $ this ->composer ->getConfig ()->get ('platform ' ) ?: array ();
426+ $ platformRepo = new PlatformRepository (array (), $ platformOverrides );
427+ $ repositorySet = new RepositorySet ($ minStability , $ stabilityFlags );
428+
429+ if ($ edition == PackageUtils::CLOUD_PKG_EDITION ) {
430+ // magento/magento-cloud-template exists on github, not the composer repo
431+ $ repoConfig = [
432+ 'url ' => 'https://github.com/magento/magento-cloud ' ,
433+ 'type ' => 'vcs '
434+ ];
435+ $ repositorySet ->addRepository (new VcsRepository (
436+ $ repoConfig ,
437+ $ this ->console ->getIO (),
438+ $ this ->composer ->getConfig (),
439+ $ this ->composer ->getLoop ()->getHttpDownloader ()
440+ ));
441+ } else {
442+ $ repositorySet ->addRepository (
443+ new CompositeRepository ($ this ->composer ->getRepositoryManager ()->getRepositories ())
444+ );
345445 }
346446
347- return $ result ;
447+ return (new VersionSelector ($ repositorySet , $ platformRepo ))->findBestCandidate (
448+ $ packageName ,
449+ $ constraint ,
450+ $ preferredStability ,
451+ $ ignorePlatformReqs
452+ );
348453 }
349454
350455 /**
0 commit comments