1. Go to this page and download the library: Download wazum/php-dag library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wazum / php-dag example snippets
use PhpDag\AsciiDag;
use PhpDag\Graph\{Graph, Node, Edge};
$graph = new Graph();
$graph->addNode(new Node('A', 'Start'))
->addNode(new Node('B', 'End'))
->addEdge(new Edge('A', 'B'));
echo AsciiDag::default()->render($graph);
$graph = (new Graph())
->connect('build', 'test')
->connect('test', 'deploy');
$graph->successors('build'); // direct child node ids
$graph->predecessors('deploy'); // direct parent node ids
$graph->outgoingEdges('build'); // outgoing Edge objects
$graph->incomingEdges('deploy'); // incoming Edge objects
$graph->roots(); // node ids with no incoming edges
$graph->leaves(); // node ids with no outgoing edges
$graph->descendants('build'); // all children, direct and indirect
$graph->ancestors('deploy'); // all parents, direct and indirect
$graph->shortestPath('build', 'deploy'); // node ids, or null
$graph->topologicalOrder(); // throws LogicException on a cycle
$graph->nodeCount();
$graph->edgeCount();
use PhpDag\Graph\NodeStyle;
use PhpDag\Style\BorderStyle;
new Node('a', 'Rounded', style: new NodeStyle(borderStyle: BorderStyle::Rounded));
new Node('b', 'Solid', style: new NodeStyle(borderStyle: BorderStyle::Solid));
new Node('c', 'Double', style: new NodeStyle(borderStyle: BorderStyle::Double));
new Node('d', 'Dashed', style: new NodeStyle(borderStyle: BorderStyle::Dashed));
new Node('e', 'Dotted', style: new NodeStyle(borderStyle: BorderStyle::Dotted));
use PhpDag\Graph\Badge;
$graph = new Graph();
$graph->addNode(new Node('db', 'Database', ['PostgreSQL', 'Port 5432'],
style: new NodeStyle(borderStyle: BorderStyle::Double, titleBodySeparator: true),
));
$graph->addNode(new Node('api', 'API Server', ['PHP 8.2'],
style: new NodeStyle(badge: new Badge('v2')),
));
$graph->addEdge(new Edge('db', 'api'));
echo AsciiDag::default()->render($graph);
use PhpDag\Style\EdgeStrokeStyle;
new Edge('A', 'B', EdgeStrokeStyle::Solid); // │ ─ (default)
new Edge('A', 'B', EdgeStrokeStyle::Heavy); // ┃ ━
new Edge('A', 'B', EdgeStrokeStyle::Dashed); // ╎ ╌
new Edge('A', 'B', EdgeStrokeStyle::Dotted); // ┊ ┈
new Edge('A', 'B', EdgeStrokeStyle::Double); // ║ ═
use PhpDag\Graph\Label;
$graph = new Graph();
$graph->addNode(new Node('review', 'Review'))
->addNode(new Node('approve', 'Approve'))
->addNode(new Node('reject', 'Reject'))
->addEdge(new Edge('review', 'approve', label: new Label('yes')))
->addEdge(new Edge('review', 'reject', label: new Label('no')));
echo AsciiDag::default()->render($graph);
use PhpDag\Style\AnsiColor;
$graph->colorPath(['A', 'B'], AnsiColor::Red);
echo AsciiDag::builder()->ansi()->build()->render($graph);
use PhpDag\Graph\Group;
$graph->addGroup(new Group('quality', 'Quality', ['lint', 'test']));
echo AsciiDag::default()->render($graph);
$graph = Graph::fromDot('digraph { rankdir=LR; a -> b [label="ok"]; }');
echo AsciiDag::default()->render($graph);
$dot = $graph->toDot(); // back to Graphviz
use PhpDag\Layout\LayerAssigner;
$pipeline = AsciiDag::builder()->defaultPipeline();
$pipeline->insertAfter(LayerAssigner::class, new MyCustomProcessor());
$pipeline->replace(LayerAssigner::class, new LayerAssigner(new MyLayering()));
echo AsciiDag::builder()->withPipeline($pipeline)->build()->render($graph);
$result = AsciiDag::default()->layout($graph);
echo $result->render(); // the diagram as a string
$width = $result->width(); // width in columns
$height = $result->height(); // height in rows
$result->renderTo(STDOUT);
// or a file: $result->renderTo(fopen('graph.txt', 'wb'));