Skip to content

Commit

Permalink
NEW Use composer 2 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz authored Apr 26, 2022
1 parent 25b4994 commit 416a550
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ the command line to install or update PHP packages.
### Requirements

* bringyourownideas/silverstripe-maintenance ^2
* composer/composer ^1
* composer/composer ^2
* silverstripe/framework ^4

#### Compatibility

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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
32 changes: 10 additions & 22 deletions src/Extensions/ComposerLoaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down
15 changes: 7 additions & 8 deletions src/UpdateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -117,7 +116,7 @@ protected function findLatestPackage(
}

$targetVersion = null;
if (0 === strpos($installedVersion, 'dev-')) {
if (0 === strpos($installedVersion ?? '', 'dev-')) {
$targetVersion = $installedVersion;
}

Expand All @@ -126,6 +125,6 @@ protected function findLatestPackage(
$targetVersion = $constraint;
}

return $versionSelector->findBestCandidate($name, $targetVersion, null, $bestStability);
return $versionSelector->findBestCandidate($name, $targetVersion, $bestStability);
}
}
13 changes: 8 additions & 5 deletions tests/Stubs/ComposerLoaderExtensionStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

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;

/**
* A partially stubbed version of the {@link ComposerLoaderExtension} extension which doesn't rely on Composer
*/
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');
Expand All @@ -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':
Expand Down
1 change: 0 additions & 1 deletion tests/UpdateCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 416a550

Please sign in to comment.