diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index d30a68385..67020ffad 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -126,18 +126,22 @@ class JsonManipulator { $decoded = JsonFile::parseJson($this->contents); - // no main node yet - if (!isset($decoded[$mainNode])) { - $this->addMainKey($mainNode, array($name => $value)); - - return true; - } - $subName = null; if (in_array($mainNode, array('config', 'repositories')) && false !== strpos($name, '.')) { list($name, $subName) = explode('.', $name, 2); } + // no main node yet + if (!isset($decoded[$mainNode])) { + if ($subName !== null) { + $this->addMainKey($mainNode, array($name => array($subName => $value))); + } else { + $this->addMainKey($mainNode, array($name => $value)); + } + + return true; + } + // main node content not match-able $nodeRegex = '{^(\s*\{\s*(?:'.self::$JSON_STRING.'\s*:\s*'.self::$JSON_VALUE.'\s*,\s*)*?)'. '('.preg_quote(JsonFile::encode($mainNode)).'\s*:\s*\{)('.self::$RECURSE_BLOCKS.')(\})(.*)}s'; diff --git a/tests/Composer/Test/Json/JsonManipulatorTest.php b/tests/Composer/Test/Json/JsonManipulatorTest.php index d8e564346..26c82f28e 100644 --- a/tests/Composer/Test/Json/JsonManipulatorTest.php +++ b/tests/Composer/Test/Json/JsonManipulatorTest.php @@ -803,6 +803,22 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase ', $manipulator->getContents()); } + public function testAddConfigSettingWorksFromScratch() + { + $manipulator = new JsonManipulator('{ +}'); + + $this->assertTrue($manipulator->addConfigSetting('foo.bar', 'baz')); + $this->assertEquals('{ + "config": { + "foo": { + "bar": "baz" + } + } +} +', $manipulator->getContents()); + } + public function testAddConfigSettingCanAdd() { $manipulator = new JsonManipulator('{