nddcoder/diagram-as-code

There is no license information available for the latest version (dev-master) of this package.

dev-master 2022-07-07 04:28 UTC

This package is auto-updated.

Last update: 2024-11-07 09:25:59 UTC


README

Installation

composer require nddcoder/diagram-as-code

Usage

Create a demo.php and return Diagram instance

<?php

use Nddcoder\Dac\Base\Cluster;
use Nddcoder\Dac\Base\Diagram;
use Nddcoder\Dac\Base\Style\Color;
use Nddcoder\Dac\Base\Style\EdgeStyle;
use Nddcoder\Dac\Base\Style\Style;
use Nddcoder\Dac\Diagram\Aws\Compute\Ec2;
use Nddcoder\Dac\Diagram\Onprem\Database\Mysql;
use Nddcoder\Dac\Diagram\Onprem\Queue\Kafka;

return Diagram::create('G', 'Demo Diagram As Code')
    ->direction(Style::DIR_LR)
    ->append(
        Cluster::create('kafka', 'Kafka Cluster')
            ->append($kafka = Kafka::create('kafka', 'Kafka'))
    )
    ->append(
        Cluster::create('event_driven', 'Event Driven')
            ->append($consumer = Ec2::create('consumer', 'Consumer'))
    )
    ->append($database = Mysql::create('mysql', 'Mysql'))
    ->connect($kafka, $consumer, EdgeStyle::create()->style(Style::BOLD)->color(Color::BROWN))
    ->connect($consumer, $database, EdgeStyle::create()->style(Style::BOLD)->color(Color::DARKGREEN)->label('WRITE'));

Run dac command to build dot config

vendor/bin/dac demo.php

Dot config output

digraph G {
    label="Demo Diagram As Code";
    node [shape=box, style=rounded, fixedsize=true, width="1.4", height="1.4", labelloc=b, imagescale=true, fontname="Sans-Serif", fontsize="13", fontcolor="#2D3436"];
    edge [color="#7B8894", fontcolor="#2D3436", fontname="Sans-Serif", fontsize="13"];
    graph [pad="0.5", splines=ortho, nodesep="1.2", ranksep="1.2", fontname="Sans-Serif", fontsize="15", fontcolor="#2D3436"];
    rankdir=LR;
    subgraph cluster_kafka {
        label="Kafka Cluster";
        graph [pad="1", shape=box, style=rounded, labeljust=l, pencolor="#AEB6BE", fontname="Sans-Serif", fontsize="12", bgcolor="#E5F5FD"];
        kafka [label=Kafka, shape=none, height="2", image="resources/onprem/queue/kafka.png"];
    }
    subgraph cluster_event_driven {
        label="Event Driven";
        graph [pad="1", shape=box, style=rounded, labeljust=l, pencolor="#AEB6BE", fontname="Sans-Serif", fontsize="12", bgcolor="#E5F5FD"];
        consumer [label=Consumer, shape=none, height="2", image="resources/aws/compute/ec2.png"];
    }
    mysql [label=Mysql, shape=none, height="2", image="resources/onprem/database/mysql.png"];
    kafka -> consumer [style=bold, color=brown];
    consumer -> mysql [style=bold, color=darkgreen, xlabel=WRITE];
}

Generate PNG file

Using Docker

vendor/bin/dac --root=/dot/vendor/nddcoder/diagram-as-code demo.php | docker run --rm -i -v $(pwd):/dot nshine/dot > demo.png

Use dot command from graphviz.

vendor/bin/dac demo.php | dot -Tpng > demo.png

PNG Output

Demo Diagram As Code

Merge multiple files

If you want to merge multiple files, you can use --all option

Example for following directory struct

projects/
  project-a/
    diagram.php
  project-b/
    .diagram/
      diagram.php
  ...
  vendor
vendor/bin/dac --all .

This command will look up for diagram.php or .diagram/diagram.php file in all sub folders

If you want set name of output diagram, you can use --name option

vendor/bin/dac --all --name=demo .

Sample