5
5
use Graphp \Graph \Edge ;
6
6
use Graphp \Graph \EdgeDirected ;
7
7
use Graphp \Graph \Entity ;
8
- use Graphp \Graph \Exception \UnexpectedValueException ;
9
8
use Graphp \Graph \Graph ;
10
9
use Graphp \Graph \Vertex ;
11
10
@@ -187,7 +186,7 @@ public function createImageHtml(Graph $graph)
187
186
*
188
187
* @param Graph $graph graph to display
189
188
* @return string filename
190
- * @throws UnexpectedValueException on error
189
+ * @throws \ UnexpectedValueException on error
191
190
* @uses GraphViz::createScript()
192
191
*/
193
192
public function createImageFile (Graph $ graph )
@@ -197,20 +196,20 @@ public function createImageFile(Graph $graph)
197
196
198
197
$ tmp = tempnam (sys_get_temp_dir (), 'graphviz ' );
199
198
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 ' );
201
200
}
202
201
203
202
$ ret = file_put_contents ($ tmp , $ script , LOCK_EX );
204
203
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 ' );
206
205
}
207
206
208
207
$ ret = 0 ;
209
208
210
209
$ executable = $ this ->getExecutable ();
211
210
system (escapeshellarg ($ executable ) . ' -T ' . escapeshellarg ($ this ->format ) . ' ' . escapeshellarg ($ tmp ) . ' -o ' . escapeshellarg ($ tmp . '. ' . $ this ->format ), $ ret );
212
211
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 . ') ' );
214
213
}
215
214
216
215
unlink ($ tmp );
@@ -229,10 +228,10 @@ public function createImageFile(Graph $graph)
229
228
*/
230
229
public function createScript (Graph $ graph )
231
230
{
232
- $ directed = false ;
231
+ $ hasDirectedEdges = false ;
233
232
foreach ($ graph ->getEdges () as $ edge ) {
234
233
if ($ edge instanceof EdgeDirected) {
235
- $ directed = true ;
234
+ $ hasDirectedEdges = true ;
236
235
break ;
237
236
}
238
237
}
@@ -247,17 +246,11 @@ public function createScript(Graph $graph)
247
246
$ name = $ this ->escape ($ name ) . ' ' ;
248
247
}
249
248
250
- $ script = ($ directed ? 'di ' :'' ) . 'graph ' . $ name . '{ ' . self ::EOL ;
249
+ $ script = ($ hasDirectedEdges ? 'di ' :'' ) . 'graph ' . $ name . '{ ' . self ::EOL ;
251
250
252
251
// 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 . '. ' )) {
261
254
$ script .= $ this ->formatIndent . $ key . ' ' . $ this ->escapeAttributes ($ layout ) . self ::EOL ;
262
255
}
263
256
}
@@ -306,7 +299,7 @@ public function createScript(Graph $graph)
306
299
$ vid = $ vids [\spl_object_hash ($ vertex )];
307
300
$ layout = $ this ->getLayoutVertex ($ vertex , $ vid );
308
301
309
- if ($ layout || $ vertex ->getEdges ()-> isEmpty ()) {
302
+ if ($ layout || ! $ vertex ->getEdges ()) {
310
303
$ script .= $ this ->formatIndent . $ this ->escape ($ vid );
311
304
if ($ layout ) {
312
305
$ script .= ' ' . $ this ->escapeAttributes ($ layout );
@@ -316,20 +309,19 @@ public function createScript(Graph $graph)
316
309
}
317
310
}
318
311
319
- $ edgeop = $ directed ? ' -> ' : ' -- ' ;
312
+ $ edgeop = $ hasDirectedEdges ? ' -> ' : ' -- ' ;
320
313
321
314
// 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);
326
318
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 ] )]);
328
320
329
- $ layout = $ this ->getLayoutEdge ($ currentEdge );
321
+ $ layout = $ this ->getLayoutEdge ($ edge );
330
322
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 ) {
333
325
$ layout ['dir ' ] = 'none ' ;
334
326
}
335
327
if ($ layout ) {
0 commit comments