From b3f46e5fa98b1d658d8f8986c2d7b828c3e53db3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 15:26:12 +0100 Subject: [PATCH] Update configuration loader to merge in realtime compiler environment --- .../src/Console/Commands/ServeCommand.php | 16 +++++++++----- .../Foundation/Internal/LoadConfiguration.php | 6 +++++ .../Unit/ServeCommandOptionsUnitTest.php | 22 ++++++++++--------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 2a3860460fe..8f778617f48 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -25,7 +25,7 @@ class ServeCommand extends Command protected $signature = 'serve {--host= : [default: "localhost"]}} {--port= : [default: 8080]} - {--dashboard= : Enable the realtime compiler dashboard. (Defaults to config option)} + {--dashboard= : Enable the realtime compiler dashboard. (Overrides config setting)} '; /** @var string */ @@ -57,11 +57,11 @@ protected function getPortSelection(): int return (int) ($this->option('port') ?: Config::getInt('hyde.server.port', 8080)); } - protected function getDashboardSelection(): bool + protected function getDashboardSelection(): ?bool { return $this->option('dashboard') !== null - ? (bool) $this->option('dashboard') - : Config::getBool('hyde.server.dashboard.enabled', true); + ? $this->option('dashboard') !== 'false' + : null; } protected function getExecutablePath(): string @@ -76,10 +76,14 @@ protected function runServerProcess(string $command): void protected function getEnvironmentVariables(): array { - return [ + $vars = [ 'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'), - 'SERVER_DASHBOARD' => $this->getDashboardSelection(), ]; + if ($this->getDashboardSelection() !== null) { + $vars['HYDE_RC_SERVER_DASHBOARD'] = $this->getDashboardSelection() ? 'enabled' : 'disabled'; + } + + return $vars; } protected function configureOutput(): void diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index 6c9b0e91af2..db1f13f5895 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Config\Repository as RepositoryContract; use Illuminate\Foundation\Bootstrap\LoadConfiguration as BaseLoadConfiguration; +use function getenv; use function array_merge; use function dirname; use function in_array; @@ -91,6 +92,11 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $ if (in_array('--no-api', $_SERVER['argv'], true)) { $repository->set('hyde.api_calls', false); } + } else { + // Check if HYDE_RC_SERVER_DASHBOARD environment variable is set, and if so, set the config value accordingly. + if (getenv('HYDE_RC_SERVER_DASHBOARD') !== false) { + $repository->set('hyde.server.dashboard.enabled', getenv('HYDE_RC_SERVER_DASHBOARD') === 'enabled'); + } } } } diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 607af78ecfb..5065f81991f 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -60,24 +60,26 @@ public function test_getPortSelection_withPortOptionAndConfigOption() public function test_getDashboardSelection() { - $this->assertSame(true, $this->getMock()->getDashboardSelection()); + $this->assertSame(null, $this->getMock()->getDashboardSelection()); } public function test_getDashboardSelection_withDashboardOption() { - $this->assertSame(false, $this->getMock(['dashboard' => false])->getDashboardSelection()); + $this->assertSame(false, $this->getMock(['dashboard' => 'false'])->getDashboardSelection()); + $this->assertSame(true, $this->getMock(['dashboard' => 'true'])->getDashboardSelection()); + $this->assertSame(true, $this->getMock(['dashboard' => ''])->getDashboardSelection()); } public function test_getDashboardSelection_withConfigOption() { $this->app['config']->set('hyde.server.dashboard.enabled', false); - $this->assertSame(false, $this->getMock()->getDashboardSelection()); + $this->assertSame(null, $this->getMock()->getDashboardSelection()); } public function test_getDashboardSelection_withDashboardOptionAndConfigOption() { $this->app['config']->set('hyde.server.dashboard.enabled', false); - $this->assertSame(true, $this->getMock(['dashboard' => true])->getDashboardSelection()); + $this->assertSame(true, $this->getMock(['dashboard' => 'true'])->getDashboardSelection()); } public function test_getDashboardSelection_propagatesToEnvironmentVariables() @@ -85,16 +87,16 @@ public function test_getDashboardSelection_propagatesToEnvironmentVariables() $command = $this->getMock(); $this->app['config']->set('hyde.server.dashboard.enabled', false); - $this->assertSame(false, $command->getEnvironmentVariables()['SERVER_DASHBOARD']); + $this->assertSame(false, isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); $this->app['config']->set('hyde.server.dashboard.enabled', true); - $this->assertSame(true, $command->getEnvironmentVariables()['SERVER_DASHBOARD']); + $this->assertSame(false, isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); - $command = $this->getMock(['dashboard' => false]); - $this->assertSame(false, $command->getEnvironmentVariables()['SERVER_DASHBOARD']); + $command = $this->getMock(['dashboard' => 'false']); + $this->assertSame('disabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); - $command = $this->getMock(['dashboard' => true]); - $this->assertSame(true, $command->getEnvironmentVariables()['SERVER_DASHBOARD']); + $command = $this->getMock(['dashboard' => 'true']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); } protected function getMock(array $options = []): ServeCommandMock