From f301710fc58e9938de5ea5d716ad14ecbfad1c08 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 21:48:00 +0100 Subject: [PATCH] Add base session state handling --- .../src/Http/BaseController.php | 16 ++++++++++++++++ .../src/Http/DashboardController.php | 1 + 2 files changed, 17 insertions(+) diff --git a/packages/realtime-compiler/src/Http/BaseController.php b/packages/realtime-compiler/src/Http/BaseController.php index 1a069d85c21..862453922ea 100644 --- a/packages/realtime-compiler/src/Http/BaseController.php +++ b/packages/realtime-compiler/src/Http/BaseController.php @@ -18,6 +18,9 @@ abstract class BaseController protected Request $request; protected ConsoleOutput $console; protected bool $withConsoleOutput = false; + protected bool $withSession = false; + + protected static bool $sessionStarted = false; abstract public function handle(): Response; @@ -28,6 +31,19 @@ public function __construct(?Request $request = null) if ($this->withConsoleOutput && ((bool) env('HYDE_SERVER_REQUEST_OUTPUT', false)) === true) { $this->console = new ConsoleOutput(); } + + if ($this->withSession && ! self::$sessionStarted) { + session_start(); + self::$sessionStarted = true; + } + } + + public function __destruct() + { + if ($this->withSession && self::$sessionStarted) { + session_write_close(); + self::$sessionStarted = false; + } } protected function sendJsonErrorResponse(int $statusCode, string $message): JsonResponse diff --git a/packages/realtime-compiler/src/Http/DashboardController.php b/packages/realtime-compiler/src/Http/DashboardController.php index 8692c2c0f93..2ab826f5731 100644 --- a/packages/realtime-compiler/src/Http/DashboardController.php +++ b/packages/realtime-compiler/src/Http/DashboardController.php @@ -37,6 +37,7 @@ class DashboardController extends BaseController public string $title; protected bool $withConsoleOutput = true; + protected bool $withSession = true; protected JsonResponse $response;