1
0
Fork 0

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

pull/3991/head
Jordi Boggiano 2015-05-01 16:08:57 +01:00
commit 081e26f4d7
2 changed files with 63 additions and 2 deletions

View File

@ -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);

View File

@ -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": "*"
}
}
',
),
);
}