Skip to content

Commit

Permalink
Merge pull request #2 from magento-commerce/MC-41943
Browse files Browse the repository at this point in the history
MC-41943: Add Support for Composer V2 in plugin.
  • Loading branch information
admanesachin authored Jun 14, 2021
2 parents 5d75c24 + 634b893 commit 2e846ae
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "magento/composer-dependency-version-audit-plugin",
"type": "composer-plugin",
"description": "Validating packages through a composer plugin",
"version": "0.1.0",
"version": "0.1.1",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"composer/composer": "<=1.10.20",
"composer-plugin-api": "^1.0"
"composer/composer": "^1.9 || ^2.0",
"composer-plugin-api": "^1.0 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9"
Expand Down
63 changes: 55 additions & 8 deletions src/Utils/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Composer\DependencyResolver\Pool;
use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface;
use Magento\ComposerDependencyVersionAuditPlugin\VersionSelectorFactory;
use Composer\Package\Version\VersionSelector;
use Composer\Repository\RepositorySet;
use Exception;

/**
* Wrapper class for calling Composer functions
Expand All @@ -26,20 +28,65 @@ class Version
* @param string $packageName
* @param RepositoryInterface $repository
* @return PackageInterface|null
* @throws Exception
*/
public function findBestCandidate(Composer $composer, string $packageName, RepositoryInterface $repository): ?PackageInterface
{
$pool = new Pool(
$composer->getPackage()->getMinimumStability(),
$composer->getPackage()->getStabilityFlags()
);
$pool->addRepository($repository);
$versionSelector = VersionSelectorFactory::create($pool);
$bestCandidate = $versionSelector->findBestCandidate($packageName);
$composerMajorVersion = (int)explode('.', $composer::VERSION)[0];

if ($composerMajorVersion === 1) {
$bestCandidate = $this->findBestCandidateComposer1($composer, $packageName, $repository);
} elseif ($composerMajorVersion === 2) {
$bestCandidate = $this->findBestCandidateComposer2($composer, $packageName, $repository);
} else {
throw new Exception("Unrecognized Composer Version");
}

if($bestCandidate instanceof PackageInterface){
return $bestCandidate;
}
return null;
}

/**
* Get Highest version package for Composer V1
*
* @param Composer $composer
* @param string $packageName
* @param RepositoryInterface $repository
* @return PackageInterface|false
*/
public function findBestCandidateComposer1(Composer $composer, string $packageName, RepositoryInterface $repository)
{
$minStability = $composer->getPackage()->getMinimumStability();
$stabilityFlags = $composer->getPackage()->getStabilityFlags();
if (!$minStability) {
$minStability = 'stable';
}
$pool = new Pool($minStability, $stabilityFlags);
$pool->addRepository($repository);
return (new VersionSelector($pool))->findBestCandidate($packageName);
}

/**
* Get Highest version package for Composer V2
*
* @param Composer $composer
* @param string $packageName
* @param RepositoryInterface $repository
* @return PackageInterface|false
*/
public function findBestCandidateComposer2(Composer $composer, string $packageName, RepositoryInterface $repository)
{
$minStability = $composer->getPackage()->getMinimumStability();
$stabilityFlags = $composer->getPackage()->getStabilityFlags();

if (!$minStability) {
$minStability = 'stable';
}

$repositorySet = new RepositorySet($minStability, $stabilityFlags);
$repositorySet->addRepository($repository);
return (new VersionSelector($repositorySet))->findBestCandidate($packageName);
}
}
29 changes: 0 additions & 29 deletions src/VersionSelectorFactory.php

This file was deleted.

0 comments on commit 2e846ae

Please sign in to comment.