Merge remote-tracking branch 'localheinz/feature/sort-packages'

main
Jordi Boggiano 9 years ago
commit 081e26f4d7

@ -92,8 +92,7 @@ class JsonManipulator
if (true === $sortPackages) {
$requirements = json_decode($links, true);
ksort($requirements);
$this->sortPackages($requirements);
$links = $this->format($requirements);
}
@ -102,6 +101,40 @@ class JsonManipulator
return true;
}
/**
* Sorts packages by importance (platform packages first, then PHP dependencies) and alphabetically.
*
* @link https://getcomposer.org/doc/02-libraries.md#platform-packages
*
* @param array $packages
*/
private function sortPackages(array &$packages = array())
{
$prefix = function ($requirement) {
return preg_replace(
array(
'/^php$/',
'/^hhvm$/',
'/^ext-\w+$/',
'/^lib-\w+$/',
'/^.+$/',
),
array(
'0-$0',
'1-$0',
'2-$0',
'3-$0',
'4-$0',
),
$requirement
);
};
uksort($packages, function ($a, $b) use ($prefix) {
return strcmp($prefix($a), $prefix($b));
});
}
public function addRepository($name, $config)
{
return $this->addSubNode('repositories', $name, $config);

@ -332,6 +332,34 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
}
'
),
array(
'{
"require": {
"foo": "baz",
"ext-mcrypt": "*",
"ext-gd": "*",
"lib-foo": "*",
"hhvm": "*",
"php": ">=5.5"
}
}',
'require',
'igorw/retry',
'*',
true,
'{
"require": {
"php": ">=5.5",
"hhvm": "*",
"ext-gd": "*",
"ext-mcrypt": "*",
"lib-foo": "*",
"foo": "baz",
"igorw/retry": "*"
}
}
',
),
);
}

Loading…
Cancel
Save