smeghead / php-class-diagram
一个命令行工具,它解析PHP源目录并输出PlantUML脚本。
Requires
- php: >=8.0
- nikic/php-parser: ^5.2
- phpstan/phpdoc-parser: ^1.30
- symfony/finder: ^5.3|^6.0|^7.0
Requires (Dev)
- clue/phar-composer: ^1.2
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
- smeghead/php-vendor-credits: ~0.0.4
- dev-main
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.0
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.1
- v0.1.0
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- v0.0.0
- dev-feat-asymmetric-visibility-property
- dev-feat-symmetric-visibility-constructor
- dev-support-mermaid
This package is auto-updated.
Last update: 2024-09-23 14:09:11 UTC
README
一个命令行工具,它解析PHP源目录并生成PlantUML类图脚本作为输出。
功能
- ♻️ 从源代码生成类图有助于持续的设计改进。
- 🔖 它生成专注于命名空间和关系的表达性类图。
- 🌐 图表是交互式超媒体工具,允许您点击类框直接导航到源代码,在视觉和交互方面增强理解。
- 🔧 这个简单的命令行工具易于使用。
- 💡 此外,它还可以生成一个包关系图,以可视化对外部命名空间的依赖关系。
什么是PlantUML
PlantUML - 维基百科 PlantUML是一个开源工具,允许用户使用纯文本语言创建图表。除了各种UML图表外,PlantUML还支持各种其他软件开发相关格式(如Archimate、块图、BPMN、C4、计算机网络图、ERD、甘特图、思维导图和WBD),以及JSON和YAML文件的可视化。
超媒体特性
此工具的一个突出特点是能够生成带有可点击链接的类图,这些链接直接导航到这些类的源代码。这把图表变成了交互式超媒体工具,在视觉和交互方面增强了源代码的理解。
要启用此功能,以SVG格式生成图表,并使用--svg-topurl
选项指定链接的基本URL
$ vendor/bin/php-class-diagram --svg-topurl='https://github.com/your-username/your-repo/blob/main/path/to/source' path/to/php/files
要嵌入SVG图表并保留可点击链接,请使用embed
或object
标签而不是img
标签。以下是一个示例
<html lang="en"> <head> <title>PHP Class Diagram</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> embed { max-width: 100%; max-height: 200%; width: auto; height: auto; } </style> </head> <body> <embed type="image/svg+xml" src="php-class-diagram.svg"/> </body> </html>
这确保了SVG图像是响应式的,并且超链接保持功能。
自用
php-class-diagram类图(链接SVG文件)
没有字段和方法的php-class-diagram类图
如果您的分析主要关注类之间的关系,则可能更适合使用简单的符号。
php-class-diagram包相关图
可视化包关系可以发现关键的设计问题。
安装
从DockerHub
您可以从以下URL使用包含php-class-diagram和PlantUML的Docker镜像。
从Composer
$ mkdir example
$ cd example
$ composer init
$ composer require --dev smeghead/php-class-diagram
现在,您可以执行./vendor/bin/php-class-diagram
。例如,尝试显示帮助信息。
$ vendor/bin/php-class-diagram --help usage: php-class-diagram [OPTIONS] <target php source directory> A CLI tool that parses the PHP source directory and generates PlantUML class diagram scripts as output. OPTIONS -h, --help show this help page. -v, --version show version. --class-diagram output class diagram script. (default) --package-diagram output package diagram script. --division-diagram output division diagram script. --jig-diagram output class diagram and package diagram script. --enable-class-properties describe properties in class diagram. (default) --disable-class-properties not describe properties in class diagram. --enable-class-methods describe methods in class diagram. (default) --disable-class-methods not describe methods in class diagram. --enable-class-name-summary describe classname with Class summary of document comment. (default) --disable-class-name-summary describe classname without Class summary of document comment. --hide-private hide private properties and methods. --hide-private-properties hide private properties. --hide-private-methods hide private methods. --svg-topurl specifies the top URL when displaying the class as a link when outputting in SVG format. --header='header string' additional header string. You can specify multiple header values. --include='wildcard' include target file pattern. (default: `*.php`) You can specify multiple include patterns. --exclude='wildcard' exclude target file pattern. You can specify multiple exclude patterns.
如何执行
类图
如果目录test/fixtures/no-namespace中有三个PHP源文件,每个文件都有一个TYPE注释,
- PHP源文件。
└─test
└─fixtures
└─no-namespace
└─product
Product.php
Name.php
Price.php
- Product.php
<?php class Product { /** @var Name product name. */ private $name; /** @var Price price of product. */ private $price; }
- Name.php
<?php class Name { /** @var string name. */ private $name; }
- Price.php
<?php class Price { /** @var int price. */ private int $price; }
执行php-class-diagram
将输出PlantUML脚本。
$ vendor/bin/php-class-diagram test/fixtures/no-namespace @startuml class-diagram package product as product { class "Price" as product_Price { -price : int } class "Name" as product_Name { -name : string } class "Product" as product_Product { -name : Name -price : Price +method1(param1) } } product_Product ..> product_Name product_Product ..> product_Price product_Product ..> product_Product @enduml
使用PlantUML将PlantUML脚本转换为图像。
选项header
您可以为PlantUML头部指定字符串。
$ vendor/bin/php-class-diagram \
--header='title "This is the class diagram"' \
path/to/src
选项include
您可以为处理指定目标文件的模式。
$ vendor/bin/php-class-diagram \ --include='*.php' \ --include='*.php4' \ path/to/src
选项exclude
您可以为排除处理的文件指定模式。
$ vendor/bin/php-class-diagram \ --exclude='test' \ --exclude='*Exception.php' \ path/to/src
包图
您可以使用php-class-diagram创建包关系图来可视化包依赖关系。
$ vendor/bin/php-class-diagram --package-diagram test/fixtures/dependency-loops @startuml package-related-diagram package hoge.fuga as ROOT { package product as product { package attribute as product.attribute { } package config as product.config { } } } product --> product.attribute product <-[#red,plain,thickness=4]-> product.config @enduml
相互依赖的包是不理想的。如果工具检测到此类依赖关系,则将发出带有粗红色线的警告。
分区图
如果您正在使用PHP 8.1中引入的枚举(Enum)功能,您可以生成分部图。将程序中使用的分部可视化对于研究和设计都是有帮助的。
$ bin/php-class-diagram --division-diagram test/fixtures/enum/ @startuml division-diagram card Suit #ccffcc [ Suit <b>スート</b> ==== Hearts <b>ハート</b> ---- Diamonds <b>ダイヤ</b> ---- Clubs <b>クローバー</b> ---- Spades <b>スペード</b> ] package Sub as Sub { card Status #ffcccc [ Status <b>ゲームのステータス</b> ==== Player <b>プレイヤーのターン</b> ---- Computer <b>コンピュータのターン</b> ---- GameSet <b>ゲーム終了</b> ] card MyExceptionCase #ccccff [ MyExceptionCase ==== InvalidMethod ---- InvalidProperty ---- Timeout ] card Size #ccffff [ Size ==== Small ---- Medium ---- Large ] } @enduml
GitHub Actions支持
以下仓库提供在GitHub Actions工作流程中使用php-class-diagram生成类图的actions。
smeghead/php-class-diagram-gh-action
开发
打开shell
docker compose build docker compose run --rm php_cli bash
安装依赖项
composer install
执行测试
composer test
贡献
欢迎提交问题和Pull Requests!