Skip to content

Commit

Permalink
Add support for Composer 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 2, 2020
1 parent 651c372 commit d0b137c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
],
"require": {
"php": "^7.4.0",
"composer-plugin-api": "^1.1.0"
"composer-plugin-api": "^1.1.0 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0.1",
"infection/infection": "^0.15.3",
"composer/composer": "^1.9.3",
"composer/composer": "^1.9.3 || ^2.0@dev",
"ext-zip": "^1.15.0",
"doctrine/coding-standard": "^7.0.2",
"vimeo/psalm": "^3.9.3"
Expand Down
8 changes: 7 additions & 1 deletion src/PackageVersions/FallbackVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ private static function getPackageData() : array
$data = json_decode(file_get_contents($path), true);
switch (basename($path)) {
case 'installed.json':
$packageData[] = $data;
// composer 2.x installed.json format
if (isset($data['packages'])) {
$packageData[] = $data['packages'];
} else {
// composer 1.x installed.json format
$packageData[] = $data;
}
break;
case 'composer.lock':
$packageData[] = $data['packages'] + ($data['packages-dev'] ?? []);
Expand Down
10 changes: 10 additions & 0 deletions src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public function activate(Composer $composer, IOInterface $io) : void
// Nothing to do here, as all features are provided through event listeners
}

public function deactivate(Composer $composer, IOInterface $io) : void
{
// Nothing to do here, as all features are provided through event listeners
}

public function uninstall(Composer $composer, IOInterface $io) : void
{
// Nothing to do here, as all features are provided through event listeners
}

/**
* {@inheritDoc}
*/
Expand Down
4 changes: 4 additions & 0 deletions test/PackageVersionsTest/FallbackVersionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function testValidVersionsWithoutComposerLock() : void
public function testValidVersionsWithoutInstalledJson() : void
{
$packages = json_decode(file_get_contents(__DIR__ . '/../../vendor/composer/installed.json'), true);
// normalize composer 2.x installed.json format to the 1.x one
if (isset($packages['packages'])) {
$packages = $packages['packages'];
}

if ($packages === []) {
// In case of --no-dev flag
Expand Down

0 comments on commit d0b137c

Please sign in to comment.