1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Koriym\AppStateDiagram; |
6
|
|
|
|
7
|
|
|
use function array_keys; |
8
|
|
|
use function dirname; |
9
|
|
|
use function htmlspecialchars; |
10
|
|
|
use function implode; |
11
|
|
|
use function nl2br; |
12
|
|
|
use function pathinfo; |
13
|
|
|
use function sprintf; |
14
|
|
|
use function strtoupper; |
15
|
|
|
use function uasort; |
16
|
|
|
|
17
|
|
|
use const PATHINFO_BASENAME; |
18
|
|
|
use const PHP_EOL; |
19
|
|
|
|
20
|
|
|
final class IndexPage |
21
|
|
|
{ |
22
|
|
|
/** @var string */ |
23
|
|
|
public $content; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
public $file; |
27
|
|
|
|
28
|
|
|
public function __construct(Profile $profile, string $mode = DumpDocs::MODE_HTML) |
29
|
|
|
{ |
30
|
|
|
$profilePath = pathinfo($profile->alpsFile, PATHINFO_BASENAME); |
31
|
|
|
$descriptors = $profile->descriptors; |
32
|
|
|
uasort($descriptors, static function (AbstractDescriptor $a, AbstractDescriptor $b): int { |
33
|
|
|
$compareId = strtoupper($a->id) <=> strtoupper($b->id); |
34
|
|
|
if ($compareId !== 0) { |
35
|
|
|
return $compareId; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$order = ['semantic' => 0, 'safe' => 1, 'unsafe' => 2, 'idempotent' => 3]; |
39
|
|
|
|
40
|
|
|
return $order[$a->type] <=> $order[$b->type]; |
41
|
|
|
}); |
42
|
|
|
$linkRelations = $this->linkRelations($profile->linkRelations); |
43
|
|
|
$ext = $mode === DumpDocs::MODE_MARKDOWN ? 'md' : DumpDocs::MODE_HTML; |
44
|
|
|
$semantics = $this->semantics($descriptors, $ext); |
45
|
|
|
$tags = $this->tags($profile->tags, $ext); |
46
|
|
|
$htmlTitle = htmlspecialchars($profile->title ?: 'ALPS'); |
47
|
|
|
$htmlDoc = nl2br(htmlspecialchars($profile->doc)); |
48
|
|
|
$profileImage = $mode === DumpDocs::MODE_HTML ? 'docs/asd.html' : 'docs/asd.md'; |
49
|
|
|
$md = <<<EOT |
50
|
|
|
# {$htmlTitle} |
51
|
|
|
|
52
|
|
|
{$htmlDoc} |
53
|
|
|
|
54
|
|
|
* [ALPS]({$profilePath}) |
55
|
|
|
* [Application State Diagram]($profileImage) |
56
|
|
|
* Semantic Descriptors |
57
|
|
|
{$semantics}{$tags}{$linkRelations} |
58
|
|
|
EOT; |
59
|
|
|
$this->file = sprintf('%s/index.%s', dirname($profile->alpsFile), $ext); |
60
|
|
|
$this->content = $mode === DumpDocs::MODE_MARKDOWN ? $md : (new MdToHtml())($htmlTitle, $md); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @param array<string, AbstractDescriptor> $semantics */ |
64
|
|
|
private function semantics(array $semantics, string $ext): string |
65
|
|
|
{ |
66
|
|
|
$lines = []; |
67
|
|
|
foreach ($semantics as $semantic) { |
68
|
|
|
$href = sprintf('docs/%s.%s.%s', $semantic->type, $semantic->id, $ext); |
69
|
|
|
$title = $semantic->title ? sprintf(', %s', $semantic->title) : ''; |
70
|
|
|
$lines[] = sprintf(' * [%s](%s) (%s)%s', $semantic->id, $href, $semantic->type, $title); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return implode(PHP_EOL, $lines); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @param array<string, list<string>> $tags */ |
|
|
|
|
77
|
|
|
private function tags(array $tags, string $ext): string |
78
|
|
|
{ |
79
|
|
|
if ($tags === []) { |
80
|
|
|
return ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$lines = []; |
84
|
|
|
$tagKeys = array_keys($tags); |
85
|
|
|
foreach ($tagKeys as $tag) { |
86
|
|
|
$href = "docs/tag.{$tag}.{$ext}"; |
87
|
|
|
$lines[] = " * [{$tag}]({$href})"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return PHP_EOL . ' * Tags' . PHP_EOL . implode(PHP_EOL, $lines); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function linkRelations(LinkRelations $linkRelations): string |
94
|
|
|
{ |
95
|
|
|
if ((string) $linkRelations === '') { |
96
|
|
|
return ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return PHP_EOL . ' * Links' . PHP_EOL . $linkRelations; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|