Skip to content

Commit

Permalink
Update configuration loader to merge in realtime compiler environment
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Nov 11, 2023
1 parent dfd9efe commit b3f46e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
16 changes: 10 additions & 6 deletions packages/framework/src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ServeCommand extends Command
protected $signature = 'serve
{--host= : <comment>[default: "localhost"]</comment>}}
{--port= : <comment>[default: 8080]</comment>}
{--dashboard= : Enable the realtime compiler dashboard. (Defaults to config option)}
{--dashboard= : Enable the realtime compiler dashboard. (Overrides config setting)}
';

/** @var string */
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,43 @@ 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()
{
$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
Expand Down

0 comments on commit b3f46e5

Please sign in to comment.