Last active
December 16, 2019 11:52
-
-
Save drupol/97aa6039cedd06a95a611407af926301 to your computer and use it in GitHub Desktop.
Composer dependency tree with PHPTree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the resulting tree:
