1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Koriym\AppStateDiagram; |
6
|
|
|
|
7
|
|
|
use function basename; |
8
|
|
|
use function count; |
9
|
|
|
use function dirname; |
10
|
|
|
use function file_put_contents; |
11
|
|
|
use function passthru; |
12
|
|
|
use function sprintf; |
13
|
|
|
use function str_replace; |
14
|
|
|
|
15
|
|
|
use const PHP_EOL; |
16
|
|
|
|
17
|
|
|
final class PutDiagram |
18
|
|
|
{ |
19
|
|
|
/** @var DrawDiagram */ |
20
|
|
|
private $draw; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->draw = new DrawDiagram(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function __invoke(Config $config): void |
28
|
|
|
{ |
29
|
|
|
$labelName = new LabelName(); |
30
|
|
|
$profile = new Profile($config->profile, $labelName); |
31
|
|
|
$this->invoke($profile); |
32
|
|
|
(new DumpDocs())($profile, $config->profile, $config->outputMode); |
33
|
|
|
$index = new IndexPage($profile, $config->outputMode); |
34
|
|
|
file_put_contents($index->file, $index->content); |
35
|
|
|
echo "ASD generated. {$index->file}" . PHP_EOL; |
36
|
|
|
echo sprintf('Descriptors(%s), Links(%s)', count($profile->descriptors), count($profile->links)) . PHP_EOL; |
37
|
|
|
if ($config->hasTag) { |
38
|
|
|
$taggedSvg = $this->drawTag($profile, $config, $labelName); |
39
|
|
|
echo "Tagged ASD generated. {$taggedSvg}" . PHP_EOL; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function invoke(AbstractProfile $profile, ?TaggedProfile $taggedProfile = null, ?string $color = null): void |
44
|
|
|
{ |
45
|
|
|
$this->draw('', new LabelName(), $profile, $taggedProfile, $color); |
46
|
|
|
$this->draw('.title', new LabelNameTitle(), $profile, $taggedProfile, $color); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function draw(string $fileId, LabelNameInterface $labelName, AbstractProfile $profile, ?TaggedProfile $taggedProfile, ?string $color): void |
50
|
|
|
{ |
51
|
|
|
$dot = ($this->draw)($profile, $labelName, $taggedProfile, $color); |
52
|
|
|
$extention = $fileId . '.dot'; |
53
|
|
|
$dotFile = str_replace(['.xml', '.json'], $extention, $profile->alpsFile); |
54
|
|
|
$this->convert($dotFile, $dot); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function drawTag(Profile $profile, Config $config, LabelName $labelName): string |
58
|
|
|
{ |
59
|
|
|
$filteredProfile = new TaggedProfile($profile, $config->filter->or, $config->filter->and); |
|
|
|
|
60
|
|
|
$tagDot = $config->filter->color ? (new DrawDiagram())($profile, $labelName, $filteredProfile, $config->filter->color) : (new DrawDiagram())($profile, $labelName, $filteredProfile); |
61
|
|
|
$file = str_replace(['.xml', '.json'], '.dot', $config->profile); |
62
|
|
|
$svgFile = str_replace(['.xml', '.json'], '.svg', $config->profile); |
63
|
|
|
$tagFile = dirname($file) . '/tag_' . basename($file); |
64
|
|
|
file_put_contents($tagFile, $tagDot); |
65
|
|
|
$filteredSvg = dirname($svgFile) . '/tag_' . basename($svgFile); |
66
|
|
|
$this->convert($filteredSvg, $tagDot); |
67
|
|
|
|
68
|
|
|
return $filteredSvg; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function convert(string $dotFile, string $dot): void |
72
|
|
|
{ |
73
|
|
|
file_put_contents($dotFile, $dot); |
74
|
|
|
$svgFile = str_replace('dot', 'svg', $dotFile); |
75
|
|
|
$cmd = "dot -Tsvg {$dotFile} -o {$svgFile}"; |
76
|
|
|
passthru($cmd, $status); |
77
|
|
|
if ($status !== 0) { |
78
|
|
|
echo 'Warning: Graphviz error. https://graphviz.org/download/' . PHP_EOL; // @codeCoverageIgnore |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|