|
8 | 8 | use Composer\Plugin\PluginInterface;
|
9 | 9 | use Composer\Script\Event;
|
10 | 10 | use Composer\Script\ScriptEvents;
|
| 11 | +use Composer\Semver\Constraint\MultiConstraint; |
| 12 | +use Composer\Semver\Intervals; |
11 | 13 | use Composer\Util\Filesystem;
|
| 14 | +use function array_key_exists; |
12 | 15 | use function array_keys;
|
| 16 | +use function class_exists; |
13 | 17 | use function dirname;
|
14 | 18 | use function file_exists;
|
15 | 19 | use function file_put_contents;
|
@@ -45,6 +49,9 @@ final class GeneratedConfig
|
45 | 49 |
|
46 | 50 | public const NOT_INSTALLED = %s;
|
47 | 51 |
|
| 52 | + /** @var string|null */ |
| 53 | + public const PHPSTAN_VERSION_CONSTRAINT = %s; |
| 54 | +
|
48 | 55 | private function __construct()
|
49 | 56 | {
|
50 | 57 | }
|
@@ -110,6 +117,8 @@ public function process(Event $event): void
|
110 | 117 | $ignore = $packageExtra['phpstan/extension-installer']['ignore'];
|
111 | 118 | }
|
112 | 119 |
|
| 120 | + $phpstanVersionConstraints = []; |
| 121 | + |
113 | 122 | foreach ($composer->getRepositoryManager()->getLocalRepository()->getPackages() as $package) {
|
114 | 123 | if (
|
115 | 124 | $package->getType() !== 'phpstan-extension'
|
@@ -151,14 +160,36 @@ public function process(Event $event): void
|
151 | 160 | ];
|
152 | 161 |
|
153 | 162 | $installedPackages[$package->getName()] = true;
|
| 163 | + |
| 164 | + $packageRequires = $package->getRequires(); |
| 165 | + if (array_key_exists('phpstan/phpstan', $packageRequires)) { |
| 166 | + $phpstanVersionConstraints[] = $packageRequires['phpstan/phpstan']->getConstraint(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + $phpstanVersionConstraint = null; |
| 171 | + if (count($phpstanVersionConstraints) > 0 && class_exists(Intervals::class)) { |
| 172 | + if (count($phpstanVersionConstraints) === 1) { |
| 173 | + $multiConstraint = $phpstanVersionConstraints[0]; |
| 174 | + } else { |
| 175 | + $multiConstraint = new MultiConstraint($phpstanVersionConstraints); |
| 176 | + } |
| 177 | + $compactedConstraint = Intervals::compactConstraint($multiConstraint); |
| 178 | + $phpstanVersionConstraint = sprintf( |
| 179 | + '%s%s && %s%s', |
| 180 | + $compactedConstraint->getLowerBound()->isInclusive() ? '>=' : '>', |
| 181 | + $compactedConstraint->getLowerBound()->getVersion(), |
| 182 | + $compactedConstraint->getUpperBound()->isInclusive() ? '<=' : '<', |
| 183 | + $compactedConstraint->getUpperBound()->getVersion() |
| 184 | + ); |
154 | 185 | }
|
155 | 186 |
|
156 | 187 | ksort($data);
|
157 | 188 | ksort($installedPackages);
|
158 | 189 | ksort($notInstalledPackages);
|
159 | 190 | sort($ignoredPackages);
|
160 | 191 |
|
161 |
| - $generatedConfigFileContents = sprintf(self::$generatedFileTemplate, var_export($data, true), var_export($notInstalledPackages, true)); |
| 192 | + $generatedConfigFileContents = sprintf(self::$generatedFileTemplate, var_export($data, true), var_export($notInstalledPackages, true), var_export($phpstanVersionConstraint, true)); |
162 | 193 | file_put_contents($generatedConfigFilePath, $generatedConfigFileContents);
|
163 | 194 | $io->write('<info>phpstan/extension-installer:</info> Extensions installed');
|
164 | 195 |
|
|
0 commit comments