From 6ddd8d4ec755b2fe9056d20b5437343cd2feb7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Mo=CC=88ller?= Date: Sat, 13 Dec 2014 00:17:14 +0100 Subject: [PATCH] Enhancement: Add sort-packages option which allows sorting of packages --- src/Composer/Command/RequireCommand.php | 9 ++-- src/Composer/Json/JsonManipulator.php | 9 +++- .../Test/Json/JsonManipulatorTest.php | 52 +++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php index 55e3752a9..91043ad71 100644 --- a/src/Composer/Command/RequireCommand.php +++ b/src/Composer/Command/RequireCommand.php @@ -47,6 +47,7 @@ class RequireCommand extends InitCommand new InputOption('update-no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'), new InputOption('update-with-dependencies', null, InputOption::VALUE_NONE, 'Allows inherited dependencies to be updated with explicit dependencies.'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'), + new InputOption('sort-packages', null, InputOption::VALUE_NONE, 'Sorts packages when adding/updating a new dependency'), )) ->setHelp(<<parseConstraints($constraint); } - if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey, $removeKey)) { + $sortPackages = $input->getOption('sort-packages'); + + if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey, $removeKey, $sortPackages)) { foreach ($requirements as $package => $version) { $baseRequirements[$package] = $version; @@ -160,14 +163,14 @@ EOT return $status; } - private function updateFileCleanly($json, array $base, array $new, $requireKey, $removeKey) + private function updateFileCleanly($json, array $base, array $new, $requireKey, $removeKey, $sortPackages) { $contents = file_get_contents($json->getPath()); $manipulator = new JsonManipulator($contents); foreach ($new as $package => $constraint) { - if (!$manipulator->addLink($requireKey, $package, $constraint)) { + if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) { return false; } if (!$manipulator->removeSubNode($removeKey, $package)) { diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index dd8c49ebd..751e9021e 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -52,7 +52,7 @@ class JsonManipulator return $this->contents . $this->newline; } - public function addLink($type, $package, $constraint) + public function addLink($type, $package, $constraint, $sortPackages = false) { $decoded = JsonFile::parseJson($this->contents); @@ -90,6 +90,13 @@ class JsonManipulator } } + if (true === $sortPackages) { + $requirements = json_decode($links, true); + + ksort($requirements); + $links = $this->format($requirements); + } + $this->contents = $matches[1] . $matches[2] . $links . $matches[4]; return true; diff --git a/tests/Composer/Test/Json/JsonManipulatorTest.php b/tests/Composer/Test/Json/JsonManipulatorTest.php index 1a0f976b5..7ffaaabc7 100644 --- a/tests/Composer/Test/Json/JsonManipulatorTest.php +++ b/tests/Composer/Test/Json/JsonManipulatorTest.php @@ -281,6 +281,58 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase ); } + /** + * @dataProvider providerAddLinkAndSortPackages + */ + public function testAddLinkAndSortPackages($json, $type, $package, $constraint, $sortPackages, $expected) + { + $manipulator = new JsonManipulator($json); + $this->assertTrue($manipulator->addLink($type, $package, $constraint, $sortPackages)); + $this->assertEquals($expected, $manipulator->getContents()); + } + + public function providerAddLinkAndSortPackages() + { + return array( + array( + '{ + "require": { + "vendor/baz": "qux" + } +}', + 'require', + 'foo', + 'bar', + true, + '{ + "require": { + "foo": "bar", + "vendor/baz": "qux" + } +} +' + ), + array( + '{ + "require": { + "vendor/baz": "qux" + } +}', + 'require', + 'foo', + 'bar', + false, + '{ + "require": { + "vendor/baz": "qux", + "foo": "bar" + } +} +' + ), + ); + } + /** * @dataProvider removeSubNodeProvider */