|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CoenJacobs\Mozart\Composer; |
| 4 | + |
| 5 | +class InstalledPackageDependencyGraph |
| 6 | +{ |
| 7 | + private string $workingDir; |
| 8 | + |
| 9 | + public function __construct(string $workingDir) |
| 10 | + { |
| 11 | + $this->workingDir = $workingDir; |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Build a package -> required package names graph from Composer's |
| 16 | + * installed.json metadata. |
| 17 | + * |
| 18 | + * @return array<string, string[]> |
| 19 | + */ |
| 20 | + public function getDependencyGraph(): array |
| 21 | + { |
| 22 | + $packages = $this->loadInstalledPackages(); |
| 23 | + $graph = []; |
| 24 | + |
| 25 | + foreach ($packages as $package) { |
| 26 | + if (!is_array($package) || empty($package['name']) || !is_string($package['name'])) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + $graph[$package['name']] = $this->extractRequiredPackageNames($package); |
| 31 | + } |
| 32 | + |
| 33 | + return $graph; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Find processed packages that must stay in vendor because they are still |
| 38 | + * required by installed packages outside the processed set. |
| 39 | + * |
| 40 | + * @param string[] $processedPackages |
| 41 | + * @return string[] |
| 42 | + */ |
| 43 | + public function getSharedProcessedPackages(array $processedPackages): array |
| 44 | + { |
| 45 | + $graph = $this->getDependencyGraph(); |
| 46 | + $processedLookup = array_fill_keys($processedPackages, true); |
| 47 | + $sharedPackages = []; |
| 48 | + $visited = []; |
| 49 | + $queue = []; |
| 50 | + |
| 51 | + foreach (array_keys($graph) as $packageName) { |
| 52 | + if (!isset($processedLookup[$packageName])) { |
| 53 | + $queue[] = $packageName; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + while (!empty($queue)) { |
| 58 | + $packageName = array_shift($queue); |
| 59 | + |
| 60 | + if (isset($visited[$packageName])) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + $visited[$packageName] = true; |
| 65 | + |
| 66 | + foreach ($graph[$packageName] ?? [] as $dependency) { |
| 67 | + if (isset($processedLookup[$dependency])) { |
| 68 | + $sharedPackages[$dependency] = true; |
| 69 | + } |
| 70 | + |
| 71 | + if (!isset($visited[$dependency])) { |
| 72 | + $queue[] = $dependency; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return array_keys($sharedPackages); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array<int, array<string, mixed>> |
| 82 | + */ |
| 83 | + private function loadInstalledPackages(): array |
| 84 | + { |
| 85 | + $installedPath = $this->workingDir |
| 86 | + . DIRECTORY_SEPARATOR . 'vendor' |
| 87 | + . DIRECTORY_SEPARATOR . 'composer' |
| 88 | + . DIRECTORY_SEPARATOR . 'installed.json'; |
| 89 | + |
| 90 | + if (!is_readable($installedPath)) { |
| 91 | + return []; |
| 92 | + } |
| 93 | + |
| 94 | + $installedJson = file_get_contents($installedPath); |
| 95 | + if ($installedJson === false) { |
| 96 | + return []; |
| 97 | + } |
| 98 | + |
| 99 | + $decoded = json_decode($installedJson, true); |
| 100 | + if (!is_array($decoded)) { |
| 101 | + return []; |
| 102 | + } |
| 103 | + |
| 104 | + if (isset($decoded['packages']) && is_array($decoded['packages'])) { |
| 105 | + return $decoded['packages']; |
| 106 | + } |
| 107 | + |
| 108 | + if (array_is_list($decoded)) { |
| 109 | + return $decoded; |
| 110 | + } |
| 111 | + |
| 112 | + return []; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param array<string, mixed> $package |
| 117 | + * @return string[] |
| 118 | + */ |
| 119 | + private function extractRequiredPackageNames(array $package): array |
| 120 | + { |
| 121 | + $requires = $package['require'] ?? []; |
| 122 | + if (!is_array($requires)) { |
| 123 | + return []; |
| 124 | + } |
| 125 | + |
| 126 | + return array_values(array_filter(array_keys($requires), function ($require): bool { |
| 127 | + return is_string($require) && str_contains($require, '/'); |
| 128 | + })); |
| 129 | + } |
| 130 | +} |
0 commit comments