Skip to content

Commit

Permalink
add Render::fromTopToBottom()
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed May 1, 2023
1 parent afc2f6f commit 9f90e79
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## 3.1.0 - 2023-05-01

### Added

- `Innmind\ObjectGraph\Render::fromTopToBottom()`
20 changes: 16 additions & 4 deletions src/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@
final class Render
{
private RewriteLocation $rewriteLocation;
private Graphviz\Graph\Rankdir $direction;

private function __construct(RewriteLocation $rewriteLocation)
{
private function __construct(
RewriteLocation $rewriteLocation,
Graphviz\Graph\Rankdir $direction,
) {
$this->rewriteLocation = $rewriteLocation;
$this->direction = $direction;
}

public function __invoke(Graph $graph): Content
{
$graphviz = Graphviz\Graph::directed('G', Graphviz\Graph\Rankdir::leftToRight)->add(
$graphviz = Graphviz\Graph::directed('G', $this->direction)->add(
$this
->render($graph->root())
->shaped(
Expand All @@ -49,7 +53,15 @@ public function __invoke(Graph $graph): Content
*/
public static function of(RewriteLocation $rewriteLocation = null): self
{
return new self($rewriteLocation ?? new RewriteLocation\NoOp);
return new self(
$rewriteLocation ?? new RewriteLocation\NoOp,
Graphviz\Graph\Rankdir::leftToRight,
);
}

public function fromTopToBottom(): self
{
return new self($this->rewriteLocation, Graphviz\Graph\Rankdir::topToBottom);
}

private function render(Node $node): Graphviz\Node
Expand Down
29 changes: 29 additions & 0 deletions tests/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,33 @@ public function testRenderRecursiveGraph()
->toList();
$this->assertCount(7, $lines);
}

public function testRenderFromTopToBottom()
{
// root
// |-> a
// | |-> leaf <-|
// |-> b ----------|
$leaf = new Foo;
$a = new Foo($leaf);
$b = new Foo($leaf);
$root = new Foo($a, $b);

$graph = Lookup::of()($root);
$graph = Locate::of()($graph);
$graph = $graph->mapNode(static fn($node) => match ($node->comesFrom($leaf)) {
true => $node->flagAsDependency(),
false => $node,
});
$dot = Render::of()->fromTopToBottom()($graph);

$this->assertInstanceOf(Content::class, $dot);
$this->assertNotEmpty($dot->toString());
$lines = $dot
->lines()
->map(static fn($line) => $line->str()->trim()->toString())
->toList();
$this->assertCount(11, $lines);
$this->assertSame('rankdir="TB";', $lines[1]);
}
}

0 comments on commit 9f90e79

Please sign in to comment.