Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to filter by composer vendor names #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Command/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ protected function configure()
// add output format option. default value MUST NOT be given, because default is to overwrite with output extension
->addOption('format', null, InputOption::VALUE_REQUIRED, 'Image format (svg, png, jpeg)'/*, 'svg'*/)

// add output format option. default value MUST NOT be given, because default is to overwrite with output extension
->addOption('vendors', null, InputOption::VALUE_REQUIRED, 'List of Vendor names to be displayed on the graph, separated by comma'/*, 'svg'*/)

/*->addOption('dev', null, InputOption::VALUE_NONE, 'If set, Whether require-dev dependencies should be shown') */;
}

Expand All @@ -47,7 +50,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$graph->setFormat($format);
}

$path = $graph->getImagePath();
$vendors = $input->getOption('vendors');

$path = $graph->getImagePath(array('vendors' => $vendors));

if ($target !== null) {
rename($path, $target);
Expand Down
30 changes: 27 additions & 3 deletions src/Graph/GraphComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@ public function __construct($dir, GraphViz $graphviz = null)
/**
*
* @param string $dir
* @param array $filters
* @return \Fhaculty\Graph\Graph
*/
public function createGraph()
public function createGraph($filters = array())
{
$graph = new Graph();

$vendors = isset($filters['vendors']) ? explode(',', $filters['vendors']) : null;

/** @var \JMS\Composer\Graph\PackageNode $package */
foreach ($this->dependencyGraph->getPackages() as $package) {

if ($this->filterByVendors($package->getName(), $vendors)) continue;

$name = $package->getName();
$start = $graph->createVertex($name, true);

Expand All @@ -75,6 +82,7 @@ public function createGraph()

foreach ($package->getOutEdges() as $requires) {
$targetName = $requires->getDestPackage()->getName();
if ($this->filterByVendors($targetName, $vendors)) continue;
$target = $graph->createVertex($targetName, true);

$label = $requires->getVersionConstraint();
Expand Down Expand Up @@ -109,9 +117,9 @@ public function displayGraph()
$this->graphviz->display($graph);
}

public function getImagePath()
public function getImagePath($filters = array())
{
$graph = $this->createGraph();
$graph = $this->createGraph($filters);

return $this->graphviz->createImageFile($graph);
}
Expand All @@ -122,4 +130,20 @@ public function setFormat($format)

return $this;
}

protected function filterByVendors($packageName, array $vendors = null)
{
if (strpos($packageName, '/') !== false) {
$vendorName = strstr($packageName, '/', true);
} else {
// if the package name has "/" in it, everything before "/" should be considered as vendor,
// if not - the whole name should be considered as vendor.
$vendorName = $packageName;
}

if (!is_null($vendors) && !in_array($vendorName, $vendors)) {
return true;
}
return false;
}
}