1
0
Fork 0

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`.
pull/8517/head
Adriano Ferreira 2020-01-07 14:14:27 -02:00
parent 6034c2af01
commit 5ea6fd0bcb
3 changed files with 91 additions and 8 deletions

View File

@ -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(

View File

@ -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));
}

View File

@ -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('{