Skip to content

Commit 3f3cc54

Browse files
committed
Move logic to base controller
1 parent bafd0bc commit 3f3cc54

File tree

2 files changed

+69
-55
lines changed

2 files changed

+69
-55
lines changed

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

+65-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,74 @@
44

55
namespace Hyde\RealtimeCompiler\Http;
66

7+
use Desilva\Microserve\Request;
8+
use Desilva\Microserve\Response;
9+
use Desilva\Microserve\JsonResponse;
10+
use Hyde\RealtimeCompiler\ConsoleOutput;
11+
use Symfony\Component\HttpKernel\Exception\HttpException;
12+
713
/**
814
* @internal This class is not intended to be edited outside the Hyde Realtime Compiler.
915
*/
1016
abstract class BaseController
1117
{
12-
//
18+
protected Request $request;
19+
protected ConsoleOutput $console;
20+
protected bool $withConsoleOutput = true;
21+
22+
abstract public function handle(): Response;
23+
24+
public function __construct()
25+
{
26+
$this->request = Request::capture();
27+
28+
if ($this->withConsoleOutput && ((bool) env('HYDE_SERVER_REQUEST_OUTPUT', false)) === true) {
29+
$this->console = new ConsoleOutput();
30+
}
31+
}
32+
33+
protected function sendJsonErrorResponse(int $statusCode, string $message): JsonResponse
34+
{
35+
return new JsonResponse($statusCode, $this->matchStatusCode($statusCode), [
36+
'error' => $message,
37+
]);
38+
}
39+
40+
protected function abort(int $code, string $message): never
41+
{
42+
throw new HttpException($code, $message);
43+
}
44+
45+
protected function matchStatusCode(int $statusCode): string
46+
{
47+
return match ($statusCode) {
48+
200 => 'OK',
49+
201 => 'Created',
50+
400 => 'Bad Request',
51+
403 => 'Forbidden',
52+
404 => 'Not Found',
53+
409 => 'Conflict',
54+
default => 'Internal Server Error',
55+
};
56+
}
57+
58+
protected function shouldUnsafeRequestBeBlocked(): bool
59+
{
60+
// As the dashboard is not password-protected, and it can make changes to the file system,
61+
// we block any requests that are not coming from the host machine. While we are clear
62+
// in the documentation that the realtime compiler should only be used for local
63+
// development, we still want to be extra careful in case someone forgets.
64+
65+
$requestIp = $_SERVER['REMOTE_ADDR'];
66+
$allowedIps = ['::1', '127.0.0.1', 'localhost'];
67+
68+
return ! in_array($requestIp, $allowedIps, true);
69+
}
70+
71+
protected function writeToConsole(string $message, string $context = 'dashboard'): void
72+
{
73+
if (isset($this->console)) {
74+
$this->console->printMessage($message, $context);
75+
}
76+
}
1377
}

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

+4-54
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
use Desilva\Microserve\JsonResponse;
2121
use Hyde\Support\Filesystem\MediaFile;
2222
use Illuminate\Support\Facades\Process;
23-
use Hyde\RealtimeCompiler\ConsoleOutput;
2423
use Hyde\Framework\Actions\StaticPageBuilder;
2524
use Hyde\Framework\Actions\AnonymousViewCompiler;
26-
use Desilva\Microserve\Request;
2725
use Composer\InstalledVersions;
2826
use Hyde\Framework\Actions\CreatesNewPageSourceFile;
2927
use Hyde\Framework\Exceptions\FileConflictException;
@@ -37,8 +35,8 @@ class DashboardController extends BaseController
3735
{
3836
public string $title;
3937

40-
protected Request $request;
41-
protected ConsoleOutput $console;
38+
protected bool $withConsoleOutput = true;
39+
4240
protected JsonResponse $response;
4341

4442
protected bool $isAsync = false;
@@ -54,12 +52,9 @@ class DashboardController extends BaseController
5452

5553
public function __construct()
5654
{
57-
$this->title = config('hyde.name').' - Dashboard';
58-
$this->request = Request::capture();
55+
parent::__construct();
5956

60-
if (((bool) env('HYDE_SERVER_REQUEST_OUTPUT', false)) === true) {
61-
$this->console = new ConsoleOutput();
62-
}
57+
$this->title = config('hyde.name').' - Dashboard';
6358

6459
$this->loadFlashData();
6560

@@ -451,38 +446,13 @@ protected static function getPackageVersion(string $packageName): string
451446
return $prettyVersion ?? 'unreleased';
452447
}
453448

454-
protected function shouldUnsafeRequestBeBlocked(): bool
455-
{
456-
// As the dashboard is not password-protected, and it can make changes to the file system,
457-
// we block any requests that are not coming from the host machine. While we are clear
458-
// in the documentation that the realtime compiler should only be used for local
459-
// development, we still want to be extra careful in case someone forgets.
460-
461-
$requestIp = $_SERVER['REMOTE_ADDR'];
462-
$allowedIps = ['::1', '127.0.0.1', 'localhost'];
463-
464-
return ! in_array($requestIp, $allowedIps, true);
465-
}
466-
467449
protected function setJsonResponse(int $statusCode, string $body): void
468450
{
469451
$this->response = new JsonResponse($statusCode, $this->matchStatusCode($statusCode), [
470452
'body' => $body,
471453
]);
472454
}
473455

474-
protected function sendJsonErrorResponse(int $statusCode, string $message): JsonResponse
475-
{
476-
return new JsonResponse($statusCode, $this->matchStatusCode($statusCode), [
477-
'error' => $message,
478-
]);
479-
}
480-
481-
protected function abort(int $code, string $message): never
482-
{
483-
throw new HttpException($code, $message);
484-
}
485-
486456
protected function findGeneralOpenBinary(): string
487457
{
488458
return match (PHP_OS_FAMILY) {
@@ -496,28 +466,8 @@ protected function findGeneralOpenBinary(): string
496466
};
497467
}
498468

499-
protected function matchStatusCode(int $statusCode): string
500-
{
501-
return match ($statusCode) {
502-
200 => 'OK',
503-
201 => 'Created',
504-
400 => 'Bad Request',
505-
403 => 'Forbidden',
506-
404 => 'Not Found',
507-
409 => 'Conflict',
508-
default => 'Internal Server Error',
509-
};
510-
}
511-
512469
protected function hasAsyncHeaders(): bool
513470
{
514471
return (getallheaders()['X-RC-Handler'] ?? getallheaders()['x-rc-handler'] ?? null) === 'Async';
515472
}
516-
517-
protected function writeToConsole(string $message, string $context = 'dashboard'): void
518-
{
519-
if (isset($this->console)) {
520-
$this->console->printMessage($message, $context);
521-
}
522-
}
523473
}

0 commit comments

Comments
 (0)