Skip to content

Commit 5872f6b

Browse files
authored
Merge pull request #58 from clue-labs/deps
Update graphp/graph API to latest development version
2 parents 5c51a59 + 6f27b25 commit 5872f6b

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"require": {
1212
"php": ">=5.3.0",
13-
"graphp/graph": "dev-master#81eef65 as 1.0.0"
13+
"graphp/graph": "dev-master#214de45 as 1.0.0"
1414
},
1515
"require-dev": {
1616
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"

examples/01-simple.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
$red->setAttribute('graphviz.color', 'red');
1414

1515
$edge = $graph->createEdgeDirected($blue, $red);
16-
$edge = $blue->createEdgeTo($red);
1716
$edge->setAttribute('graphviz.color', 'grey');
1817

1918
$graphviz = new Graphp\GraphViz\GraphViz();

src/GraphViz.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Graphp\Graph\Edge;
66
use Graphp\Graph\EdgeDirected;
77
use Graphp\Graph\Entity;
8-
use Graphp\Graph\Exception\UnexpectedValueException;
98
use Graphp\Graph\Graph;
109
use Graphp\Graph\Vertex;
1110

@@ -187,7 +186,7 @@ public function createImageHtml(Graph $graph)
187186
*
188187
* @param Graph $graph graph to display
189188
* @return string filename
190-
* @throws UnexpectedValueException on error
189+
* @throws \UnexpectedValueException on error
191190
* @uses GraphViz::createScript()
192191
*/
193192
public function createImageFile(Graph $graph)
@@ -197,20 +196,20 @@ public function createImageFile(Graph $graph)
197196

198197
$tmp = tempnam(sys_get_temp_dir(), 'graphviz');
199198
if ($tmp === false) {
200-
throw new UnexpectedValueException('Unable to get temporary file name for graphviz script');
199+
throw new \UnexpectedValueException('Unable to get temporary file name for graphviz script');
201200
}
202201

203202
$ret = file_put_contents($tmp, $script, LOCK_EX);
204203
if ($ret === false) {
205-
throw new UnexpectedValueException('Unable to write graphviz script to temporary file');
204+
throw new \UnexpectedValueException('Unable to write graphviz script to temporary file');
206205
}
207206

208207
$ret = 0;
209208

210209
$executable = $this->getExecutable();
211210
system(escapeshellarg($executable) . ' -T ' . escapeshellarg($this->format) . ' ' . escapeshellarg($tmp) . ' -o ' . escapeshellarg($tmp . '.' . $this->format), $ret);
212211
if ($ret !== 0) {
213-
throw new UnexpectedValueException('Unable to invoke "' . $executable .'" to create image file (code ' . $ret . ')');
212+
throw new \UnexpectedValueException('Unable to invoke "' . $executable .'" to create image file (code ' . $ret . ')');
214213
}
215214

216215
unlink($tmp);
@@ -229,10 +228,10 @@ public function createImageFile(Graph $graph)
229228
*/
230229
public function createScript(Graph $graph)
231230
{
232-
$directed = false;
231+
$hasDirectedEdges = false;
233232
foreach ($graph->getEdges() as $edge) {
234233
if ($edge instanceof EdgeDirected) {
235-
$directed = true;
234+
$hasDirectedEdges = true;
236235
break;
237236
}
238237
}
@@ -247,17 +246,11 @@ public function createScript(Graph $graph)
247246
$name = $this->escape($name) . ' ';
248247
}
249248

250-
$script = ($directed ? 'di':'') . 'graph ' . $name . '{' . self::EOL;
249+
$script = ($hasDirectedEdges ? 'di':'') . 'graph ' . $name . '{' . self::EOL;
251250

252251
// add global attributes
253-
$globals = array(
254-
'graph' => 'graphviz.graph.',
255-
'node' => 'graphviz.node.',
256-
'edge' => 'graphviz.edge.',
257-
);
258-
259-
foreach ($globals as $key => $prefix) {
260-
if ($layout = $this->getAttributesPrefixed($graph, $prefix)) {
252+
foreach (array('graph', 'node', 'edge') as $key) {
253+
if ($layout = $this->getAttributesPrefixed($graph, 'graphviz.' . $key . '.')) {
261254
$script .= $this->formatIndent . $key . ' ' . $this->escapeAttributes($layout) . self::EOL;
262255
}
263256
}
@@ -306,7 +299,7 @@ public function createScript(Graph $graph)
306299
$vid = $vids[\spl_object_hash($vertex)];
307300
$layout = $this->getLayoutVertex($vertex, $vid);
308301

309-
if ($layout || $vertex->getEdges()->isEmpty()) {
302+
if ($layout || !$vertex->getEdges()) {
310303
$script .= $this->formatIndent . $this->escape($vid);
311304
if ($layout) {
312305
$script .= ' ' . $this->escapeAttributes($layout);
@@ -316,20 +309,19 @@ public function createScript(Graph $graph)
316309
}
317310
}
318311

319-
$edgeop = $directed ? ' -> ' : ' -- ';
312+
$edgeop = $hasDirectedEdges ? ' -> ' : ' -- ';
320313

321314
// add all edges as directed edges
322-
foreach ($graph->getEdges() as $currentEdge) {
323-
$both = $currentEdge->getVertices()->getVector();
324-
$currentStartVertex = $both[0];
325-
$currentTargetVertex = $both[1];
315+
foreach ($graph->getEdges() as $edge) {
316+
$vertices = $edge->getVertices();
317+
assert($vertices[0] instanceof Vertex && $vertices[1] instanceof Vertex);
326318

327-
$script .= $this->formatIndent . $this->escape($vids[\spl_object_hash($currentStartVertex)]) . $edgeop . $this->escape($vids[\spl_object_hash($currentTargetVertex)]);
319+
$script .= $this->formatIndent . $this->escape($vids[\spl_object_hash($vertices[0])]) . $edgeop . $this->escape($vids[\spl_object_hash($vertices[1])]);
328320

329-
$layout = $this->getLayoutEdge($currentEdge);
321+
$layout = $this->getLayoutEdge($edge);
330322

331-
// this edge is not a loop and also points to the opposite direction => this is actually an undirected edge
332-
if ($directed && $currentStartVertex !== $currentTargetVertex && $currentEdge->isConnection($currentTargetVertex, $currentStartVertex)) {
323+
// omit arrow head if this is an undirected edge in a mixed graph
324+
if ($hasDirectedEdges && !$edge instanceof EdgeDirected) {
333325
$layout['dir'] = 'none';
334326
}
335327
if ($layout) {

0 commit comments

Comments
 (0)