Skip to content

Commit 3b7477f

Browse files
committed
Add option to export graph into image file
1 parent 39cfb3f commit 3b7477f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

bin/graph-composer

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ require_once __DIR__ . '/../vendor/autoload.php';
77
$dir = isset($argv[1]) ? $argv[1] : '.';
88

99
$graph = new Clue\GraphComposer($dir);
10-
$graph->displayGraph();
10+
11+
if (isset($argv[2])) {
12+
$graph->exportGraph($argv[2]);
13+
} else {
14+
$graph->displayGraph();
15+
}

src/Clue/GraphComposer.php

+23
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,27 @@ public function displayGraph()
8787
$graphviz->setFormat('svg');
8888
$graphviz->display();
8989
}
90+
91+
public function exportGraph($target)
92+
{
93+
$graph = $this->createGraph();
94+
95+
if (is_dir($target)) {
96+
$target = rtrim($target, '/') . '/graph-composer.svg';
97+
}
98+
99+
$filename = basename($target);
100+
$format = 'svg';
101+
$pos = strrpos($filename, '.');
102+
if ($pos !== false && isset($filename[$pos + 1])) {
103+
// extension found and not empty
104+
$format = substr($filename, $pos + 1);
105+
}
106+
107+
$graphviz = new GraphViz($graph);
108+
$graphviz->setFormat($format);
109+
$temp = $graphviz->createImageFile();
110+
111+
rename($temp, $target);
112+
}
90113
}

0 commit comments

Comments
 (0)