Skip to content

Commit

Permalink
Merge branch 'master' of github.com:hydephp/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jan 25, 2023
2 parents 52bebb0 + 7dbdcef commit 84e819c
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/end-to-end-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: vendor/bin/pest --stop-on-failure

- name: Prepare the Environment
run: echo -e "APP_URL=http://localhost:8080 \nDUSK_ENABLED=true" > .env
run: echo -e "APP_URL=http://localhost:8080 \nDUSK_ENABLED=true\nSERVER_DASHBOARD=false" > .env

- name: Upgrade Chrome Driver
run: php hyde dusk:chrome-driver `/opt/google/chrome/chrome --version | cut -d " " -f3 | cut -d "." -f1`
Expand Down
1 change: 1 addition & 0 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

'server' => [
'port' => env('SERVER_PORT', 8080),
'dashboard' => env('SERVER_DASHBOARD', true),
],

/*
Expand Down
1 change: 1 addition & 0 deletions packages/framework/config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

'server' => [
'port' => env('SERVER_PORT', 8080),
'dashboard' => env('SERVER_DASHBOARD', true),
],

/*
Expand Down
111 changes: 111 additions & 0 deletions packages/realtime-compiler/resources/dashboard.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
@php /** @var \Hyde\RealtimeCompiler\Http\DashboardController $dashboard */ @endphp
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>{{ $title }}</title>
<base target="_parent">
</head>
<body class="d-flex flex-column min-vh-100">
<nav class="navbar navbar-dark bg-dark flex-md-nowrap p-2">
<a class="navbar-brand col-md-3 col-lg-2 me-0 px-3 fs-6" href="/dashboard" style="font-weight: 600;">{{ $title }}</a>
<div class="navbar-nav">
@if($request->embedded)
<div class="nav-item text-nowrap pe-4">
<a class="nav-link px-3" href="/dashboard">Open full page dashboard</a>
</div>
@else
<div class="nav-item text-nowrap">
<a class="nav-link px-3" href="/">Back to site</a>
</div>
@endif
</div>
</nav>
<main class="container py-4 mb-auto">
<div class="col-xl-10 mx-auto">
<header class="px-4 py-5 my-4 text-center bg-light">
<h1 class="display-6 fw-bold">{{ $title }}</h1>
<div class="mx-auto">
<h2 class="h4">Welcome to the dashboard for your HydePHP site.</h2>
<p class="lead mb-0">This page is accessible through the Hyde Realtime Compiler and will not be saved to your static site.</p>
</div>
</header>
</div>
<section>
<div class="col-xl-10 mx-auto">
<div class="card mb-4">
<div class="card-header">
<h2 class="h5 mb-0">Project Information</h2>
</div>
<div class="card-body">
<table class="table table-bordered">
<tr>
@foreach($dashboard->getProjectInformation() as $type => $info)
<td>
<strong class="h6">{{ $type }}</strong>
<span class="card-text">{{ $info }}</span>
</td>
@endforeach
</tr>
</table>
</div>
</div>
</div>
</section>
<section>
<div class="col-xl-10 mx-auto">
<div class="card mb-4">
<div class="card-header">
<h2 class="h5 mb-0">Site Pages & Routes</h2>
</div>
<div class="card-body">
<table class="table table-bordered">
<tr>
@foreach(['Page Type', 'Route Key', 'Source File', 'Output File', 'Identifier'] as $header)
<th>{{ $header }}</th>
@endforeach
<th class="text-end">Actions</th>
</tr>
@foreach($dashboard->getPageList() as $route)
<tr>
<td>
<code title="\{{ $route->getPageClass() }}">{{ class_basename($route->getPageClass()) }}</code>
</td>
<td>
{{ $route->getRouteKey() }}
</td>
<td>
{{ $route->getSourcePath() }}
</td>
<td>
{{ $route->getOutputPath() }}
</td>
<td>
{{ $route->getPageIdentifier() }}
</td>
<td class="text-end">
<a href="{{ $route->getLink() }}" class="btn btn-outline-primary btn-sm">View</a>
</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
</section>
</main>
<footer class="bg-light text-center py-3 mt-3">
<div class="container d-flex align-items-center justify-content-between">
<div class="col-lg-3"></div>
<div class="col-lg-6">
<p class="mb-1">
HydePHP Realtime Compiler <span class="text-muted">{{ $dashboard->getVersion() }}</span>
</p>
</div>
<div class="col-lg-3"></div>
</div>
</footer>
</body>
</html>
169 changes: 169 additions & 0 deletions packages/realtime-compiler/src/Http/DashboardController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

declare(strict_types=1);

namespace Hyde\RealtimeCompiler\Http;

use function app;
use function array_merge;
use Composer\InstalledVersions;
use function config;
use Desilva\Microserve\Request;
use function file_get_contents;
use Hyde\Framework\Actions\AnonymousViewCompiler;
use Hyde\Framework\Actions\StaticPageBuilder;
use Hyde\Hyde;
use Hyde\Pages\Concerns\HydePage;
use function sprintf;
use function str_replace;
use function str_starts_with;

/**
* @internal
* @experimental
*/
class DashboardController
{
public string $title;

public function __construct()
{
$this->title = config('site.name').' - Dashboard';
}

public function show(): string
{
return AnonymousViewCompiler::call(__DIR__.'/../../resources/dashboard.blade.php', array_merge(
(array) $this, ['dashboard' => $this, 'request' => Request::capture()],
));
}

public function getVersion(): string
{
$version = InstalledVersions::getPrettyVersion('hyde/realtime-compiler');

return str_starts_with($version, 'dev-') ? $version : "v$version";
}

public function getProjectInformation(): array
{
return [
'Git Version:' => app('git.version'),
'Hyde Version:' => InstalledVersions::getPrettyVersion('hyde/hyde') ?: 'unreleased',
'Framework Version:' => InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased',
'Project Path:' => Hyde::path(),
];
}

/** @return array<string, \Hyde\Support\Models\Route> */
public function getPageList(): array
{
return Hyde::routes()->all();
}

public static function enabled(): bool
{
return config('hyde.server.dashboard', true);
}

// This method is called from the PageRouter and allows us to serve a dynamic welcome page
public static function renderIndexPage(HydePage $page): string
{
$contents = file_get_contents((new StaticPageBuilder($page))->__invoke());

// If the page is the default welcome page we inject dashboard components
if (str_contains($contents, 'This is the default homepage')) {
if (config('hyde.server.dashboard.welcome-banner', true)) {
$contents = str_replace("</div>\n <!-- End Main Hero Content -->",
sprintf("%s\n</div>\n<!-- End Main Hero Content -->", self::welcomeComponent()),
$contents);
}

if (config('hyde.server.dashboard.welcome-dashboard', true)) {
$contents = str_replace('</body>', sprintf("%s\n</body>", self::welcomeFrame()), $contents);
}

if (config('hyde.server.dashboard.button', false)) {
$contents = self::injectDashboardButton($contents);
}
}

return $contents;
}

protected static function injectDashboardButton(string $contents): string
{
return str_replace('</body>', sprintf('%s</body>', self::button()), $contents);
}

protected static function button(): string
{
return <<<'HTML'
<style>
.dashboard-btn {
background-image: linear-gradient(to right, #1FA2FF 0%, #12D8FA 51%, #1FA2FF 100%);
margin: 10px;
padding: .5rem 1rem;
text-align: center;
transition: 0.5s;
background-size: 200% auto;
background-position: right center;
color: white;
box-shadow: 0 0 20px #162134;
border-radius: 10px;
display: block;
position: absolute;
right: 1rem;
top: 1rem
}
.dashboard-btn:hover {
background-position: left center;
color: #fff;
text-decoration: none;
}
</style>
<a href="/dashboard" class="dashboard-btn">Dashboard</a>
HTML;
}

protected static function welcomeComponent(): string
{
$dashboardMessage = config('hyde.server.dashboard.welcome-dashboard', true)
? '<br>Scroll down to see it, or visit <a href="/dashboard" style="color: #1FA2FF;">/dashboard</a> at any time!' : '';

return <<<HTML
<!-- Dashboard Component -->
<section class="text-white">
<hr style="border-width: 1px; max-width: 240px; opacity: .75; margin-top: 30px; margin-bottom: 24px">
<p style="margin-bottom: 8px;">
<span style="
background: #1FA2FF;
background: -webkit-linear-gradient(to right, #1FA2FF, #12D8FA, #1FA2FF);
background: linear-gradient(to right, #1FA2FF, #12D8FA, #1FA2FF);
padding: 3px 8px;
border-radius: 25px;
font-size: 12px;
text-transform: uppercase;
font-weight: 600;
">New</span> When using the Realtime Compiler, you now have a content dashboard!
$dashboardMessage
</p>
<a href="#dashboard" onclick="document.getElementById('dashboard').scrollIntoView({behavior: 'smooth'}); return false;">
<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>
</a>
</section>
<!-- End Dashboard Component -->
HTML;
}

protected static function welcomeFrame(): string
{
return <<<'HTML'
<aside>
<iframe id="dashboard" src="/dashboard?embedded=true" frameborder="0" style="width: 100vw; height: 100vh; position: absolute;"></iframe>
</aside>
HTML;
}
}
12 changes: 12 additions & 0 deletions packages/realtime-compiler/src/Routing/PageRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Hyde\Pages\Concerns\HydePage;
use Hyde\RealtimeCompiler\Concerns\InteractsWithLaravel;
use Hyde\RealtimeCompiler\Concerns\SendsErrorResponses;
use Hyde\RealtimeCompiler\Http\DashboardController;
use Hyde\RealtimeCompiler\Http\HtmlResponse;
use Hyde\Support\Models\Route;

/**
Expand All @@ -28,6 +30,12 @@ public function __construct(Request $request)

protected function handlePageRequest(): Response
{
if ($this->request->path === '/dashboard' && DashboardController::enabled()) {
return new HtmlResponse(200, 'OK', [
'body' => (new DashboardController())->show(),
]);
}

$html = $this->getHtml(Route::getOrFail($this->normalizePath($this->request->path))->getPage());

return (new Response(200, 'OK', [
Expand Down Expand Up @@ -55,6 +63,10 @@ protected function normalizePath(string $path): string

protected function getHtml(HydePage $page): string
{
if ($page->identifier === 'index' && DashboardController::enabled()) {
return DashboardController::renderIndexPage($page);
}

return file_get_contents((new StaticPageBuilder($page))->__invoke());
}

Expand Down

0 comments on commit 84e819c

Please sign in to comment.