Skip to content

Commit

Permalink
Separating out findBestCandidate() code for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
pdohogne-magento committed Dec 15, 2020
1 parent 243946b commit a453f06
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 66 deletions.
6 changes: 6 additions & 0 deletions docs/class_descriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ This class contains methods to retrieve Composer [Package](https://getcomposer.o
- Returns the existing root project package, including all user customizations
- **`fetchMageRootFromRepo()`**
- Given a Magento edition and version constraint, fetch the best-fit Magento root project package from the Composer repository or GitHub (in the case of cloud)
- **`findBestCandidate()`**
- Wrapper function around different versions of [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html)
- **`findBestCandidateComposer1()`**
- Helper function to run [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html) on Composer version 1.x.x
- **`findBestCandidateComposer2()`**
- Helper function to run [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html) on Composer version 2.x.x
- **`parseVersionAndEditionFromLock()`**
- Inspect the `composer.lock` file for the currently-installed Magento product or cloud metapackage and parse out the edition and version for use by `getOriginalRootPackage()`
- **`getTargetLabel()`**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Composer\Installer\PackageEvent;
use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Exception;
use Magento\ComposerRootUpdatePlugin\Utils\Console;
use Magento\ComposerRootUpdatePlugin\Utils\PackageUtils;
Expand Down Expand Up @@ -55,8 +54,8 @@ public function __construct($console)
public function packageEvent($event)
{
$packageName = PluginDefinition::PACKAGE_NAME;
$apiMajorVersion = explode('.', PluginInterface::PLUGIN_API_VERSION)[0];
if ($apiMajorVersion == '1') {
$composerMajorVersion = explode('.', Composer::VERSION)[0];
if ($composerMajorVersion == '1') {
$jobs = $event->getRequest()->getJobs();
foreach ($jobs as $job) {
if (key_exists('packageName', $job) && $job['packageName'] === $packageName) {
Expand All @@ -67,7 +66,7 @@ public function packageEvent($event)
}
}
}
} elseif ($apiMajorVersion == '2') {
} elseif ($composerMajorVersion == '2') {
if (strpos($event->getOperation()->show(false), $packageName) !== false) {
$pkg = $event->getLocalRepo()->findPackage($packageName, '*');
if ($pkg !== null) {
Expand Down
229 changes: 167 additions & 62 deletions src/Magento/ComposerRootUpdatePlugin/Updater/RootPackageRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

use Composer\Composer;
use Composer\DependencyResolver\Pool;
use Composer\IO\IOInterface;
use Composer\Package\BasePackage;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Package\Version\VersionSelector;
use Composer\Plugin\PluginInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\RepositorySet;
use Composer\Repository\VcsRepository;
use Magento\ComposerRootUpdatePlugin\ComposerReimplementation\AccessibleRootPackageLoader;
Expand Down Expand Up @@ -251,20 +250,15 @@ protected function fetchMageRootFromRepo(
$phpVersion = null,
$preferredStability = 'stable'
) {
$apiMajorVersion = explode('.', PluginInterface::PLUGIN_API_VERSION)[0];
$packageName = $this->pkgUtils->getProjectPackageName($edition);
$parsedConstraint = (new VersionParser())->parseConstraints($constraint);
$phpVersion = $ignorePlatformReqs ? null : $phpVersion;

$minStability = $this->composer->getPackage()->getMinimumStability();
if (!$minStability) {
$minStability = 'stable';
}
$rootPackageLoader = new AccessibleRootPackageLoader();
$stabilityFlags = $rootPackageLoader->extractStabilityFlags($packageName, $constraint, $minStability);
$stability = key_exists($packageName, $stabilityFlags)
? array_search($stabilityFlags[$packageName], BasePackage::$stabilities)
: $minStability;
$this->console->comment("Minimum stability for \"$packageName: $constraint\": $stability", IOInterface::DEBUG);

$metapackageName = $this->pkgUtils->getMetapackageName($edition);
if ($edition != PackageUtils::CLOUD_PKG_EDITION && !$this->pkgUtils->isConstraintStrict($constraint)) {
Expand All @@ -275,76 +269,187 @@ protected function fetchMageRootFromRepo(
);
}

$phpVersion = $ignorePlatformReqs ? null : $phpVersion;
$versionSelector = null;
$result = null;
if ($apiMajorVersion == '1') {
$pool = new Pool(
$stability,
$stabilityFlags,
[$packageName => $parsedConstraint]
);
if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
// magento/magento-cloud-template exists on github, not the composer repo
$repoConfig = [
'url' => 'https://github.com/magento/magento-cloud',
'type' => 'vcs'
];
$pool->addRepository(new VcsRepository(
$repoConfig,
$this->console->getIO(),
$this->composer->getConfig()
));
} else {
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
$bestCandidate = $this->findBestCandidate(
$packageName,
$edition,
$constraint,
$minStability,
$stabilityFlags,
$preferredStability,
$ignorePlatformReqs,
$phpVersion
);

if (!$bestCandidate) {
$err = "Could not find a Magento project package matching \"$metapackageName $constraint\"";
if ($phpVersion) {
$err = "$err for PHP version $phpVersion";
}
$this->console->error($err);
}

$versionSelector = new VersionSelector($pool);
$result = ($versionSelector)->findBestCandidate(
return $bestCandidate;
}

/**
* Wrapper functions around different versions of VersionSelector::findBestCandidate()
*
* @param string $packageName
* @param string $edition
* @param string $constraint
* @param string $minStability
* @param array $stabilityFlags
* @param string $preferredStability
* @param bool $ignorePlatformReqs
* @param string $phpVersion
* @return PackageInterface|false
*
* @see VersionSelector::findBestCandidate()
*/
protected function findBestCandidate(
$packageName,
$edition,
$constraint,
$minStability,
$stabilityFlags,
$preferredStability,
$ignorePlatformReqs,
$phpVersion
) {
$composerMajorVersion = explode('.', Composer::VERSION)[0];
$bestCandidate = null;
if ($composerMajorVersion == '1') {
$bestCandidate = $this->findBestCandidateComposer1(
$packageName,
$edition,
$constraint,
$phpVersion,
$preferredStability
$minStability,
$stabilityFlags,
$preferredStability,
$phpVersion
);
} elseif ($apiMajorVersion == '2') {
$repositorySet = new RepositorySet($minStability, $stabilityFlags);
if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
// magento/magento-cloud-template exists on github, not the composer repo
$repoConfig = [
'url' => 'https://github.com/magento/magento-cloud',
'type' => 'vcs'
];
$repositorySet->addRepository(new VcsRepository(
$repoConfig,
$this->console->getIO(),
$this->composer->getConfig(),
$this->composer->getLoop()->getHttpDownloader()
));
} else {
$repositorySet->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
}

$versionSelector = new VersionSelector($repositorySet);
$result = ($versionSelector)->findBestCandidate(
} elseif ($composerMajorVersion == '2') {
$bestCandidate = $this->findBestCandidateComposer2(
$packageName,
$edition,
$constraint,
$preferredStability
$minStability,
$stabilityFlags,
$preferredStability,
$ignorePlatformReqs
);
} else {
$this->console->error(
"Fetching Magento root composer failed; unrecognized composer plugin API version"
);
}
return $bestCandidate;
}

/**
* Helper function to run VersionSelector::findBestCandidate() on Composer version 1.x.x
*
* @param string $packageName
* @param string $edition
* @param string $constraint
* @param string $minStability
* @param array $stabilityFlags
* @param string $preferredStability
* @param string $phpVersion
* @return PackageInterface|false
*/
private function findBestCandidateComposer1(
$packageName,
$edition,
$constraint,
$minStability,
$stabilityFlags,
$preferredStability,
$phpVersion
) {
$parsedConstraint = (new VersionParser())->parseConstraints($constraint);
$stability = key_exists($packageName, $stabilityFlags)
? array_search($stabilityFlags[$packageName], BasePackage::$stabilities)
: $minStability;

$pool = new Pool(
$stability,
$stabilityFlags,
[$packageName => $parsedConstraint]
);

if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
// magento/magento-cloud-template exists on github, not the composer repo
$repoConfig = [
'url' => 'https://github.com/magento/magento-cloud',
'type' => 'vcs'
];
$pool->addRepository(new VcsRepository(
$repoConfig,
$this->console->getIO(),
$this->composer->getConfig()
));
} else {
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
}

if (!$result) {
$err = "Could not find a Magento project package matching \"$metapackageName $constraint\"";
if ($phpVersion) {
$err = "$err for PHP version $phpVersion";
}
$this->console->error($err);
return (new VersionSelector($pool))->findBestCandidate(
$packageName,
$constraint,
$phpVersion,
$preferredStability
);
}

/**
* Helper function to run VersionSelector::findBestCandidate() on Composer version 2.x.x
*
* @param string $packageName
* @param string $edition
* @param string $constraint
* @param string $minStability
* @param array $stabilityFlags
* @param string $preferredStability
* @param bool $ignorePlatformReqs
* @return PackageInterface|false
*/
private function findBestCandidateComposer2(
$packageName,
$edition,
$constraint,
$minStability,
$stabilityFlags,
$preferredStability,
$ignorePlatformReqs
) {
$platformOverrides = $this->composer->getConfig()->get('platform') ?: array();
$platformRepo = new PlatformRepository(array(), $platformOverrides);
$repositorySet = new RepositorySet($minStability, $stabilityFlags);

if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
// magento/magento-cloud-template exists on github, not the composer repo
$repoConfig = [
'url' => 'https://github.com/magento/magento-cloud',
'type' => 'vcs'
];
$repositorySet->addRepository(new VcsRepository(
$repoConfig,
$this->console->getIO(),
$this->composer->getConfig(),
$this->composer->getLoop()->getHttpDownloader()
));
} else {
$repositorySet->addRepository(
new CompositeRepository($this->composer->getRepositoryManager()->getRepositories())
);
}

return $result;
return (new VersionSelector($repositorySet, $platformRepo))->findBestCandidate(
$packageName,
$constraint,
$preferredStability,
$ignorePlatformReqs
);
}

/**
Expand Down

0 comments on commit a453f06

Please sign in to comment.