LoginSignup
1
1

More than 3 years have passed since last update.

plantuml-encodingを使ってPHPでPlantUmlする

Posted at

概要

PHP で API クライアントを実装する を参考に、plantuml-encodingを使用してPlantUMLのコードを圧縮文字列にする変換をPHPで行う。

最終目的: PlantUMLのコードをテキストとして実データで保持しつつ(git管理しつつ)、図は自動で更新され、かつ閲覧のみならば簡単に行える環境を探す

try

環境

PHP実行できる環境。
今回はdocker-composeで用意。

最初のディレクトリ構造
project/
 ├ docker-compose.yml
 ├ docker(dockerfileなど入ってる)
 ├ public/
   └ index.php/

jawira/plantuml-encoding

composerで入れる

$ composer require jawira/plantuml-encoding

とりあえず実行してみます

参考のサンプルコードを少し変えてみました。

index.php
<?php
require '../vendor/autoload.php';
use function Jawira\PlantUml\encodep;
$encode = encodep('
    Alice -> Bob: hello
');
$svg = file_get_contents("http://www.plantuml.com/plantuml/svg/{$encode}");

echo $svg

encodep()関数でエンコードを行います。
生成した圧縮文字を用いてPlantUML Serverからsvgを取得し、それをそのまま画面に出します。

スクリーンショット 2019-11-07 9.58.16.png

PlantUmlを別ファイルに記述する

./umls/test.puml
@startuml
Alice -> Bob: hello
@enduml
index.php
<?php
require '../vendor/autoload.php';
use function Jawira\PlantUml\encodep;

$uml = file_get_contents('../umls/test.puml');
$uml = str_replace('@startuml', '', $uml);
$uml = str_replace('@enduml', '', $uml);

$encode = encodep($uml);
$svg = file_get_contents("http://www.plantuml.com/plantuml/svg/{$encode}");

echo $svg;

別のファイルにUMLを記述して読み込みます。
エディタのパッケージで開発中にプレビューを行うには@startuml@endumlの記述が必要なのですが、エンコード時には取り除きます。

おわり

フレームワークなど使って.pumlファイルを保存する作業ディレクトリ内の内容が、そのまま図として表示が出来るようにしたら便利なんじゃないかなと思いました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1