/
PackageSorter.php
140 lines (117 loc) · 4.08 KB
/
PackageSorter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php declare(strict_types=1);
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Util;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackageInterface;
class PackageSorter
{
/**
* Returns the most recent version of a set of packages
*
* This is ideally the default branch version, or failing that it will return the package with the highest version
*
* @template T of PackageInterface
* @param array<T> $packages
* @return ($packages is non-empty-array<T> ? T : T|null)
*/
public static function getMostCurrentVersion(array $packages): ?PackageInterface
{
if (count($packages) === 0) {
return null;
}
$highest = reset($packages);
foreach ($packages as $candidate) {
if ($candidate->isDefaultBranch()) {
return $candidate;
}
if (version_compare($highest->getVersion(), $candidate->getVersion(), '<')) {
$highest = $candidate;
}
}
return $highest;
}
/**
* Sorts packages by name
*
* @template T of PackageInterface
* @param array<T> $packages
* @return array<T>
*/
public static function sortPackagesAlphabetically(array $packages): array
{
usort($packages, static function (PackageInterface $a, PackageInterface $b) {
return $a->getName() <=> $b->getName();
});
return $packages;
}
/**
* Sorts packages by dependency weight
*
* 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, array $weights = []): array
{
$usageList = [];
foreach ($packages as $package) {
$links = $package->getRequires();
if ($package instanceof RootPackageInterface) {
$links = array_merge($links, $package->getDevRequires());
}
foreach ($links as $link) {
$target = $link->getTarget();
$usageList[$target][] = $package->getName();
}
}
$computing = [];
$computed = [];
$computeImportance = static function ($name) use (&$computeImportance, &$computing, &$computed, $usageList, $weights) {
// reusing computed importance
if (isset($computed[$name])) {
return $computed[$name];
}
// canceling circular dependency
if (isset($computing[$name])) {
return 0;
}
$computing[$name] = true;
$weight = $weights[$name] ?? 0;
if (isset($usageList[$name])) {
foreach ($usageList[$name] as $user) {
$weight -= 1 - $computeImportance($user);
}
}
unset($computing[$name]);
$computed[$name] = $weight;
return $weight;
};
$weightedPackages = [];
foreach ($packages as $index => $package) {
$name = $package->getName();
$weight = $computeImportance($name);
$weightedPackages[] = ['name' => $name, 'weight' => $weight, 'index' => $index];
}
usort($weightedPackages, static function (array $a, array $b): int {
if ($a['weight'] !== $b['weight']) {
return $a['weight'] - $b['weight'];
}
return strnatcasecmp($a['name'], $b['name']);
});
$sortedPackages = [];
foreach ($weightedPackages as $pkg) {
$sortedPackages[] = $packages[$pkg['index']];
}
return $sortedPackages;
}
}