• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Mermaid-PHP / 7723213701

28 Jan 2024 11:09AM UTC coverage: 89.301% (-0.4%) from 89.693%
7723213701

push

github

web-flow
Add PHP 8.3 to GitHub Actions workflow and update jbzoo/toolbox-dev (#19)

409 of 458 relevant lines covered (89.3%)

60.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.69
/src/Render.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Mermaid-PHP.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Mermaid-PHP
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\MermaidPHP;
18

19
use JBZoo\MermaidPHP\ClassDiagram\ClassDiagram;
20
use JBZoo\MermaidPHP\ERDiagram\ERDiagram;
21
use JBZoo\MermaidPHP\Timeline\Timeline;
22

23
class Render
24
{
25
    public const THEME_DEFAULT = 'default';
26
    public const THEME_FOREST  = 'forest';
27
    public const THEME_DARK    = 'dark';
28
    public const THEME_NEUTRAL = 'neutral';
29

30
    public const DEFAULT_MERMAID_URL = 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
31

32
    public static function html(ClassDiagram|ERDiagram|Graph|Timeline $graph, array $params = []): string
33
    {
34
        $theme     = (string)($params['theme'] ?? self::THEME_FOREST);
204✔
35
        $scriptUrl = (string)($params['mermaid_url'] ?? self::DEFAULT_MERMAID_URL);
204✔
36
        $showZoom  = (bool)($params['show-zoom'] ?? true);
204✔
37
        $isDebug   = (bool)($params['debug'] ?? false);
204✔
38

39
        $title     = (string)($params['title'] ?? '');
204✔
40
        $pageTitle = $title === '' ? $title : 'JBZoo - Mermaid Graph';
204✔
41

42
        /** @see https://mermaid-js.github.io/mermaid/#/mermaidAPI?id=loglevel */
43
        $mermaidParams = \json_encode([
204✔
44
            'startOnLoad' => true,
204✔
45
            'theme'       => $theme,
204✔
46
            'themeCSS'    => \implode(\PHP_EOL, [
204✔
47
                '.edgePath .path:hover {stroke-width:4px; cursor:pointer}',
204✔
48
                '.edgeLabel {border-radius:4px}',
204✔
49
                '.label {font-family:Source Sans Pro,Helvetica Neue,Arial,sans-serif;}',
204✔
50
            ]),
204✔
51
            'maxTextSize'         => 1000000, // almost no size limitation
204✔
52
            'loglevel'            => 'debug',
204✔
53
            'securityLevel'       => 'loose',
204✔
54
            'arrowMarkerAbsolute' => true,
204✔
55
            'flowchart'           => [
204✔
56
                'htmlLabels'     => true,
204✔
57
                'useMaxWidth'    => true,
204✔
58
                'diagramPadding' => 12,
204✔
59
                'curve'          => 'basis',
204✔
60
            ],
204✔
61
        ], \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
204✔
62

63
        $debugCode = '';
204✔
64
        if ($isDebug) {
204✔
65
            $graphParams = \json_encode($graph->getParams(), \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
204✔
66

67
            $debugCode .= '<hr>';
204✔
68
            $debugCode .= '<pre><code>' . \htmlentities((string)$graph) . '</code></pre>';
204✔
69
            $debugCode .= '<hr>';
204✔
70
            $debugCode .= "<pre><code>Params = {$graphParams}</code></pre>";
204✔
71
        }
72

73
        $html = [
204✔
74
            '<!DOCTYPE html>',
204✔
75
            '<html lang="en">',
204✔
76
            '<head>',
204✔
77
            '    <meta charset="utf-8">',
204✔
78
            "    <title>{$pageTitle}</title>",
204✔
79
            '   <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>',
204✔
80
            '<script type="module">',
204✔
81
            "        import mermaid from '{$scriptUrl}';",
204✔
82
            '</script>',
204✔
83
            '</head>',
204✔
84
            '<body>',
204✔
85

86
            $title !== '' ? "<h1>{$title}</h1><hr>" : '',
204✔
87

88
            '    <div class="mermaid" style="margin-top:20px;">' . $graph . '</div>',
204✔
89

90
            $debugCode,
204✔
91

92
            $showZoom ?
204✔
93
                "<input type=\"button\" class=\"btn btn-primary\" id=\"zoom\" value=\"Zoom In\">
204✔
94
                <script>
95
                     mermaid.initialize({$mermaidParams});
204✔
96
                     $(function () {
97
                        $('#zoom').click(() => {
98
                            $('.mermaid').removeAttr('data-processed');
99
                            $('.mermaid').width($('.mermaid svg').css('max-width'));
100
                        });
101
                     });
102
                </script>"
204✔
103
                : '',
204✔
104
            '<script>
204✔
105
                $(document).on("click", "path", e => {
106
                    e.currentTarget.style.stroke = e.currentTarget.style.stroke ? "" : "red";
107
                });
108
            </script>',
204✔
109
            '</body>',
204✔
110
            '</html>',
204✔
111
        ];
204✔
112

113
        return \implode(\PHP_EOL, $html);
204✔
114
    }
115

116
    public static function escape(string $text): string
117
    {
118
        $text = \trim($text);
×
119
        $text = \htmlentities($text);
×
120

121
        $text = \str_replace(['&', '#lt;', '#gt;'], ['#', '<', '>'], $text);
×
122

123
        return "\"{$text}\"";
×
124
    }
125

126
    public static function getId(string $userFriendlyId): string
127
    {
128
        return \md5($userFriendlyId);
×
129
    }
130

131
    public static function getLiveEditorUrl(ERDiagram|Graph|Timeline $graph): string
132
    {
133
        $json = \json_encode([
×
134
            'code'    => (string)$graph,
×
135
            'mermaid' => ['theme' => 'forest'],
×
136
        ]);
×
137

138
        if ($json === false) {
×
139
            throw new \RuntimeException('Can\'t encode JSON');
×
140
        }
141

142
        $params = \base64_encode($json);
×
143

144
        return "https://mermaid-js.github.io/mermaid-live-editor/#/edit/{$params}";
×
145
    }
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc