diff --git a/src/Command/Export.php b/src/Command/Export.php index 3c1d04f..7b92d21 100644 --- a/src/Command/Export.php +++ b/src/Command/Export.php @@ -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') */; } @@ -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); diff --git a/src/Graph/GraphComposer.php b/src/Graph/GraphComposer.php index 0dea8fd..861015e 100644 --- a/src/Graph/GraphComposer.php +++ b/src/Graph/GraphComposer.php @@ -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); @@ -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(); @@ -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); } @@ -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; + } }