Skip to content

Instantly share code, notes, and snippets.

@olvlvl
Created January 16, 2014 16:18
Show Gist options
  • Save olvlvl/8457794 to your computer and use it in GitHub Desktop.
Save olvlvl/8457794 to your computer and use it in GitHub Desktop.
Package weight computing for Composer
<?php
$stable_sort = function(&$array, $picker=null)
{
static $transform, $restore;
$i = 0;
if (!$transform)
{
$transform = function(&$v, $k) use (&$i)
{
$v = array($v, ++$i, $k, $v);
};
$restore = function(&$v, $k)
{
$v = $v[3];
};
}
if ($picker)
{
array_walk
(
$array, function(&$v, $k) use (&$i, $picker)
{
$v = array($picker($v), ++$i, $k, $v);
}
);
}
else
{
array_walk($array, $transform);
}
asort($array);
array_walk($array, $restore);
};
/*
*
*/
$lock = json_decode(file_get_contents('composer.lock'), true);
$packages = array();
foreach ($lock['packages'] as $package)
{
if (empty($package['require']))
{
continue;
}
$keys = array_keys($package['require']);
$packages[$package['name']] = array_combine($keys, $keys);
}
$usage = array_combine(array_keys($packages), array_fill(0, count($packages), 0));
$count_required = function($package_id) use (&$packages, &$count_required)
{
$i = 0;
foreach ($packages as $require_id => $require)
{
if (empty($require[$package_id]))
{
continue;
}
$i += 1 + $count_required($require_id);
}
return $i;
};
foreach ($usage as $package_id => &$count)
{
$count = $count_required($package_id);
}
$sorted = $usage;
$stable_sort($sorted);
$sorted = array_reverse($sorted, true);
var_dump($sorted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment