Skip to content

Commit

Permalink
Update GraphViz renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 10, 2018
1 parent 0727216 commit 4295b3d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
12 changes: 10 additions & 2 deletions spec/drupol/phptree/tests/TestGraphVizSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@
use drupol\phptree\Node\ValueNode;
use drupol\phptree\tests\TestGraphViz;
use drupol\phptree\Visitor\BreadthFirstVisitor;
use Fhaculty\Graph\Graph;
use Graphp\GraphViz\GraphViz;
use PhpSpec\ObjectBehavior;

class TestGraphVizSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$visitor = new BreadthFirstVisitor();
$graph = new Graph();
$graphviz = new GraphViz();

$this->beConstructedWith($visitor);
$this->beConstructedWith($visitor, $graph, $graphviz);

$this->shouldHaveType(TestGraphViz::class);
}

public function it_can_create_a_graph()
{
$this->beConstructedWith(new BreadthFirstVisitor());
$visitor = new BreadthFirstVisitor();
$graph = new Graph();
$graphviz = new GraphViz();

$this->beConstructedWith($visitor, $graph, $graphviz);

$tree = new ValueNode('root', 2);

Expand Down
61 changes: 40 additions & 21 deletions src/Render/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use drupol\phptree\Node\NodeInterface;
use drupol\phptree\Visitor\VisitorInterface;
use Fhaculty\Graph\Graph;
use Graphp\GraphViz\GraphViz as OriginalGraphViz;

/**
* Class GraphViz
Expand All @@ -23,11 +24,6 @@ class GraphViz implements RendererInterface
*/
private $graph;

/**
* @var array
*/
private $nodes;

/**
* @var \Graphp\GraphViz\GraphViz
*/
Expand All @@ -38,12 +34,11 @@ class GraphViz implements RendererInterface
*
* @param \drupol\phptree\Visitor\VisitorInterface $visitor
*/
public function __construct(VisitorInterface $visitor)
public function __construct(VisitorInterface $visitor, Graph $graph, OriginalGraphViz $graphViz)
{
$this->visitor = $visitor;
$this->graph = new Graph();
$this->graphviz = new \Graphp\GraphViz\GraphViz();
$this->nodes = [];
$this->graph = $graph;
$this->graphviz = $graphViz;
}

/**
Expand All @@ -53,36 +48,60 @@ public function __construct(VisitorInterface $visitor)
*/
public function render(NodeInterface $node): string
{
foreach ($this->visitor->traverse($node) as $child) {
$hash_parent = $this->hash($child);
return $this->graphviz->createScript($this->getGraph($node));
}

/**
* @param \drupol\phptree\Node\NodeInterface $node
*
* @return \Fhaculty\Graph\Graph
*/
public function getGraph(NodeInterface $node): Graph
{
foreach ($this->visitor->traverse($node) as $node_visited) {
/** @var int $hash */
$hash = $this->hash($node_visited);

if (!isset($this->nodes[$hash_parent])) {
$this->nodes[$hash_parent] = $this->graph->createVertex($hash_parent);
if (false === $this->graph->hasVertex($hash)) {
$this->graph->createVertex($hash);
}

if (null === $parent = $child->getParent()) {
if (null === $parent = $node_visited->getParent()) {
continue;
}

$hash = $this->hash($parent);
/** @var int $hash_parent */
$hash_parent = $this->hash($parent);

if (!isset($this->nodes[$hash])) {
$this->nodes[$hash] = $this->graph->createVertex($hash);
if (false === $this->graph->hasVertex($hash_parent)) {
$this->graph->createVertex($hash_parent);
}

$this->nodes[$hash]->createEdgeTo($this->nodes[$hash_parent]);
$this->graph->getVertex($hash_parent)->createEdgeTo($this->graph->getVertex($hash));
}

return $this->graphviz->createScript($this->graph);
return $this->graph;
}

/**
* @param \drupol\phptree\Node\NodeInterface $node
*
* @return $this
*/
public function display(NodeInterface $node): RendererInterface
{
$this->graphviz->display($this->getGraph($node));

return $this;
}

/**
* @param \drupol\phptree\Node\NodeInterface $node
*
* @return int
* @return int|null|string
*/
protected function hash(NodeInterface $node)
{
return (int) \spl_object_hash($node);
return \spl_object_hash($node);
}
}

0 comments on commit 4295b3d

Please sign in to comment.