From 0ab843a05886850d9c442871866f713fd9adf5b8 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 13 Apr 2018 13:10:22 +0200 Subject: [PATCH] Fix setting of scripts from config command, refs #7225 --- src/Composer/Command/ConfigCommand.php | 6 +++--- src/Composer/Config/JsonConfigSource.php | 8 ++++---- src/Composer/Json/JsonManipulator.php | 12 ++++++++++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 940f5df65..4e0232844 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -613,12 +613,12 @@ EOT } // handle script - if (preg_match('/^scripts\.(.+)/', $settingKey,$matches)){ + if (preg_match('/^scripts\.(.+)/', $settingKey, $matches)){ if ($input->getOption('unset')) { - return $this->configSource->removeConfigSetting($settingKey); + return $this->configSource->removeProperty($settingKey); } - return $this->configSource->addConfigSetting($settingKey, $values[0]); + return $this->configSource->addProperty($settingKey, count($values) > 1 ? $values : $values[0]); } throw new \InvalidArgumentException('Setting '.$settingKey.' does not exist or is not supported by this command'); diff --git a/src/Composer/Config/JsonConfigSource.php b/src/Composer/Config/JsonConfigSource.php index 68de49ab3..128ebf8ec 100644 --- a/src/Composer/Config/JsonConfigSource.php +++ b/src/Composer/Config/JsonConfigSource.php @@ -135,10 +135,10 @@ class JsonConfigSource implements ConfigSourceInterface public function addProperty($name, $value) { $this->manipulateJson('addProperty', $name, $value, function (&$config, $key, $val) { - if (substr($key, 0, 6) === 'extra.') { + if (substr($key, 0, 6) === 'extra.' || substr($key, 0, 8) === 'scripts.') { $bits = explode('.', $key); $last = array_pop($bits); - $arr = &$config['extra']; + $arr = &$config[reset($bits)]; foreach ($bits as $bit) { if (!isset($arr[$bit])) { $arr[$bit] = array(); @@ -159,10 +159,10 @@ class JsonConfigSource implements ConfigSourceInterface { $authConfig = $this->authConfig; $this->manipulateJson('removeProperty', $name, function (&$config, $key) { - if (substr($key, 0, 6) === 'extra.') { + if (substr($key, 0, 6) === 'extra.' || substr($key, 0, 8) === 'scripts.') { $bits = explode('.', $key); $last = array_pop($bits); - $arr = &$config['extra']; + $arr = &$config[reset($bits)]; foreach ($bits as $bit) { if (!isset($arr[$bit])) { return; diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index 52834527b..b9dc24bb5 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -171,6 +171,10 @@ class JsonManipulator return $this->addSubNode('extra', substr($name, 6), $value); } + if (substr($name, 0, 8) === 'scripts.') { + return $this->addSubNode('scripts', substr($name, 8), $value); + } + return $this->addMainKey($name, $value); } @@ -180,6 +184,10 @@ class JsonManipulator return $this->removeSubNode('extra', substr($name, 6)); } + if (substr($name, 0, 8) === 'scripts.') { + return $this->removeSubNode('scripts', substr($name, 8)); + } + return $this->removeMainKey($name); } @@ -188,7 +196,7 @@ class JsonManipulator $decoded = JsonFile::parseJson($this->contents); $subName = null; - if (in_array($mainNode, array('config', 'extra')) && false !== strpos($name, '.')) { + if (in_array($mainNode, array('config', 'extra', 'scripts')) && false !== strpos($name, '.')) { list($name, $subName) = explode('.', $name, 2); } @@ -308,7 +316,7 @@ class JsonManipulator } $subName = null; - if (in_array($mainNode, array('config', 'extra')) && false !== strpos($name, '.')) { + if (in_array($mainNode, array('config', 'extra', 'scripts')) && false !== strpos($name, '.')) { list($name, $subName) = explode('.', $name, 2); }