php uml圖生成並能支援processon匯入

shuibo發表於2019-02-16

專案需要畫uml圖,手寫浪費時間,於是就搜了一些相關的工具來生成它。有php外掛phpumd等等。發現了一個簡單易用的工具,那就是phuml

Phuml

它是一款全自動uml圖生成器,支援php5。

安裝教程

git clone https://github.com/jakobwesthoff/phuml  //下載原始碼
yum install graphviz        //安裝圖形庫
cd phuml/src/app
./phuml -r /path -graphviz -createAssociations false -neato test.png       //生成uml圖

是不是很簡單。
但是uml圖並不方便操作。因為經常使用processon來做圖,所以如果能生成一個可以匯入到processon的.pos檔案那就更好了。

.pos檔案

processon支援匯入.pos檔案,那.pos檔案到底是什麼樣子的?

{
    "diagram":{
        //image是匯出時,生成的圖片,elements包含各個元素。匯入主要看的是elements。
        "image":{
            "height":322,
            "pngdata":"",                      //匯出時,base64格式圖片
            "width":310,
            "y":9,
            "x":26
        },
        "elements":{
            "page":{
                "gridSize":15,                 //網格大小
                "showGrid":true,             //顯示網格
                "orientation":"portrait",     //頁面方向 portrait豎向
                "height":1500,               //頁面高度
                "backgroundColor":"transparent",  //背景顏色
                "width":1250,                 //頁面長度
                "padding":20                 //頁面的灰邊寬度
            },
            "elements":{
                "15a3b34d769898":{             //生成一串隨機id作為標識,每個圖形都對應著一個
                    "textBlock":[             //文字塊,uml圖為三塊
                        //類名
                        {
                            "position":{
                                "w":"w-20",
                                "h":"30",
                                "y":"0",
                                "x":"10"
                            },
                            "text":"類"
                        },
                        //屬性
                        {
                            "position":{
                                "w":"w-20",
                                "h":47,
                                "y":"35",
                                "x":"10"
                            },
                            "text":"+ attribute1:type = defaultValue + attribute2:type - attribute3:type",
                            "fontStyle":{
                                "bold":false,
                                "textAlign":"left"
                            }
                        },
                        //方法
                        {
                            "position":{
                                "w":"w-20",
                                "h":145,
                                "y":93,
                                "x":"10"
                            },
                            "text":"+ operation1(params):returnType - operation2(params) - operation3() + operation1(params):returnType - operation2(params) - operation3() + operation1(params):returnType - operation2(params) - operation3()",
                            "fontStyle":{
                                "bold":false,
                                "textAlign":"left"
                            }
                        }
                    ],
                    "lineStyle":{

                    },
                    "link":"",
                    "children":[

                    ],
                    "parent":"",
                    "attribute":{
                        "linkable":true,
                        "visible":true,
                        "container":false,
                        "rotatable":true,
                        "markerOffset":5,
                        "collapsable":false,
                        "collapsed":false
                    },
                    "fontStyle":{
                        "bold":true
                    },
                    "resizeDir":[
                        "tl",
                        "tr",
                        "br",
                        "bl"
                    ],
                    "dataAttributes":[

                    ],
                    "shapeStyle":{
                        "alpha":1
                    },
                    "id":"15a3b34d769898",
                    "anchors":[
                        {
                            "y":"0",
                            "x":"w/2"
                        },
                        {
                            "y":"h",
                            "x":"w/2"
                        },
                        {
                            "y":"h/2",
                            "x":"0"
                        },
                        {
                            "y":"h/2",
                            "x":"w"
                        }
                    ],
                    "title":"類",
                    "category":"uml_class",
                    "name":"cls",
                    "path":[
                        {
                            "actions":[
                                {
                                    "action":"move",
                                    "y":"4",
                                    "x":"0"
                                },
                                {
                                    "y1":"0",
                                    "action":"quadraticCurve",
                                    "y":"0",
                                    "x":"4",
                                    "x1":"0"
                                },
                                {
                                    "action":"line",
                                    "y":"0",
                                    "x":"w-4"
                                },
                                {
                                    "y1":"0",
                                    "action":"quadraticCurve",
                                    "y":"4",
                                    "x":"w",
                                    "x1":"w"
                                },
                                {
                                    "action":"line",
                                    "y":"h-4",
                                    "x":"w"
                                },
                                {
                                    "y1":"h",
                                    "action":"quadraticCurve",
                                    "y":"h",
                                    "x":"w-4",
                                    "x1":"w"
                                },
                                {
                                    "action":"line",
                                    "y":"h",
                                    "x":"4"
                                },
                                {
                                    "y1":"h",
                                    "action":"quadraticCurve",
                                    "y":"h-4",
                                    "x":"0",
                                    "x1":"0"
                                },
                                {
                                    "action":"close"
                                }
                            ]
                        },
                        {
                            "fillStyle":{
                                "type":"none"
                            },
                            "actions":[
                                {
                                    "action":"move",
                                    "y":30,
                                    "x":"0"
                                },
                                {
                                    "action":"line",
                                    "y":30,
                                    "x":"w"
                                },
                                {
                                    "action":"move",
                                    "y":88,
                                    "x":"0"
                                },
                                {
                                    "action":"line",
                                    "y":88,
                                    "x":"w"
                                }
                            ]
                        },
                        {
                            "lineStyle":{
                                "lineWidth":0
                            },
                            "fillStyle":{
                                "type":"none"
                            },
                            "actions":[
                                {
                                    "action":"move",
                                    "y":"0",
                                    "x":"0"
                                },
                                {
                                    "action":"line",
                                    "y":"0",
                                    "x":"w"
                                },
                                {
                                    "action":"line",
                                    "y":"h",
                                    "x":"w"
                                },
                                {
                                    "action":"line",
                                    "y":"h",
                                    "x":"0"
                                },
                                {
                                    "action":"close"
                                }
                            ]
                        }
                    ],
                    "fillStyle":{

                    },
                    "locked":false,
                    "group":"",
                    "props":{
                        "w":230,
                        "heights":[
                            30,
                            50,
                            145
                        ],
                        "angle":0,
                        "h":242,
                        "y":49,
                        "zindex":1,
                        "x":66
                    }
                }
            }
        }
    },
    "meta":{
        "id":"58a29cdce4b028637aca553f",
        "member":"saber",
        "exportTime":"2017-02-14 14:00:44",
        "diagramInfo":{
            "category":"uml",
            "title":"訂單",
            "created":"2017-02-14 13:59:56",
            "attributes":null,
            "creator":"saber",
            "modified":"2017-02-14 14:00:25"
        },
        "type":"ProcessOn Schema File",
        "version":"1.0"
    }
}

所以只要仿照著它的格式來一份就能生成一份pos檔案。

使用

git clone https://github.com/sabercoding/phuml.git  //下載原始碼
./phuml -r /path -processon  test.pos       //生成pos檔案

待優化之處

  1. 類圖大小及文字塊大小

  2. 類之間的關係

  3. 類排版

最新發表在這裡

相關文章