From e43cae6231f2f9dc02f3f23b86c660bc4103c3df Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 20 Jul 2022 22:29:27 +0200 Subject: [PATCH] Add COMPOSER_PREFER_STABLE and COMPOSER_PREFER_LOWEST env vars, fixes #10919 --- doc/03-cli.md | 24 +++++++++++++++++++----- src/Composer/Command/BaseCommand.php | 13 ++++++++++--- src/Composer/Command/RequireCommand.php | 4 ++-- src/Composer/Command/UpdateCommand.php | 4 ++-- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index 1ff39c67f..2d53490dd 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -218,9 +218,11 @@ and this feature is only available for your root package dependencies. a `+` makes it only ignore the upper-bound of the requirements. For example, if a package requires `php: ^7`, then the option `--ignore-platform-req=php+` would allow installing on PHP 8, but installation on PHP 5.6 would still fail. -* **--prefer-stable:** Prefer stable versions of dependencies. +* **--prefer-stable:** Prefer stable versions of dependencies. Can also be set via the + COMPOSER_PREFER_STABLE=1 env var. * **--prefer-lowest:** Prefer lowest versions of dependencies. Useful for testing minimal - versions of requirements, generally used with `--prefer-stable`. + versions of requirements, generally used with `--prefer-stable`. Can also be set via the + COMPOSER_PREFER_LOWEST=1 env var. * **--interactive:** Interactive interface with autocompletion to select the packages to update. * **--root-reqs:** Restricts the update to your first degree dependencies. @@ -275,9 +277,11 @@ If you do not specify a package, Composer will prompt you to search for a packag * **--ignore-platform-req:** ignore a specific platform requirement(`php`, `hhvm`, `lib-*` and `ext-*`) and force the installation even if the local machine does not fulfill it. Multiple requirements can be ignored via wildcard. -* **--prefer-stable:** Prefer stable versions of dependencies. +* **--prefer-stable:** Prefer stable versions of dependencies. Can also be set via the + COMPOSER_PREFER_STABLE=1 env var. * **--prefer-lowest:** Prefer lowest versions of dependencies. Useful for testing minimal - versions of requirements, generally used with `--prefer-stable`. + versions of requirements, generally used with `--prefer-stable`. Can also be set via the + COMPOSER_PREFER_LOWEST=1 env var. * **--sort-packages:** Keep packages sorted in `composer.json`. * **--optimize-autoloader (-o):** Convert PSR-0/4 autoloading to classmap to get a faster autoloader. This is recommended especially for production, but @@ -1233,9 +1237,19 @@ useful for plugin authors to identify what is firing when exactly. ### COMPOSER_NO_DEV -If set to `1`, it is the equivalent of passing the `--no-dev` argument to `install` or +If set to `1`, it is the equivalent of passing the `--no-dev` option to `install` or `update`. You can override this for a single command by setting `COMPOSER_NO_DEV=0`. +### COMPOSER_PREFER_STABLE + +If set to `1`, it is the equivalent of passing the `--prefer-stable` option to +`update` or `require`. + +### COMPOSER_PREFER_LOWEST + +If set to `1`, it is the equivalent of passing the `--prefer-lowest` option to +`update` or `require`. + ### COMPOSER_IGNORE_PLATFORM_REQ or COMPOSER_IGNORE_PLATFORM_REQS If `COMPOSER_IGNORE_PLATFORM_REQS` set to `1`, it is the equivalent of passing the `--ignore-platform-reqs` argument. diff --git a/src/Composer/Command/BaseCommand.php b/src/Composer/Command/BaseCommand.php index 75048a672..5198f06bf 100644 --- a/src/Composer/Command/BaseCommand.php +++ b/src/Composer/Command/BaseCommand.php @@ -243,9 +243,16 @@ abstract class BaseCommand extends Command $input->setOption('no-progress', true); } - if (true === $input->hasOption('no-dev')) { - if (!$input->getOption('no-dev') && (bool) Platform::getEnv('COMPOSER_NO_DEV')) { - $input->setOption('no-dev', true); + $envOptions = [ + 'COMPOSER_NO_DEV' => 'no-dev', + 'COMPOSER_PREFER_STABLE' => 'prefer-stable', + 'COMPOSER_PREFER_LOWEST' => 'prefer-lowest', + ]; + foreach ($envOptions as $envName => $optionName) { + if (true === $input->hasOption($optionName)) { + if (false === $input->getOption($optionName) && (bool) Platform::getEnv($envName)) { + $input->setOption($optionName, true); + } } } diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php index 08339998f..b648166db 100644 --- a/src/Composer/Command/RequireCommand.php +++ b/src/Composer/Command/RequireCommand.php @@ -90,8 +90,8 @@ class RequireCommand extends BaseCommand new InputOption('with-all-dependencies', null, InputOption::VALUE_NONE, 'Alias for --update-with-all-dependencies'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), - new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies.'), - new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies.'), + new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies (can also be set via the COMPOSER_PREFER_STABLE=1 env var).'), + new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies (can also be set via the COMPOSER_PREFER_LOWEST=1 env var).'), new InputOption('sort-packages', null, InputOption::VALUE_NONE, 'Sorts packages when adding/updating a new dependency'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index 51b32ede8..243872d6a 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -75,8 +75,8 @@ class UpdateCommand extends BaseCommand new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), - new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies.'), - new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies.'), + new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies (can also be set via the COMPOSER_PREFER_STABLE=1 env var).'), + new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies (can also be set via the COMPOSER_PREFER_LOWEST=1 env var).'), new InputOption('interactive', 'i', InputOption::VALUE_NONE, 'Interactive interface with autocompletion to select the packages to update.'), new InputOption('root-reqs', null, InputOption::VALUE_NONE, 'Restricts the update to your first degree dependencies.'), ))