graphp/graphviz
PHP GraphViz builder for creating, editing, and rendering Graphviz DOT graphs. Build nodes and edges programmatically, manage attributes, and export to images or DOT output for documentation, diagrams, and visualization workflows.
Installation
composer require graphp/graphviz
Ensure graphviz CLI tools are installed on your system (Linux/macOS: brew install graphviz or apt-get install graphviz; Windows: via Graphviz binaries).
Basic Usage
use Graphp\Graphviz\Graphviz;
$graph = new Graphviz();
$graph->addNode('A');
$graph->addNode('B');
$graph->addEdge('A', 'B');
$graph->render('png')->save('output.png');
First Use Case Visualize a simple Laravel route hierarchy or dependency graph:
$graph = new Graphviz();
$graph->addNode('routes/web.php', ['label' => 'Web Routes']);
$graph->addNode('routes/api.php', ['label' => 'API Routes']);
$graph->addEdge('routes/web.php', 'routes/api.php', ['label' => 'Included']);
$graph->render('png')->save(storage_path('app/graphs/routes.png'));
Dynamic Graph Generation Useful for debugging Laravel service containers or Eloquent relationships:
$graph = new Graphviz();
$graph->setAttribute('graph', ['rankdir' => 'LR']); // Left-to-right layout
foreach (app()->getBindings() as $id => $binding) {
$graph->addNode($id, ['shape' => 'box']);
}
foreach (app()->getServiceProviders() as $provider) {
$graph->addEdge($provider, $provider . '.register');
}
Integration with Laravel Events Generate graphs on-demand (e.g., after model creation):
use Graphp\Graphviz\Graphviz;
Event::listen('eloquent.created: App\Models\User', function ($user) {
$graph = new Graphviz();
$graph->addNode('User', ['label' => 'User Model']);
$graph->addNode('Database', ['label' => 'DB']);
$graph->addEdge('User', 'Database', ['label' => 'Saves To']);
$graph->render('svg')->save(storage_path("app/graphs/user_{$user->id}.svg"));
});
Custom Styling for Readability Leverage Graphviz attributes for clarity:
$graph->setAttribute('node', ['fontname' => 'Arial', 'fontsize' => '12']);
$graph->setAttribute('edge', ['fontcolor' => 'gray', 'penwidth' => '2']);
$graph->addNode('Controller', ['style' => 'filled', 'color' => 'lightblue']);
Laravel Artisan Commands Create a custom command for graph generation:
use Graphp\Graphviz\Graphviz;
use Illuminate\Console\Command;
class GenerateGraphCommand extends Command
{
protected $signature = 'graph:generate {--format=png}';
protected $description = 'Generate a system graph';
public function handle()
{
$graph = new Graphviz();
// ... add nodes/edges ...
$graph->render($this->option('format'))
->save(storage_path("app/graphs/system.{$this->option('format')}"));
$this->info('Graph generated!');
}
}
API Response Visualization For debugging API payloads:
$graph = new Graphviz();
$graph->addNode('Request', ['label' => 'GET /api/users']);
$graph->addNode('Response', ['label' => json_encode($response->data)]);
$graph->addEdge('Request', 'Response');
$graph->render('svg')->save(storage_path("app/graphs/api_debug.svg"));
Graphviz CLI Dependency
graphp/graphviz requires the system-installed dot binary. Forgetting this causes RuntimeExceptions.dot -V in your terminal. Use Docker if local installation is problematic.Memory Limits
ini_set('memory_limit', '512M') or generate graphs in chunks.Edge Cases in Node Labels
&, ") in labels break DOT syntax.htmlLabel for HTML content:
$graph->addNode('Node & Edge', ['htmlLabel' => '<>Node & Edge</>']);
Validate DOT Syntax
Use Graphviz::getDot() to inspect the generated DOT code before rendering:
$dotCode = $graph->getDot();
file_put_contents('debug.dot', $dotCode);
Validate with dot -Tplain debug.dot (should output clean text).
Fallback Formats If PNG fails (e.g., missing libraries), try SVG or plain DOT:
$graph->render('svg')->save('fallback.svg');
Custom Renderers
Extend Graphp\Graphviz\Renderer\RendererInterface to support additional formats (e.g., PDF via dot -Tpdf).
Subgraphs for Hierarchy Group related nodes (e.g., Laravel components):
$graph->addSubgraph('cluster_0')
->addNode('AuthServiceProvider')
->addNode('RouteServiceProvider');
Dynamic Layouts Adjust layouts per graph type:
$graph->setAttribute('graph', ['rankdir' => 'TB']); // Top-to-bottom
$graph->setAttribute('graph', ['splines' => 'ortho']); // Orthogonal edges
Graphviz once and reuse it for multiple graphs (e.g., in a service class).storage/app/graphs and serve via Laravel’s filesystem:
return response()->file(storage_path("app/graphs/system.png"));
How can I help you explore Laravel packages today?