jbzoo/mermaid-php
Composer 安装命令:
composer require jbzoo/mermaid-php
包简介
Generate diagrams and flowcharts with the help of the mermaid script language
README 文档
README
A powerful PHP library for generating Mermaid diagrams programmatically. Create flowcharts, ER diagrams, class diagrams, and timelines with a fluent, object-oriented API.
Table of Contents
- Installation
- Requirements
- Features
- Quick Start
- Diagram Types
- Output Formats
- Development
- License
- Related Projects
Installation
Install via Composer:
composer require jbzoo/mermaid-php
Requirements
- PHP 8.2 or higher
- ext-json
Features
✅ Multiple Diagram Types: Flowcharts, ER diagrams, class diagrams, and timelines ✅ Fluent API: Intuitive method chaining for building complex diagrams ✅ Multiple Output Formats: Mermaid syntax, HTML with embedded viewer, live editor URLs ✅ Theme Support: Default, forest, dark, and neutral themes ✅ Rich Customization: Nodes, links, relationships, styling, and more ✅ Type Safety: Full PHP type hints and strict typing ✅ Zero Dependencies: Only requires PHP and ext-json
Quick Start
<?php use JBZoo\MermaidPHP\Graph; use JBZoo\MermaidPHP\Node; use JBZoo\MermaidPHP\Link; // Create a simple flowchart $graph = new Graph(['title' => 'My Workflow']); $graph ->addNode($start = new Node('start', 'Start', Node::ROUND)) ->addNode($process = new Node('process', 'Process Data', Node::SQUARE)) ->addNode($end = new Node('end', 'End', Node::ROUND)) ->addLink(new Link($start, $process)) ->addLink(new Link($process, $end)); // Output Mermaid syntax echo $graph; // Generate HTML with embedded viewer echo $graph->renderHtml(['theme' => 'dark']); // Get live editor URL for debugging echo $graph->getLiveEditorUrl();
Diagram Types
Flowcharts/Graphs
Create complex flowcharts with nodes, links, and subgraphs:
<?php use JBZoo\MermaidPHP\Graph; use JBZoo\MermaidPHP\Link; use JBZoo\MermaidPHP\Node; use JBZoo\MermaidPHP\Render; $graph = (new Graph(['abc_order' => true])) ->addSubGraph($subGraph1 = new Graph(['title' => 'Main workflow'])) ->addSubGraph($subGraph2 = new Graph(['title' => 'Problematic workflow'])) ->addStyle('linkStyle default interpolate basis'); $subGraph1 ->addNode($nodeE = new Node('E', 'Result two', Node::SQUARE)) ->addNode($nodeB = new Node('B', 'Round edge', Node::ROUND)) ->addNode($nodeA = new Node('A', 'Hard edge', Node::SQUARE)) ->addNode($nodeC = new Node('C', 'Decision', Node::CIRCLE)) ->addNode($nodeD = new Node('D', 'Result one', Node::SQUARE)) ->addLink(new Link($nodeE, $nodeD)) ->addLink(new Link($nodeB, $nodeC)) ->addLink(new Link($nodeC, $nodeD, 'A double quote:"')) ->addLink(new Link($nodeC, $nodeE, 'A dec char:♥')) ->addLink(new Link($nodeA, $nodeB, ' Link text<br>/\\!@#$%^&*()_+><\' " ')); $subGraph2 ->addNode($alone = new Node('alone', 'Alone')) ->addLink(new Link($alone, $nodeC)); echo $graph; // Get result as string (or $graph->__toString(), or (string)$graph) $htmlCode = $graph->renderHtml([ 'debug' => true, 'theme' => Render::THEME_DARK, 'title' => 'Example', 'show-zoom' => false, 'mermaid_url' => 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs', ]); // Get result as HTML code for debugging echo $graph->getLiveEditorUrl(); // Get link to live editor
Result
graph TB;
subgraph "Main workflow"
E["Result two"];
B("Round edge");
A["Hard edge"];
C(("Decision"));
D["Result one"];
E-->D;
B-->C;
C-->|"A double quote:#quot;"|D;
C-->|"A dec char:#hearts;"|E;
A-->|"Link text<br>/\!@#$%^#amp;*()_+><' #quot;"|B;
end
subgraph "Problematic workflow"
alone("Alone");
alone-->C;
end
linkStyle default interpolate basis;
Loading
ER Diagrams
Build entity-relationship diagrams for database schemas:
<?php use JBZoo\MermaidPHP\ERDiagram\Entity\Entity; use JBZoo\MermaidPHP\ERDiagram\Entity\EntityProperty; use JBZoo\MermaidPHP\ERDiagram\ERDiagram; use JBZoo\MermaidPHP\ERDiagram\Relation\ManyToMany; use JBZoo\MermaidPHP\ERDiagram\Relation\ManyToOne; use JBZoo\MermaidPHP\ERDiagram\Relation\OneToMany; use JBZoo\MermaidPHP\ERDiagram\Relation\OneToOne; use JBZoo\MermaidPHP\ERDiagram\Relation\Relation; use JBZoo\MermaidPHP\Render; $diagram = (new ERDiagram(['title' => 'Order Example'])); $diagram ->addEntity($customerEntity = new Entity('C', 'Customer', props: [ new EntityProperty('id', 'int', [EntityProperty::PRIMARY_KEY], 'ID of user'), new EntityProperty('cash', 'float'), ])) ->addEntity($orderEntity = new Entity('O', 'Order')) ->addEntity($lineItemEntity = new Entity('LI', 'Line-Item')) ->addEntity($deliveryAddressEntity = new Entity('DA', 'Delivery-Address')) ->addEntity($creditCardEntity = new Entity('CC', 'Credit-Card')) ->addRelation(new OneToMany($customerEntity, $orderEntity, 'places', Relation::ONE_OR_MORE)) ->addRelation(new ManyToOne($lineItemEntity, $orderEntity, 'belongs', Relation::ZERO_OR_MORE)) ->addRelation(new ManyToMany($customerEntity, $deliveryAddressEntity, 'uses', Relation::ONE_OR_MORE)) ->addRelation(new OneToOne($customerEntity, $creditCardEntity, 'has', Relation::ONE_OR_MORE)) ; //header('Content-Type: text/plain'); //echo $diagram; // Get result as string (or $graph->__toString(), or (string)$graph) $htmlCode = $diagram->renderHtml([ 'debug' => true, 'theme' => Render::THEME_DARK, 'title' => 'Example', 'show-zoom' => false, 'mermaid_url' => 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs', ]); // Get result as HTML code for debugging echo $diagram->getLiveEditorUrl(); // Get link to live editor
Result
---
title: Order Example
---
erDiagram
"Customer" ||--|{ "Order" : places
"Line-Item" }o--|| "Order" : belongs
"Customer" }o--|{ "Delivery-Address" : uses
"Customer" ||--|| "Credit-Card" : has
"Customer" {
int id PK "ID of user"
float cash
}
Loading
Timeline Diagrams
Create timeline visualizations for chronological data:
<?php use JBZoo\MermaidPHP\Timeline\Timeline; use JBZoo\MermaidPHP\Timeline\Marker; use JBZoo\MermaidPHP\Timeline\Event; $timeline = (new Timeline(['title' => 'History of Social Media Platform'])) ->addSection( (new Timeline(['title' => 'Subsection 1'])) ->addMarker(new Marker('2002', [ new Event('Linkedin') ])) ) ->addSection( (new Timeline(['title' => 'Subsection 2'])) ->addMarker(new Marker('2004', [ new Event('Facebook'), new Event('Google'), ])) ->addMarker(new Marker('2005', [ new Event('Youtube'), ])) ->addMarker(new Marker('2006', [ new Event('Twitter'), ])) ) ; //header('Content-Type: text/plain'); //echo $diagram; // Get result as string (or $timeline->__toString(), or (string)$timeline) $htmlCode = $timeline->renderHtml([ 'debug' => true, 'theme' => Render::THEME_DARK, 'title' => 'Example', 'show-zoom' => false, 'mermaid_url' => 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs', ]); // Get result as HTML code for debugging echo $diagram->getLiveEditorUrl(); // Get link to live editor
Result
timeline
title History of Social Media Platform
section "Subsection 1"
2002 : Linkedin
section "Subsection 2"
2004 : Facebook : Google
2005 : Youtube
2006 : Twitter
Loading
Class Diagrams
Generate UML class diagrams with relationships, namespaces, and cardinality:
<?php use JBZoo\MermaidPHP\ClassDiagram\ClassDiagram; use JBZoo\MermaidPHP\ClassDiagram\Concept\Concept; use JBZoo\MermaidPHP\ClassDiagram\Concept\Attribute; use JBZoo\MermaidPHP\ClassDiagram\Concept\Visibility; use JBZoo\MermaidPHP\ClassDiagram\Concept\Method; use JBZoo\MermaidPHP\ClassDiagram\ConceptNamespace\ConceptNamespace; use JBZoo\MermaidPHP\ClassDiagram\Relationship\Relationship; use JBZoo\MermaidPHP\ClassDiagram\Relationship\RelationType; use JBZoo\MermaidPHP\ClassDiagram\Relationship\Cardinality; use JBZoo\MermaidPHP\ClassDiagram\Relationship\Link; use JBZoo\MermaidPHP\Render; $diagram = (new ClassDiagram()) ->setTitle('Animal example') ->setDirection(\JBZoo\MermaidPHP\Direction::TOP_TO_BOTTOM) ->addClass($animalClass = new Concept( identifier: 'Animal', attributes: [ new Attribute('age', 'int', Visibility::PUBLIC), new Attribute('gender', 'String', Visibility::PUBLIC), ], annotation: 'abstract' )) ->addClass($duckClass = new Concept( identifier: 'Duck', attributes: [ new Attribute('beakColor', 'String', Visibility::PUBLIC), ], methods: [ new Method('swim') ], )) ->addRelationship(new Relationship( classA: $duckClass, classB: $animalClass, relationType: RelationType::REALIZATION )) ; //header('Content-Type: text/plain'); //echo $diagram; // Get result as string (or $diagram->__toString(), or (string) $diagram) $htmlCode = $diagram->renderHtml([ 'debug' => true, 'theme' => Render::THEME_DARK, 'title' => 'Example', 'show-zoom' => false, 'mermaid_url' => 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs', ]); // Get result as HTML code for debugging echo $diagram->getLiveEditorUrl(); // Get link to live editor
Result
---
title: Animal example
---
classDiagram
direction TB
class Animal {
<<abstract>>
+int age
+String gender
}
class Duck {
+String beakColor
swim()
}
Duck ..|> Animal
Loading
Output Formats
All diagram types support multiple output formats:
Mermaid Syntax
// Get raw Mermaid syntax $mermaidCode = (string) $diagram; echo $diagram; // or use __toString()
HTML with Embedded Viewer
// Generate complete HTML page with Mermaid viewer $htmlCode = $diagram->renderHtml([ 'theme' => Render::THEME_DARK, // dark, forest, default, neutral 'title' => 'My Diagram', 'show-zoom' => true, 'debug' => false, 'mermaid_url' => 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs' ]);
Live Editor URL
// Get URL to Mermaid live editor for debugging $url = $diagram->getLiveEditorUrl();
Development
Setup
# Install dependencies make update # Run tests make test # Run all tests and code quality checks make test-all # Run code style checks make codestyle
Testing
- Unit Tests: PHPUnit tests in
tests/directory - Code Coverage: Available via
make report-all - Static Analysis: Psalm integration for type safety
Useful Links
- Mermaid Documentation - Official Mermaid.js documentation
- Mermaid Live Editor - Online editor for testing diagrams
- Mermaid on GitHub - Source code and issues
License
MIT
Related Projects
- CI-Report-Converter - Converting different error reports for deep compatibility with popular CI systems.
- Composer-Diff - See what packages have changed after
composer update. - Composer-Graph - Dependency graph visualization of composer.json based on mermaid-js.
- Utils - Collection of useful PHP functions, mini-classes, and snippets for every day.
- Image - Package provides object-oriented way to manipulate with images as simple as possible.
- Data - Extended implementation of ArrayObject. Use files as config/array.
- Retry - Tiny PHP library providing retry/backoff functionality with multiple backoff strategies and jitter support.
- SimpleTypes - Converting any values and measures - money, weight, exchange rates, length, ...
jbzoo/mermaid-php 适用场景与选型建议
jbzoo/mermaid-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.18M 次下载、GitHub Stars 达 48, 最近一次更新时间为 2019 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「diagrams」 「mermaid」 「mermaid-js」 「flowcharts」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jbzoo/mermaid-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jbzoo/mermaid-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jbzoo/mermaid-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A lightweight PHP library to convert PDF documents into clean, structured Markdown. Supports text extraction, headings, lists, tables, diagrams and code blocks for easier content reuse and publishing.
Bundle extension that installs and loads Semantic MediaWiki and associated extensions
Laravel Atlas scans your Laravel project to generate a complete, structured map of its internal components — models, controllers, routes, jobs, observers, events, commands, and more — and exports visual or machine-readable representations in formats like Mermaid, Markdown, JSON, or PDF.
This module offers some basic statistics-relevant modules
Generate entity diagrams that show how the different parts of your Craft site relate to each other
Laravel package to generate comprehensive markdown documentation for project files and database schema with AI-readable output
统计信息
- 总下载量: 1.18M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 48
- 点击次数: 30
- 依赖项目数: 6
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-12