From cc37c664ecde041f8cbbb7d0c2f8a01c94610365 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 31 Oct 2025 15:41:12 +0100 Subject: [PATCH] Leverage COMPOSER_PREFER_DEV_OVER_PRERELEASE when possible --- src/Flex.php | 14 ++++++++++---- src/PackageFilter.php | 8 ++++---- tests/PackageFilterTest.php | 4 ++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Flex.php b/src/Flex.php index 5bfa0cf1..2c59eb7a 100644 --- a/src/Flex.php +++ b/src/Flex.php @@ -11,6 +11,7 @@ namespace Symfony\Flex; +use Composer\Command\BaseConfigCommand; use Composer\Command\GlobalCommand; use Composer\Composer; use Composer\Console\Application; @@ -76,7 +77,7 @@ class Flex implements PluginInterface, EventSubscriberInterface private $operations = []; private $lock; private $displayThanksReminder = 0; - private $ignoreUnstableReleases = false; + private $ignorePreleases = false; private $reinstall; private static $activated = true; private static $aliasResolveCommands = [ @@ -182,7 +183,12 @@ class_exists(__NAMESPACE__.str_replace('/', '\\', substr($file, \strlen(__DIR__) } } - $this->ignoreUnstableReleases = $input->hasParameterOption('--prefer-lowest', true) && $input->hasParameterOption('--prefer-stable', true); + if (class_exists(BaseConfigCommand::class)) { + // composer 2.9+ + $_SERVER['COMPOSER_PREFER_DEV_OVER_PRERELEASE'] = '1'; + } else { + $this->ignorePreleases = $input->hasParameterOption('--prefer-lowest', true) && $input->hasParameterOption('--prefer-stable', true); + } $addCommand = 'add'.(method_exists($app, 'addCommand') ? 'Command' : ''); $app->$addCommand(new Command\RecipesCommand($this, $this->lock, $rfs)); @@ -195,8 +201,8 @@ class_exists(__NAMESPACE__.str_replace('/', '\\', substr($file, \strlen(__DIR__) $symfonyRequire = preg_replace('/\.x$/', '.x-dev', getenv('SYMFONY_REQUIRE') ?: ($composer->getPackage()->getExtra()['symfony']['require'] ?? '')); - if ($symfonyRequire || $this->ignoreUnstableReleases) { - $this->filter = new PackageFilter($io, $symfonyRequire, $this->downloader, $this->ignoreUnstableReleases); + if ($symfonyRequire || $this->ignorePreleases) { + $this->filter = new PackageFilter($io, $symfonyRequire, $this->downloader, $this->ignorePreleases); } } diff --git a/src/PackageFilter.php b/src/PackageFilter.php index 9ad2de5c..2f2a10d6 100644 --- a/src/PackageFilter.php +++ b/src/PackageFilter.php @@ -30,16 +30,16 @@ class PackageFilter private $symfonyConstraints; private $downloader; private $io; - private $ignoreUnstableReleases; + private $ignorePreleases; - public function __construct(IOInterface $io, string $symfonyRequire, Downloader $downloader, bool $ignoreUnstableReleases = false) + public function __construct(IOInterface $io, string $symfonyRequire, Downloader $downloader, bool $ignorePreleases = false) { $this->versionParser = new VersionParser(); $this->symfonyRequire = $symfonyRequire; $this->symfonyConstraints = '' !== $symfonyRequire ? $this->versionParser->parseConstraints($symfonyRequire) : null; $this->downloader = $downloader; $this->io = $io; - $this->ignoreUnstableReleases = $ignoreUnstableReleases; + $this->ignorePreleases = $ignorePreleases; } /** @@ -50,7 +50,7 @@ public function __construct(IOInterface $io, string $symfonyRequire, Downloader */ public function removeLegacyPackages(array $data, RootPackageInterface $rootPackage, array $lockedPackages): array { - if ($this->ignoreUnstableReleases) { + if ($this->ignorePreleases) { $filteredPackages = []; foreach ($data as $package) { if (\in_array($package->getStability(), ['stable', 'dev'], true)) { diff --git a/tests/PackageFilterTest.php b/tests/PackageFilterTest.php index 062ab337..0d076fff 100644 --- a/tests/PackageFilterTest.php +++ b/tests/PackageFilterTest.php @@ -210,7 +210,7 @@ public function provideRemoveLegacyPackages() ]]]; } - public function testIgnoreUnstableReleasesFiltersPreReleases() + public function testIgnorePreleases() { $io = new NullIO(); $downloader = $this->getMockBuilder(Downloader::class)->disableOriginalConstructor()->getMock(); @@ -229,7 +229,7 @@ public function testIgnoreUnstableReleasesFiltersPreReleases() $this->assertSame([$stablePkg, $devPkg], $result); } - public function testWithoutIgnoreUnstableReleasesKeepsAll() + public function testWithoutIgnorePreleases() { $io = new NullIO(); $downloader = $this->getMockBuilder(Downloader::class)->disableOriginalConstructor()->getMock();