From 607d491921f5b86974353f35a2b19333217533ff Mon Sep 17 00:00:00 2001 From: Adriano Ferreira Date: Fri, 10 Jan 2020 09:11:38 -0200 Subject: [PATCH] Implemented way to add packages into suggest through CLI --- src/Composer/Command/ConfigCommand.php | 12 ++++++ src/Composer/Json/JsonManipulator.php | 8 ++++ .../Test/Json/JsonManipulatorTest.php | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index ce6d2d0ba..0d408be14 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -601,6 +601,18 @@ EOT return 0; } + if (preg_match('/^suggest\.(.+)/', $settingKey, $matches)) { + if ($input->getOption('unset')) { + $this->configSource->removeProperty($settingKey); + + return 0; + } + + $this->configSource->addProperty($settingKey, $values[0]); + + return 0; + } + // handle platform if (preg_match('/^platform\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index e64a56f71..e1ee43485 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -167,6 +167,10 @@ class JsonManipulator public function addProperty($name, $value) { + if (substr($name, 0, 8) === 'suggest.') { + return $this->addSubNode('suggest', substr($name, 8), $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, 8) === 'suggest.') { + return $this->removeSubNode('suggest', substr($name, 8)); + } + if (substr($name, 0, 6) === 'extra.') { return $this->removeSubNode('extra', substr($name, 6)); } diff --git a/tests/Composer/Test/Json/JsonManipulatorTest.php b/tests/Composer/Test/Json/JsonManipulatorTest.php index 8bc7831af..914de1882 100644 --- a/tests/Composer/Test/Json/JsonManipulatorTest.php +++ b/tests/Composer/Test/Json/JsonManipulatorTest.php @@ -1814,6 +1814,46 @@ class JsonManipulatorTest extends TestCase ', $manipulator->getContents()); } + public function testAddSuggestWithPackage() + { + $manipulator = new JsonManipulator('{ + "repositories": [ + { + "type": "package", + "package": { + "authors": [], + "extra": { + "package-xml": "package.xml" + } + } + } + ], + "suggest": { + "package": "Description" + } +}'); + + $this->assertTrue($manipulator->addProperty('suggest.new-package', 'new-description')); + $this->assertEquals('{ + "repositories": [ + { + "type": "package", + "package": { + "authors": [], + "extra": { + "package-xml": "package.xml" + } + } + } + ], + "suggest": { + "package": "Description", + "new-package": "new-description" + } +} +', $manipulator->getContents()); + } + public function testAddRepositoryCanInitializeEmptyRepositories() { $manipulator = new JsonManipulator('{