From 5b42944a6b161fd1e170d9a47e096176240dd1f1 Mon Sep 17 00:00:00 2001 From: Kyrylo Kostiukov Date: Wed, 5 Apr 2023 13:15:22 +0200 Subject: [PATCH 1/2] Add possibility to filter by composer vendor names --- src/Command/Export.php | 7 ++++++- src/Graph/GraphComposer.php | 31 ++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) 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..093ef51 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, $vendors)) continue; + $name = $package->getName(); $start = $graph->createVertex($name, true); @@ -109,9 +116,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 +129,22 @@ public function setFormat($format) return $this; } + + protected function filterByVendors(\JMS\Composer\Graph\PackageNode $package, array $vendors = null) + { + $packageName = $package->getName(); + + 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; + } } From 265d10d237b1571757a4a05df568552a36f3dabb Mon Sep 17 00:00:00 2001 From: Kyrylo Kostiukov Date: Wed, 5 Apr 2023 13:30:49 +0200 Subject: [PATCH 2/2] Add possibility to filter by composer vendor names (filter also the dependencies) --- src/Graph/GraphComposer.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Graph/GraphComposer.php b/src/Graph/GraphComposer.php index 093ef51..861015e 100644 --- a/src/Graph/GraphComposer.php +++ b/src/Graph/GraphComposer.php @@ -68,7 +68,7 @@ public function createGraph($filters = array()) /** @var \JMS\Composer\Graph\PackageNode $package */ foreach ($this->dependencyGraph->getPackages() as $package) { - if ($this->filterByVendors($package, $vendors)) continue; + if ($this->filterByVendors($package->getName(), $vendors)) continue; $name = $package->getName(); $start = $graph->createVertex($name, true); @@ -82,6 +82,7 @@ public function createGraph($filters = array()) foreach ($package->getOutEdges() as $requires) { $targetName = $requires->getDestPackage()->getName(); + if ($this->filterByVendors($targetName, $vendors)) continue; $target = $graph->createVertex($targetName, true); $label = $requires->getVersionConstraint(); @@ -130,10 +131,8 @@ public function setFormat($format) return $this; } - protected function filterByVendors(\JMS\Composer\Graph\PackageNode $package, array $vendors = null) + protected function filterByVendors($packageName, array $vendors = null) { - $packageName = $package->getName(); - if (strpos($packageName, '/') !== false) { $vendorName = strstr($packageName, '/', true); } else {