|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Config; |
| 6 | + |
| 7 | +use Composer\Composer; |
| 8 | +use Composer\Package\CompletePackage; |
| 9 | +use Composer\Package\PackageInterface; |
| 10 | + |
| 11 | +use function array_key_exists; |
| 12 | + |
| 13 | +/** |
| 14 | + * @internal |
| 15 | + */ |
| 16 | +final class PackagesListBuilder |
| 17 | +{ |
| 18 | + private Composer $composer; |
| 19 | + |
| 20 | + public function __construct(Composer $composer) |
| 21 | + { |
| 22 | + $this->composer = $composer; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @return CompletePackage[] |
| 27 | + * |
| 28 | + * @psalm-return array<string, CompletePackage> |
| 29 | + */ |
| 30 | + public function build(): array |
| 31 | + { |
| 32 | + $allPackages = $this->getAllPackages(); |
| 33 | + |
| 34 | + $packageDepths = []; |
| 35 | + $this->calculatePackageDepths($allPackages, $packageDepths, 0, $this->composer->getPackage(), true); |
| 36 | + |
| 37 | + $result = []; |
| 38 | + foreach ($this->getSortedPackageNames($packageDepths) as $name) { |
| 39 | + if (array_key_exists($name, $allPackages)) { |
| 40 | + $result[$name] = $allPackages[$name]; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return $result; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get package names stable sorted by depth |
| 49 | + * |
| 50 | + * @psalm-param array<string, int> $packageDepths |
| 51 | + * |
| 52 | + * @return string[] |
| 53 | + */ |
| 54 | + private function getSortedPackageNames(array $packageDepths): array |
| 55 | + { |
| 56 | + $n = 0; |
| 57 | + foreach ($packageDepths as $name => $depth) { |
| 58 | + $packageDepths[$name] = [$depth, ++$n]; |
| 59 | + } |
| 60 | + |
| 61 | + /** @psalm-var array<string, array{0:int,1:int}> $packageDepths */ |
| 62 | + |
| 63 | + uasort($packageDepths, static function (array $a, array $b) { |
| 64 | + $result = $a[0] <=> $b[0]; |
| 65 | + return $result === 0 ? $a[1] <=> $b[1] : $result; |
| 66 | + }); |
| 67 | + |
| 68 | + return array_keys($packageDepths); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param CompletePackage[] $allPackages |
| 73 | + * |
| 74 | + * @psalm-param array<string, CompletePackage> $allPackages |
| 75 | + * @psalm-param array<string, int> $packageDepths |
| 76 | + */ |
| 77 | + private function calculatePackageDepths( |
| 78 | + array $allPackages, |
| 79 | + array &$packageDepths, |
| 80 | + int $depth, |
| 81 | + PackageInterface $package, |
| 82 | + bool $includingDev |
| 83 | + ): void { |
| 84 | + $name = $package->getPrettyName(); |
| 85 | + |
| 86 | + $packageProcessed = array_key_exists($name, $packageDepths); |
| 87 | + |
| 88 | + if (!$packageProcessed || $packageDepths[$name] < $depth) { |
| 89 | + $packageDepths[$name] = $depth; |
| 90 | + } |
| 91 | + |
| 92 | + // Prevent infinite loop in case of circular dependencies |
| 93 | + if ($packageProcessed) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + ++$depth; |
| 98 | + |
| 99 | + $dependencies = $includingDev |
| 100 | + ? array_keys($package->getRequires()) |
| 101 | + : [...array_keys($package->getRequires()), ...array_keys($package->getDevRequires())]; |
| 102 | + |
| 103 | + foreach ($dependencies as $dependency) { |
| 104 | + if (array_key_exists($dependency, $allPackages)) { |
| 105 | + $this->calculatePackageDepths($allPackages, $packageDepths, $depth, $allPackages[$dependency], false); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @return CompletePackage[] |
| 112 | + * |
| 113 | + * @psalm-return array<string, CompletePackage> |
| 114 | + */ |
| 115 | + private function getAllPackages(): array |
| 116 | + { |
| 117 | + $packages = $this->composer->getRepositoryManager()->getLocalRepository()->getPackages(); |
| 118 | + |
| 119 | + $result = []; |
| 120 | + foreach ($packages as $package) { |
| 121 | + if (!$package instanceof CompletePackage) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + $result[$package->getPrettyName()] = $package; |
| 125 | + } |
| 126 | + |
| 127 | + return $result; |
| 128 | + } |
| 129 | +} |
0 commit comments