소스 검색

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`.
Adriano Ferreira 5 년 전
부모
커밋
5ea6fd0bcb
3개의 변경된 파일91개의 추가작업 그리고 8개의 파일을 삭제
  1. 38 8
      src/Composer/Command/ConfigCommand.php
  2. 8 0
      src/Composer/Json/JsonManipulator.php
  3. 45 0
      tests/Composer/Test/Json/JsonManipulatorTest.php

+ 38 - 8
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(

+ 8 - 0
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));
         }

+ 45 - 0
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('{