Passed
Push — master ( eb20d7...7dbdce )
by Caen
03:29 queued 13s
created

DashboardController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 5
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\RealtimeCompiler\Http;
6
7
use function app;
8
use function array_merge;
9
use Composer\InstalledVersions;
10
use function config;
11
use Desilva\Microserve\Request;
12
use function file_get_contents;
13
use Hyde\Framework\Actions\AnonymousViewCompiler;
14
use Hyde\Framework\Actions\StaticPageBuilder;
15
use Hyde\Hyde;
16
use Hyde\Pages\Concerns\HydePage;
17
use function sprintf;
18
use function str_replace;
19
use function str_starts_with;
20
21
/**
22
 * @internal
23
 * @experimental
24
 */
25
class DashboardController
26
{
27
    public string $title;
28
29
    public function __construct()
30
    {
31
        $this->title = config('site.name').' - Dashboard';
32
    }
33
34
    public function show(): string
35
    {
36
        return AnonymousViewCompiler::call(__DIR__.'/../../resources/dashboard.blade.php', array_merge(
37
            (array) $this, ['dashboard' => $this, 'request' => Request::capture()],
38
        ));
39
    }
40
41
    public function getVersion(): string
42
    {
43
        $version = InstalledVersions::getPrettyVersion('hyde/realtime-compiler');
44
45
        return str_starts_with($version, 'dev-') ? $version : "v$version";
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type null; however, parameter $haystack of str_starts_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        return str_starts_with(/** @scrutinizer ignore-type */ $version, 'dev-') ? $version : "v$version";
Loading history...
Bug Best Practice introduced by
The expression return str_starts_with($...$version : 'v'.$version could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
46
    }
47
48
    public function getProjectInformation(): array
49
    {
50
        return [
51
            'Git Version:' => app('git.version'),
52
            'Hyde Version:' => InstalledVersions::getPrettyVersion('hyde/hyde') ?: 'unreleased',
53
            'Framework Version:' => InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased',
54
            'Project Path:' => Hyde::path(),
55
        ];
56
    }
57
58
    /** @return array<string, \Hyde\Support\Models\Route> */
59
    public function getPageList(): array
60
    {
61
        return Hyde::routes()->all();
62
    }
63
64
    public static function enabled(): bool
65
    {
66
        return config('hyde.server.dashboard', true);
67
    }
68
69
    // This method is called from the PageRouter and allows us to serve a dynamic welcome page
70
    public static function renderIndexPage(HydePage $page): string
71
    {
72
        $contents = file_get_contents((new StaticPageBuilder($page))->__invoke());
73
74
        // If the page is the default welcome page we inject dashboard components
75
        if (str_contains($contents, 'This is the default homepage')) {
76
            if (config('hyde.server.dashboard.welcome-banner', true)) {
77
                $contents = str_replace("</div>\n            <!-- End Main Hero Content -->",
78
                    sprintf("%s\n</div>\n<!-- End Main Hero Content -->", self::welcomeComponent()),
79
                $contents);
80
            }
81
82
            if (config('hyde.server.dashboard.welcome-dashboard', true)) {
83
                $contents = str_replace('</body>', sprintf("%s\n</body>", self::welcomeFrame()), $contents);
84
            }
85
86
            if (config('hyde.server.dashboard.button', false)) {
87
                $contents = self::injectDashboardButton($contents);
88
            }
89
        }
90
91
        return $contents;
92
    }
93
94
    protected static function injectDashboardButton(string $contents): string
95
    {
96
        return str_replace('</body>', sprintf('%s</body>', self::button()), $contents);
97
    }
98
99
    protected static function button(): string
100
    {
101
        return <<<'HTML'
102
            <style>
103
                 .dashboard-btn {
104
                    background-image: linear-gradient(to right, #1FA2FF 0%, #12D8FA  51%, #1FA2FF  100%);
105
                    margin: 10px;
106
                    padding: .5rem 1rem;
107
                    text-align: center;
108
                    transition: 0.5s;
109
                    background-size: 200% auto;
110
                    background-position: right center;
111
                    color: white;            
112
                    box-shadow: 0 0 20px #162134;
113
                    border-radius: 10px;
114
                    display: block;
115
                    position: absolute;
116
                    right: 1rem;
117
                    top: 1rem
118
                 }
119
        
120
                 .dashboard-btn:hover {
121
                    background-position: left center;
122
                    color: #fff;
123
                    text-decoration: none;
124
                }
125
            </style>
126
            <a href="/dashboard" class="dashboard-btn">Dashboard</a>
127
        HTML;
128
    }
129
130
    protected static function welcomeComponent(): string
131
    {
132
        $dashboardMessage = config('hyde.server.dashboard.welcome-dashboard', true)
133
            ? '<br>Scroll down to see it, or visit <a href="/dashboard" style="color: #1FA2FF;">/dashboard</a> at any time!' : '';
134
135
        return <<<HTML
136
            <!-- Dashboard Component -->
137
            <section class="text-white">
138
                <hr style="border-width: 1px; max-width: 240px; opacity: .75; margin-top: 30px; margin-bottom: 24px">
139
                <p style="margin-bottom: 8px;">
140
                    <span style="
141
                        background: #1FA2FF;
142
                        background: -webkit-linear-gradient(to right, #1FA2FF, #12D8FA, #1FA2FF);
143
                        background: linear-gradient(to right, #1FA2FF, #12D8FA, #1FA2FF);
144
                        padding: 3px 8px;
145
                        border-radius: 25px;
146
                        font-size: 12px;
147
                        text-transform: uppercase;
148
                        font-weight: 600;
149
                    ">New</span> When using the Realtime Compiler, you now have a content dashboard!
150
                    $dashboardMessage
151
                </p>
152
                
153
                <a href="#dashboard" onclick="document.getElementById('dashboard').scrollIntoView({behavior: 'smooth'}); return false;">
154
                    <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#ffffff"><path d="M0 0h24v24H0z" fill="none"/><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"/></svg>
155
                </a>
156
            </section>
157
            <!-- End Dashboard Component -->
158
        HTML;
159
    }
160
161
    protected static function welcomeFrame(): string
162
    {
163
        return <<<'HTML'
164
            <aside>
165
                <iframe id="dashboard" src="/dashboard?embedded=true" frameborder="0" style="width: 100vw; height: 100vh; position: absolute;"></iframe>
166
            </aside>
167
        HTML;
168
    }
169
}
170