Skip to content

Instantly share code, notes, and snippets.

@drupol
Last active December 16, 2019 11:52
Show Gist options
  • Save drupol/97aa6039cedd06a95a611407af926301 to your computer and use it in GitHub Desktop.
Save drupol/97aa6039cedd06a95a611407af926301 to your computer and use it in GitHub Desktop.
Composer dependency tree with PHPTree
<?php
declare(strict_types=1);
use drupol\launcher\Launcher;
use drupol\phptree\Exporter\Image;
use drupol\phptree\Importer\SimpleArray;
include './vendor/autoload.php';
// Get the graph dependency as JSON.
$jsonArray = json_decode(
shell_exec('composer show --tree -f json'),
true
);
// Build the root node.
$nodes = getNodeFromPackage(['name' => 'Root package']);
// Build the children recursively.
$nodes['children'] = array_map(
static function (array $package): array {
return getNodeFromPackage($package);
},
$jsonArray['installed']
);
// Import the array into a tree.
$tree = (new SimpleArray())->import($nodes);
// Display the full tree.
Launcher::open((new Image())->setFormat('png')->export($tree));
// Display the tree under the 7th dependency (phpspec/phpspec)
Launcher::open((new Image())->setFormat('png')->export($tree[7]));
function getChildrenOf(array $package): array
{
$package += ['requires' => []];
return array_map(
static function (array $package): array {
return getNodeFromPackage($package);
},
$package['requires']
);
}
function getNodeFromPackage(array $package): array
{
return array_filter([
'value' => $package['name'],
'children' => getChildrenOf($package),
]);
}
@drupol
Copy link
Author

drupol commented Dec 16, 2019

Here's the resulting tree:
graphvizYNNNsB

@drupol
Copy link
Author

drupol commented Dec 16, 2019

And here's the dependency graph of phpspec/phpspec.
graphvizD7p4AC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment