From 5ea6fd0bcbb820daf79c4ff8dae24073b4ef8604 Mon Sep 17 00:00:00 2001 From: Adriano Ferreira Date: Tue, 7 Jan 2020 14:14:27 -0200 Subject: [PATCH] Implemented way to use preferred-install for defining granular preferences through CLI Currently, preferred-install accepts the hash of patterns as the value in the composer.json. I've followed the same approach as used in extra and platform for letting the user define install preferences through CLI in the format: `composer config preferred-install my-organization/stable-package.dist`. --- src/Composer/Command/ConfigCommand.php | 46 +++++++++++++++---- src/Composer/Json/JsonManipulator.php | 8 ++++ .../Test/Json/JsonManipulatorTest.php | 45 ++++++++++++++++++ 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index ce6d2d0ba..3577b336e 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -306,14 +306,6 @@ EOT 'process-timeout' => array('is_numeric', 'intval'), 'use-include-path' => array($booleanValidator, $booleanNormalizer), 'use-github-api' => array($booleanValidator, $booleanNormalizer), - 'preferred-install' => array( - function ($val) { - return in_array($val, array('auto', 'source', 'dist'), true); - }, - function ($val) { - return $val; - }, - ), 'store-auths' => array( function ($val) { return in_array($val, array('true', 'false', 'prompt'), true); @@ -458,6 +450,16 @@ EOT }, ), ); + $uniqueOrDotNestedArray = array( + 'preferred-install' => array( + function ($val) { + return in_array($val, array('auto', 'source', 'dist'), true); + }, + function ($val) { + return $val; + }, + ), + ); if ($input->getOption('unset') && (isset($uniqueConfigValues[$settingKey]) || isset($multiConfigValues[$settingKey]))) { if ($settingKey === 'disable-tls' && $this->config->get('disable-tls')) { @@ -478,6 +480,34 @@ EOT return 0; } + if (isset($uniqueOrDotNestedArray[$settingKey])) { + + try { + $this->handleSingleValue($settingKey, $uniqueOrDotNestedArray[$settingKey], $values, 'addConfigSetting'); + } catch ( \RuntimeException $e ) { + + if ( $input->getOption( 'unset' ) ) { + $this->configSource->removeProperty( $settingKey ); + + return 0; + } + + $valueData = explode( '.', $values[0] ); + + if ( ! isset( $valueData[0], $valueData[1] ) ) { + throw new \RuntimeException( 'Invalid pattern format. It should be my-organization/stable-package.dist' ); + } + + list( $validator, $normalizer ) = $uniqueOrDotNestedArray[ $settingKey ]; + if ( ! $validator( $valueData[1] ) ) { + throw new \RuntimeException( 'Invalid option for install method. It accepts only: auto, source, and dist' ); + } + + $this->configSource->addProperty( 'config.' . $settingKey . '.' . $valueData[0], $valueData[1] ); + } + + return 0; + } // handle properties $uniqueProps = array( diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index e64a56f71..8c6efd325 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -167,6 +167,10 @@ class JsonManipulator public function addProperty($name, $value) { + if ( substr( $name, 0, 7 ) === 'config.' ) { + return $this->addConfigSetting( substr( $name, 7 ), $value ); + } + if (substr($name, 0, 6) === 'extra.') { return $this->addSubNode('extra', substr($name, 6), $value); } @@ -180,6 +184,10 @@ class JsonManipulator public function removeProperty($name) { + if ( substr( $name, 0, 7 ) === 'config.' ) { + return $this->removeConfigSetting( substr( $name, 7 ) ); + } + if (substr($name, 0, 6) === 'extra.') { return $this->removeSubNode('extra', substr($name, 6)); } diff --git a/tests/Composer/Test/Json/JsonManipulatorTest.php b/tests/Composer/Test/Json/JsonManipulatorTest.php index 8bc7831af..4f70e8669 100644 --- a/tests/Composer/Test/Json/JsonManipulatorTest.php +++ b/tests/Composer/Test/Json/JsonManipulatorTest.php @@ -1814,6 +1814,51 @@ class JsonManipulatorTest extends TestCase ', $manipulator->getContents()); } + public function testAddConfigWithPackage() { + $manipulator = new JsonManipulator('{ + "repositories": [ + { + "type": "package", + "package": { + "authors": [], + "extra": { + "package-xml": "package.xml" + } + } + } + ], + "config": { + "platform": { + "php": "5.3.9" + } + } +}'); + + $this->assertTrue($manipulator->addProperty('config.preferred-install.my-organization/stable-package', 'dist')); + $this->assertEquals('{ + "repositories": [ + { + "type": "package", + "package": { + "authors": [], + "extra": { + "package-xml": "package.xml" + } + } + } + ], + "config": { + "platform": { + "php": "5.3.9" + }, + "preferred-install": { + "my-organization/stable-package": "dist" + } + } +} +', $manipulator->getContents()); + } + public function testAddRepositoryCanInitializeEmptyRepositories() { $manipulator = new JsonManipulator('{