7
7
use drupol \phptree \Node \NodeInterface ;
8
8
use drupol \phptree \Visitor \VisitorInterface ;
9
9
use Fhaculty \Graph \Graph ;
10
+ use Graphp \GraphViz \GraphViz as OriginalGraphViz ;
10
11
11
12
/**
12
13
* Class GraphViz
@@ -23,11 +24,6 @@ class GraphViz implements RendererInterface
23
24
*/
24
25
private $ graph ;
25
26
26
- /**
27
- * @var array
28
- */
29
- private $ nodes ;
30
-
31
27
/**
32
28
* @var \Graphp\GraphViz\GraphViz
33
29
*/
@@ -38,12 +34,11 @@ class GraphViz implements RendererInterface
38
34
*
39
35
* @param \drupol\phptree\Visitor\VisitorInterface $visitor
40
36
*/
41
- public function __construct (VisitorInterface $ visitor )
37
+ public function __construct (VisitorInterface $ visitor, Graph $ graph , OriginalGraphViz $ graphViz )
42
38
{
43
39
$ this ->visitor = $ visitor ;
44
- $ this ->graph = new Graph ();
45
- $ this ->graphviz = new \Graphp \GraphViz \GraphViz ();
46
- $ this ->nodes = [];
40
+ $ this ->graph = $ graph ;
41
+ $ this ->graphviz = $ graphViz ;
47
42
}
48
43
49
44
/**
@@ -53,36 +48,60 @@ public function __construct(VisitorInterface $visitor)
53
48
*/
54
49
public function render (NodeInterface $ node ): string
55
50
{
56
- foreach ($ this ->visitor ->traverse ($ node ) as $ child ) {
57
- $ hash_parent = $ this ->hash ($ child );
51
+ return $ this ->graphviz ->createScript ($ this ->getGraph ($ node ));
52
+ }
53
+
54
+ /**
55
+ * @param \drupol\phptree\Node\NodeInterface $node
56
+ *
57
+ * @return \Fhaculty\Graph\Graph
58
+ */
59
+ public function getGraph (NodeInterface $ node ): Graph
60
+ {
61
+ foreach ($ this ->visitor ->traverse ($ node ) as $ node_visited ) {
62
+ /** @var int $hash */
63
+ $ hash = $ this ->hash ($ node_visited );
58
64
59
- if (! isset ( $ this ->nodes [ $ hash_parent ] )) {
60
- $ this ->nodes [ $ hash_parent ] = $ this -> graph ->createVertex ($ hash_parent );
65
+ if (false === $ this ->graph -> hasVertex ( $ hash )) {
66
+ $ this ->graph ->createVertex ($ hash );
61
67
}
62
68
63
- if (null === $ parent = $ child ->getParent ()) {
69
+ if (null === $ parent = $ node_visited ->getParent ()) {
64
70
continue ;
65
71
}
66
72
67
- $ hash = $ this ->hash ($ parent );
73
+ /** @var int $hash_parent */
74
+ $ hash_parent = $ this ->hash ($ parent );
68
75
69
- if (! isset ( $ this ->nodes [ $ hash ] )) {
70
- $ this ->nodes [ $ hash ] = $ this -> graph ->createVertex ($ hash );
76
+ if (false === $ this ->graph -> hasVertex ( $ hash_parent )) {
77
+ $ this ->graph ->createVertex ($ hash_parent );
71
78
}
72
79
73
- $ this ->nodes [ $ hash ] ->createEdgeTo ($ this ->nodes [ $ hash_parent ] );
80
+ $ this ->graph -> getVertex ( $ hash_parent ) ->createEdgeTo ($ this ->graph -> getVertex ( $ hash ) );
74
81
}
75
82
76
- return $ this ->graphviz ->createScript ($ this ->graph );
83
+ return $ this ->graph ;
84
+ }
85
+
86
+ /**
87
+ * @param \drupol\phptree\Node\NodeInterface $node
88
+ *
89
+ * @return $this
90
+ */
91
+ public function display (NodeInterface $ node ): RendererInterface
92
+ {
93
+ $ this ->graphviz ->display ($ this ->getGraph ($ node ));
94
+
95
+ return $ this ;
77
96
}
78
97
79
98
/**
80
99
* @param \drupol\phptree\Node\NodeInterface $node
81
100
*
82
- * @return int
101
+ * @return int|null|string
83
102
*/
84
103
protected function hash (NodeInterface $ node )
85
104
{
86
- return ( int ) \spl_object_hash ($ node );
105
+ return \spl_object_hash ($ node );
87
106
}
88
107
}
0 commit comments