diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3832a91..3fc1da0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,4 +6,4 @@ on: jobs: ci: - uses: silverstripe/github-actions-ci-cd/.github/workflows/ci.yml@0.1.14 + uses: silverstripe/github-actions-ci-cd/.github/workflows/ci.yml@v0.1 diff --git a/README.md b/README.md index 7104e29..485e809 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ the command line to install or update PHP packages. ### Requirements * bringyourownideas/silverstripe-maintenance ^2 -* composer/composer ^1 +* composer/composer ^2 * silverstripe/framework ^4 #### Compatibility @@ -30,6 +30,9 @@ the command line to install or update PHP packages. The 1.x release line of this module is compatible with SilverStripe ^3.2, and the 2.x release line is compatible with SilverStripe ^4.0. +The 2.x release line of the module is compatible with composer v1, and this 3.x release line is +compatible with composer v2 + ### Installation Run the following command to install this package: diff --git a/composer.json b/composer.json index 386841f..38683e2 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "prefer-stable": true, "require": { "php": "^7.4 || ^8.0", - "composer/composer": "^1.10", + "composer/composer": "^2", "silverstripe/framework": "^4.10", "bringyourownideas/silverstripe-maintenance": "^2" }, diff --git a/src/Extensions/ComposerLoaderExtension.php b/src/Extensions/ComposerLoaderExtension.php index 7ce9614..db00e00 100644 --- a/src/Extensions/ComposerLoaderExtension.php +++ b/src/Extensions/ComposerLoaderExtension.php @@ -6,9 +6,8 @@ use Composer\Factory; use Composer\IO\NullIO; use Composer\Package\Link; -use Composer\Repository\ArrayRepository; -use Composer\Repository\BaseRepository; -use Composer\Repository\CompositeRepository; +use Composer\Repository\InstalledRepository; +use Composer\Repository\RootPackageRepository; use Composer\Repository\RepositoryInterface; use SilverStripe\Core\Environment; use SilverStripe\Core\Extension; @@ -40,11 +39,10 @@ public function getComposer() /** * Retrieve an array of primary composer dependencies from composer.json. - * * Packages are filtered by allowed type. + * Dependencies in composer.json that do not match any of the given types are not returned. * - * @param array|null $allowedTypes An array of "allowed" package types. Dependencies in composer.json that do not - * match any of the given types are not returned. + * @param array|null $allowedTypes An array of "allowed" package types. * @return array[] */ public function getPackages(array $allowedTypes = null) @@ -69,39 +67,29 @@ public function getPackages(array $allowedTypes = null) /** * Provides access to the Composer repository - * - * @return RepositoryInterface */ - protected function getRepository() + protected function getRepository(): RepositoryInterface { /** @var Composer $composer */ $composer = $this->getComposer(); - - /** @var BaseRepository $repository */ - return new CompositeRepository([ - new ArrayRepository([$composer->getPackage()]), - $composer->getRepositoryManager()->getLocalRepository(), + return new InstalledRepository([ + new RootPackageRepository($composer->getPackage()), + $composer->getRepositoryManager()->getLocalRepository() ]); } /** * Find all dependency constraints for the given package in the current repository and return the strictest one - * - * @param BaseRepository $repository - * @param string $packageName - * @return string */ - protected function getInstalledConstraint(BaseRepository $repository, $packageName) + protected function getInstalledConstraint(InstalledRepository $repository, string $packageName): string { $constraints = []; foreach ($repository->getDependents($packageName) as $dependent) { /** @var Link $link */ list (, $link) = $dependent; - $constraints[] = $link->getPrettyConstraint(); + $constraints[] = $link->getPrettyConstraint() ?? ''; } - usort($constraints, 'version_compare'); - return array_pop($constraints); } diff --git a/src/UpdateChecker.php b/src/UpdateChecker.php index 3db6163..2112752 100644 --- a/src/UpdateChecker.php +++ b/src/UpdateChecker.php @@ -4,11 +4,11 @@ use BringYourOwnIdeas\Maintenance\Util\ComposerLoader; use Composer\Composer; -use Composer\DependencyResolver\Pool; use Composer\Package\BasePackage; use Composer\Package\PackageInterface; use Composer\Package\Version\VersionSelector; use Composer\Repository\CompositeRepository; +use Composer\Repository\RepositorySet; use SilverStripe\Core\Injector\Injector; /** @@ -71,15 +71,14 @@ protected function getVersionSelector(Composer $composer) { if (!$this->versionSelector) { // Instantiate a new repository pool, providing the stability flags from the project - $pool = new Pool( + $respositorySet = new RepositorySet( $composer->getPackage()->getMinimumStability(), $composer->getPackage()->getStabilityFlags() ); - $pool->addRepository(new CompositeRepository($composer->getRepositoryManager()->getRepositories())); - - $this->versionSelector = new VersionSelector($pool); + $repo = new CompositeRepository($composer->getRepositoryManager()->getRepositories()); + $respositorySet->addRepository($repo); + $this->versionSelector = new VersionSelector($respositorySet); } - return $this->versionSelector; } @@ -117,7 +116,7 @@ protected function findLatestPackage( } $targetVersion = null; - if (0 === strpos($installedVersion, 'dev-')) { + if (0 === strpos($installedVersion ?? '', 'dev-')) { $targetVersion = $installedVersion; } @@ -126,6 +125,6 @@ protected function findLatestPackage( $targetVersion = $constraint; } - return $versionSelector->findBestCandidate($name, $targetVersion, null, $bestStability); + return $versionSelector->findBestCandidate($name, $targetVersion, $bestStability); } } diff --git a/tests/Stubs/ComposerLoaderExtensionStub.php b/tests/Stubs/ComposerLoaderExtensionStub.php index 55cbd46..ea14f1d 100644 --- a/tests/Stubs/ComposerLoaderExtensionStub.php +++ b/tests/Stubs/ComposerLoaderExtensionStub.php @@ -4,8 +4,9 @@ use BringYourOwnIdeas\UpdateChecker\Extensions\ComposerLoaderExtension; use Composer\Package\Package; -use Composer\Repository\ArrayRepository; -use Composer\Repository\BaseRepository; +use Composer\Repository\InstalledRepository; +use Composer\Repository\InstalledArrayRepository; +use Composer\Repository\RepositoryInterface; use SilverStripe\Dev\TestOnly; /** @@ -13,7 +14,7 @@ */ class ComposerLoaderExtensionStub extends ComposerLoaderExtension implements TestOnly { - protected function getRepository() + protected function getRepository(): RepositoryInterface { $vendorModule = new Package('silverstripe/framework', '4.1.1.0', '4.1.1'); $vendorModule->setType('silverstripe-vendormodule'); @@ -24,10 +25,12 @@ protected function getRepository() $generalPackage = new Package('something/unrelated', '1.2.3.4', '1.2.3'); $generalPackage->setType('package'); - return new ArrayRepository([$vendorModule, $silverstripeModule, $generalPackage]); + return new InstalledRepository([ + new InstalledArrayRepository([$vendorModule, $silverstripeModule, $generalPackage]) + ]); } - protected function getInstalledConstraint(BaseRepository $repository, $packageName) + protected function getInstalledConstraint(InstalledRepository $repository, string $packageName): string { switch ($packageName) { case 'silverstripe/framework': diff --git a/tests/UpdateCheckerTest.php b/tests/UpdateCheckerTest.php index d1cb6b5..382d33d 100644 --- a/tests/UpdateCheckerTest.php +++ b/tests/UpdateCheckerTest.php @@ -55,7 +55,6 @@ public function testCheckForUpdates() ->method('findLatestPackage') ->will($this->returnValue($mockPackage)); - $result = $this->updateChecker->checkForUpdates($mockPackage, '~1.2.0'); $this->assertArrayNotHasKey('AvailableVersion', $result, 'No available update is recorded'); $this->assertArrayNotHasKey('AvailableHash', $result, 'No available update is recorded');