Skip to content

Commit e0760a9

Browse files
authored
Merge pull request #1858 from hydephp/deeper-realtime-compiler-integration
Refactor realtime compiler to handle virtual routes through the service container
2 parents 97acdcb + 8ca3da8 commit e0760a9

File tree

10 files changed

+119
-23
lines changed

10 files changed

+119
-23
lines changed

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This serves two purposes:
1818
- Removed the nullable type hint from the `PostAuthor` class's `name` property as it is now always set in https://github.com/hydephp/develop/pull/1794
1919
- Improved the accessibility of the heading permalinks feature in https://github.com/hydephp/develop/pull/1803
2020
- Updated to HydeFront v3.4 in https://github.com/hydephp/develop/pull/1803
21+
- Realtime Compiler: Virtual routes are now managed through the service container in https://github.com/hydephp/develop/pull/1858
2122

2223
### Deprecated
2324
- The `PostAuthor::getName()` method is now deprecated and will be removed in v2. (use `$author->name` instead) in https://github.com/hydephp/develop/pull/1794

composer.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/realtime-compiler/composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
"pestphp/pest-plugin": false
3434
}
3535
},
36+
"extra": {
37+
"laravel": {
38+
"providers": [
39+
"Hyde\\RealtimeCompiler\\RealtimeCompilerServiceProvider"
40+
]
41+
}
42+
},
3643
"require-dev": {
3744
"phpunit/phpunit": "^10.0",
3845
"robiningelbrecht/phpunit-pretty-print": "^1.3",

packages/realtime-compiler/src/Http/DashboardController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ protected function createPage(): void
330330
$content = $this->request->data['contentInput'] ?? $this->abort(400, 'Must provide content');
331331
$pageType = $this->request->data['pageTypeSelection'] ?? $this->abort(400, 'Must provide page type');
332332

333-
// Optional data
333+
// Optional data // Todo: Filter empty data? Comment them?
334334
$postDescription = $this->request->data['postDescription'] ?? null;
335335
$postCategory = $this->request->data['postCategory'] ?? null;
336336
$postAuthor = $this->request->data['postAuthor'] ?? null;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\RealtimeCompiler\Http;
6+
7+
use Desilva\Microserve\Request;
8+
use Desilva\Microserve\Response;
9+
use Desilva\Microserve\JsonResponse;
10+
11+
class VirtualRouteController
12+
{
13+
public static function ping(): JsonResponse
14+
{
15+
return new JsonResponse(200, 'OK', [
16+
'server' => 'Hyde/RealtimeCompiler',
17+
]);
18+
}
19+
20+
public static function dashboard(Request $request): Response
21+
{
22+
return (new DashboardController($request))->handle();
23+
}
24+
25+
public static function liveEdit(Request $request): Response
26+
{
27+
return (new LiveEditController($request))->handle();
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\RealtimeCompiler;
6+
7+
use Desilva\Microserve\Response;
8+
9+
/**
10+
* @public This class is part of the public API and is covered by the backward compatibility promise.
11+
*/
12+
class RealtimeCompiler
13+
{
14+
/** @var array<string, callable(\Desilva\Microserve\Request): Response> */
15+
protected array $virtualRoutes = [];
16+
17+
/** @param callable(\Desilva\Microserve\Request): Response $route */
18+
public function registerVirtualRoute(string $uri, callable $route): void
19+
{
20+
$this->virtualRoutes[$uri] = $route;
21+
}
22+
23+
/** @return array<string, callable(\Desilva\Microserve\Request): Response> */
24+
public function getVirtualRoutes(): array
25+
{
26+
return $this->virtualRoutes;
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\RealtimeCompiler;
6+
7+
use Illuminate\Support\ServiceProvider;
8+
use Hyde\RealtimeCompiler\Http\LiveEditController;
9+
use Hyde\RealtimeCompiler\Http\DashboardController;
10+
use Hyde\RealtimeCompiler\Http\VirtualRouteController;
11+
12+
class RealtimeCompilerServiceProvider extends ServiceProvider
13+
{
14+
public function register(): void
15+
{
16+
$this->app->singleton(RealtimeCompiler::class);
17+
}
18+
19+
public function boot(): void
20+
{
21+
$router = $this->app->make(RealtimeCompiler::class);
22+
23+
$router->registerVirtualRoute('/ping', [VirtualRouteController::class, 'ping']);
24+
25+
if (DashboardController::enabled()) {
26+
$router->registerVirtualRoute('/dashboard', [VirtualRouteController::class, 'dashboard']);
27+
}
28+
29+
if (LiveEditController::enabled()) {
30+
$router->registerVirtualRoute('/_hyde/live-edit', [VirtualRouteController::class, 'liveEdit']);
31+
}
32+
}
33+
}

packages/realtime-compiler/src/Routing/PageRouter.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Hyde\Framework\Exceptions\RouteNotFoundException;
1414
use Hyde\Framework\Features\Documentation\DocumentationSearchPage;
1515
use Hyde\Pages\Concerns\HydePage;
16-
use Hyde\RealtimeCompiler\Concerns\InteractsWithLaravel;
1716
use Hyde\RealtimeCompiler\Concerns\SendsErrorResponses;
1817
use Hyde\RealtimeCompiler\Http\DashboardController;
1918
use Desilva\Microserve\HtmlResponse;
@@ -25,26 +24,16 @@
2524
class PageRouter
2625
{
2726
use SendsErrorResponses;
28-
use InteractsWithLaravel;
2927

3028
protected Request $request;
3129

3230
public function __construct(Request $request)
3331
{
3432
$this->request = $request;
35-
$this->bootApplication();
3633
}
3734

3835
protected function handlePageRequest(): Response
3936
{
40-
if ($this->request->path === '/dashboard' && DashboardController::enabled()) {
41-
return (new DashboardController($this->request))->handle();
42-
}
43-
44-
if ($this->request->path === '/_hyde/live-edit' && LiveEditController::enabled()) {
45-
return (new LiveEditController($this->request))->handle();
46-
}
47-
4837
return new HtmlResponse(200, 'OK', [
4938
'body' => $this->getHtml($this->getPageFromRoute()),
5039
]);

packages/realtime-compiler/src/Routing/Router.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Hyde\RealtimeCompiler\Routing;
66

7-
use Desilva\Microserve\JsonResponse;
87
use Desilva\Microserve\Request;
98
use Desilva\Microserve\Response;
9+
use Hyde\RealtimeCompiler\RealtimeCompiler;
1010
use Hyde\RealtimeCompiler\Actions\AssetFileLocator;
1111
use Hyde\RealtimeCompiler\Concerns\SendsErrorResponses;
1212
use Hyde\RealtimeCompiler\Models\FileObject;
@@ -20,10 +20,6 @@ class Router
2020

2121
protected Request $request;
2222

23-
protected array $virtualRoutes = [
24-
'/ping',
25-
];
26-
2723
public function __construct(Request $request)
2824
{
2925
$this->request = $request;
@@ -35,12 +31,12 @@ public function handle(): Response
3531
return $this->proxyStatic();
3632
}
3733

38-
if (in_array($this->request->path, $this->virtualRoutes)) {
39-
if ($this->request->path === '/ping') {
40-
return new JsonResponse(200, 'OK', [
41-
'server' => 'Hyde/RealtimeCompiler',
42-
]);
43-
}
34+
$this->bootApplication();
35+
36+
$virtualRoutes = app(RealtimeCompiler::class)->getVirtualRoutes();
37+
38+
if (isset($virtualRoutes[$this->request->path])) {
39+
return $virtualRoutes[$this->request->path]($this->request);
4440
}
4541

4642
return PageRouter::handle($this->request);

0 commit comments

Comments
 (0)