|
| 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"; |
| 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 | +} |
0 commit comments