bartlett/graph-uml

PHP library which helps you build UML diagrams.

1.0.0-rc.2 2020-09-10 11:05 UTC

This package is auto-updated.

Last update: 2021-08-24 11:37:03 UTC


README

Stable Upcoming
Latest Stable Version Unstable Version
Minimum PHP Version Minimum PHP Version
License License

Generate UML diagrams by reflection for your PHP projects

Note: This project is in beta stage! Feel free to report any issues you encounter.

Features

The main features provided by this library are:

  • build UML statements of a class diagram
  • build image in one of the supported formats with local dot executable (when GraphVizGenerator is used)

Install

The recommended way to install this library is through composer. If you don't know yet what is composer, have a look on introduction.

composer require bartlett/graph-uml

Additionally, you'll have to install GraphViz (dot executable). Users of Debian/Ubuntu-based distributions may simply invoke:

$ sudo apt update
$ sudo apt-get install graphviz

while remaining users should install from GraphViz Download page.

Quick Start

Once installed, you can use the following code to build UML class diagram for your existing classes:

<?php
use Bartlett\GraphUml;

use Graphp\Graph\Graph;
use Graphp\GraphViz\GraphViz;

$generator = new GraphUml\Generator\GraphVizGenerator(new GraphViz());
$graph = new Graph();
$builder = new GraphUml\ClassDiagramBuilder(
    $generator,
    $graph,
    [
        'label_format' => 'html',
        'show_constants' => false,
        'show_private' => false,
        'show_protected' => false
    ]
);

$builder->createVertexClass(GraphUml\Formatter\HtmlFormatter::class);
$builder->createVertexClass(GraphUml\Formatter\RecordFormatter::class);
$builder->createVertexClass(GraphUml\Generator\GraphVizGenerator::class);
$builder->createVertexClass(GraphUml\ClassDiagramBuilder::class);

// For large graph, orientation is recommended
// https://graphviz.gitlab.io/_pages/doc/info/attrs.html#d:rankdir
$graph->setAttribute($generator->getPrefix() . 'graph.rankdir', 'LR');
// https://graphviz.gitlab.io/_pages/doc/info/attrs.html#d:bgcolor
$graph->setAttribute($generator->getPrefix() . 'graph.bgcolor', 'transparent');
// https://graphviz.gitlab.io/_pages/doc/info/attrs.html#d:fillcolor
$graph->setAttribute($generator->getPrefix() . 'node.fillcolor', 'lightgrey');
// https://graphviz.gitlab.io/_pages/doc/info/attrs.html#d:style
$graph->setAttribute($generator->getPrefix() . 'node.style', 'filled');

// To use this feature, use my fork of graphp/graphviz project (see composer.json)
$graph->setAttribute($generator->getPrefix() . 'cluster.2.graph.bgcolor', 'lightblue');
// Either numeric or class namespace is allowed (2 = Bartlett\GraphUml)
// https://graphviz.gitlab.io/_pages/doc/info/attrs.html#k:colorList
$graph->setAttribute($generator->getPrefix() . 'cluster.Bartlett\\GraphUml\\Generator.graph.style', 'filled');
$graph->setAttribute($generator->getPrefix() . 'cluster.Bartlett\\GraphUml\\Generator.graph.fillcolor', 'yellow:blue');
$graph->setAttribute($generator->getPrefix() . 'cluster.Bartlett\\GraphUml\\Generator.graph.gradientangle', 45);
$graph->setAttribute($generator->getPrefix() . 'cluster.Bartlett\\GraphUml\\Formatter.graph.style', 'radial');
$graph->setAttribute($generator->getPrefix() . 'cluster.Bartlett\\GraphUml\\Formatter.graph.fillcolor', 'yellow:blue');
$graph->setAttribute($generator->getPrefix() . 'cluster.Bartlett\\GraphUml\\Formatter.graph.gradientangle', 180);

// show UML diagram statements
echo $generator->createScript($graph);
// default format is PNG
echo $generator->createImageFile($graph) . ' file generated' . PHP_EOL;

That should give such output:

GraPHP UML Application

Documentation

Full documentation is written in MarkDown format, and HTML export is possible with Daux.io. See output results at http://bartlett.laurent-laville.org/graph-uml/ or raw *.md files in docs folder.

Table of Contents

Resources

Contributors

  • Laurent Laville (Lead Developer)

Credits

This code is a refactored version of clue/graph-uml project, licensed under MIT.