diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index 9505a8df9..fd37f681d 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -119,8 +119,8 @@ class JsonManipulator } // child exists - if (preg_match('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', $children, $matches)) { - $children = preg_replace('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', '${1}'.$this->format($value, 1).'$3', $children); + if (preg_match('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', $children, $matches)) { + $children = preg_replace('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', '${1}'.$this->format($value, 1).'$3', $children); } elseif (preg_match('#[^\s](\s*)$#', $children, $match)) { // child missing but non empty children $children = preg_replace( @@ -164,7 +164,7 @@ class JsonManipulator } if (preg_match('{"'.preg_quote($name).'"\s*:}i', $children)) { - if (preg_match_all('{"'.preg_quote($name).'"\s*:\s*(?:[0-9.]+|null|true|false|"[^"]+"|\{'.self::$RECURSE_BLOCKS.'\})}', $children, $matches)) { + if (preg_match_all('{"'.preg_quote($name).'"\s*:\s*(?:[0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})}', $children, $matches)) { $bestMatch = ''; foreach ($matches[0] as $match) { if (strlen($bestMatch) < strlen($match)) { @@ -215,6 +215,10 @@ class JsonManipulator reset($data); if (is_numeric(key($data))) { + foreach ($data as $key => $val) { + $data[$key] = $this->format($val, $depth + 1); + } + return '['.implode(', ', $data).']'; } diff --git a/tests/Composer/Test/Json/JsonManipulatorTest.php b/tests/Composer/Test/Json/JsonManipulatorTest.php index b0971ce51..8679a7508 100644 --- a/tests/Composer/Test/Json/JsonManipulatorTest.php +++ b/tests/Composer/Test/Json/JsonManipulatorTest.php @@ -444,6 +444,41 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase "foo": 50 } } +', $manipulator->getContents()); + } + + public function testAddConfigSettingCanOverwriteArrays() + { + $manipulator = new JsonManipulator('{ + "config": { + "github-oauth": { + "github.com": "foo" + }, + "github-protocols": ["https"] + } +}'); + + $this->assertTrue($manipulator->addConfigSetting('github-protocols', array('https', 'http'))); + $this->assertEquals('{ + "config": { + "github-oauth": { + "github.com": "foo" + }, + "github-protocols": ["https", "http"] + } +} +', $manipulator->getContents()); + + $this->assertTrue($manipulator->addConfigSetting('github-oauth', array('github.com' => 'bar', 'alt.example.org' => 'baz'))); + $this->assertEquals('{ + "config": { + "github-oauth": { + "github.com": "bar", + "alt.example.org": "baz" + }, + "github-protocols": ["https", "http"] + } +} ', $manipulator->getContents()); } }