1
0
Fork 0

Load plugins which modify install path early, fixes #10618 (#10621)

pull/10657/head
Jordi Boggiano 2022-03-15 22:11:19 +01:00 committed by GitHub
parent 7bee425b58
commit 6b97524b25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 6 deletions

View File

@ -323,6 +323,15 @@ hint to Composer that the plugin should be installed on its own before proceedin
the rest of the package downloads. This slightly slows down the overall installation
process however, so do not use it in plugins which do not absolutely require it.
### plugin-modifies-install-path
Some special plugins modify the install path of packages.
As of Composer 2.2.9, you can specify `{"extra": {"plugin-modifies-install-path": true}}`
in your composer.json to hint to Composer that the plugin should be activated as soon
as possible to prevent any bad side-effects from Composer assuming packages are installed
in another location than they actually are.
## Plugin Autoloading
Due to plugins being loaded by Composer at runtime, and to ensure that plugins which

View File

@ -477,7 +477,18 @@ class PluginManager
private function loadRepository(RepositoryInterface $repo, $isGlobalRepo)
{
$packages = $repo->getPackages();
$sortedPackages = PackageSorter::sortPackages($packages);
$weights = array();
foreach ($packages as $package) {
if ($package->getType() === 'composer-plugin') {
$extra = $package->getExtra();
if ($package->getName() === 'composer/installers' || (isset($extra['plugin-modifies-install-path']) && $extra['plugin-modifies-install-path'] === true)) {
$weights[$package->getName()] = -10000;
}
}
}
$sortedPackages = PackageSorter::sortPackages($packages, $weights);
foreach ($sortedPackages as $package) {
if (!($package instanceof CompletePackage)) {
continue;

View File

@ -23,9 +23,10 @@ class PackageSorter
* Packages of equal weight are sorted alphabetically
*
* @param PackageInterface[] $packages
* @param array<string, int> $weights Pre-set weights for some packages to give them more (negative number) or less (positive) weight offsets
* @return PackageInterface[] sorted array
*/
public static function sortPackages(array $packages)
public static function sortPackages(array $packages, array $weights = array())
{
$usageList = array();
@ -41,7 +42,7 @@ class PackageSorter
}
$computing = array();
$computed = array();
$computeImportance = function ($name) use (&$computeImportance, &$computing, &$computed, $usageList) {
$computeImportance = function ($name) use (&$computeImportance, &$computing, &$computed, $usageList, $weights) {
// reusing computed importance
if (isset($computed[$name])) {
return $computed[$name];
@ -53,7 +54,7 @@ class PackageSorter
}
$computing[$name] = true;
$weight = 0;
$weight = isset($weights[$name]) ? $weights[$name] : 0;
if (isset($usageList[$name])) {
foreach ($usageList[$name] as $user) {

View File

@ -127,6 +127,23 @@ class PackageSorterTest extends TestCase
'foo/baz',
),
),
'pre-weighted packages bumped to top incl their deps' => array(
array(
$this->createPackage('foo/bar', array('foo/dep')),
$this->createPackage('foo/bar2', array('foo/dep2')),
$this->createPackage('foo/dep', array()),
$this->createPackage('foo/dep2', array()),
),
array(
'foo/dep',
'foo/bar',
'foo/dep2',
'foo/bar2',
),
array(
'foo/bar' => -1000
)
),
);
}
@ -135,10 +152,11 @@ class PackageSorterTest extends TestCase
*
* @param Package[] $packages
* @param string[] $expectedOrderedList
* @param array<string, int> $weights
*/
public function testSortingOrdersDependenciesHigherThanPackage($packages, $expectedOrderedList)
public function testSortingOrdersDependenciesHigherThanPackage($packages, $expectedOrderedList, $weights = array())
{
$sortedPackages = PackageSorter::sortPackages($packages);
$sortedPackages = PackageSorter::sortPackages($packages, $weights);
$sortedPackageNames = array_map(function ($package) {
return $package->getName();
}, $sortedPackages);