phpdocumentor/graphviz
PHP library to create and render Graphviz DOT graphs. Build nodes and edges via a fluent API, generate DOT output, and render to common formats (PNG, SVG, PDF) through Graphviz executables—useful for diagrams, docs, and visualizing relations.
Start by installing the package via Composer:
composer require phpdocumentor/graphviz
You’ll also need the Graphviz command-line tool (dot) installed on your system — verify with dot -V. Once set up, the most immediate use case is generating a simple diagram for code structure visualization (e.g., class dependencies or component relationships):
use phpDocumentor\GraphViz\Graph;
use phpDocumentor\GraphViz\Nodes\Node;
$graph = Graph::Directed('example');
$graph->addNode(new Node('A', ['label' => 'Class_A']))
->addNode(new Node('B', ['label' => 'Class_B']))
->addEdge('A', 'B');
echo $graph->toString(); // Outputs DOT syntax
For rendering to image format (e.g., PNG), ensure Graphviz binaries are in your PATH, then:
$graph->render('output.png'); // Requires Graphviz installed
Check examples/ in the repository (if available) or inspect src/ for minimal working samples.
Fluent API for Modularity: Chain operations to build graphs incrementally. Ideal for generating architecture diagrams from reflection data (e.g., using phpdocumentor/reflection-docblock or nikic/php-parser to analyze classes and dependencies).
Configurable Styling: Use attributes to control appearance:
$graph->setGraphAttributes(['rankdir' => 'LR']);
$graph->addNode(new Node('X', ['shape' => 'box', 'color' => 'blue']));
$graph->addEdge('X', 'Y', ['label' => 'uses', 'fontcolor' => 'red']);
Subgraph Clustering: Group related nodes (e.g., by namespace or module):
$cluster = $graph->addCluster('cluster_services', ['label' => 'Services']);
$cluster->addNode('UserService')
->addNode('OrderService');
Integration with CI/Docs: Output DOT to disk for inclusion in documentation workflows (e.g., used in PHPDoc toolchains or Sphinx), or hook into build scripts to auto-generate SVGs for README.md.
Test-Driven Diagrams: Generate diagrams programmatically in tests to validate expected structures (e.g., verify dependency flow in a service container).
Binary Dependency: Rendering fails silently if dot isn’t executable or in $PATH. Always check via exec('which dot', $out, $code) in dev or use Graph::withBinaryPath('/usr/bin/dot') if needed.
Escaping Special Characters: Node/edge labels with spaces, quotes, or HTML-like strings require quoting or escaping. Use Node::createSafeName() (if available) or wrap values in double quotes manually.
DOT Syntax Limitations: While the wrapper simplifies DOT, complex graphs (e.g., overlapping clusters) may need direct DOT tweaks. Review $graph->toString() for debugging.
Non-blocking Rendering: Prefer toString() in development for fast iteration; use render() only in production builds or CLI tools.
Extensibility: Extend Node or Edge to inject domain-specific attributes (e.g., @Entity-style nodes with ORM metadata icons).
Version Note: The package hasn’t had a release since 2021 — verify compatibility with PHP 8.0+ and consider pinning in composer.json. The codebase is small and stable, so it’s low-risk for adoption if active forks exist.
How can I help you explore Laravel packages today?