From ef5863a976e4ca7e0c95cc1e7e7d56ecb78bccce Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 13:43:10 +0100 Subject: [PATCH 01/52] Create ServeCommandOptionsUnitTest.php --- .../tests/Unit/ServeCommandOptionsUnitTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php new file mode 100644 index 00000000000..642076eb05b --- /dev/null +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -0,0 +1,16 @@ + Date: Sat, 11 Nov 2023 13:43:23 +0100 Subject: [PATCH 02/52] Link to added unit test --- packages/framework/tests/Feature/Commands/ServeCommandTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/tests/Feature/Commands/ServeCommandTest.php b/packages/framework/tests/Feature/Commands/ServeCommandTest.php index 7112b461ff8..1e7430dae7f 100644 --- a/packages/framework/tests/Feature/Commands/ServeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/ServeCommandTest.php @@ -13,6 +13,7 @@ /** * @covers \Hyde\Console\Commands\ServeCommand + * @see \Hyde\Framework\Testing\Unit\ServeCommandOptionsUnitTest */ class ServeCommandTest extends TestCase { From c38c269090279755fdfb584851261fd5a6f08250 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 11 Nov 2023 12:43:35 +0000 Subject: [PATCH 03/52] Apply fixes from StyleCI --- packages/framework/tests/Feature/Commands/ServeCommandTest.php | 1 + packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/framework/tests/Feature/Commands/ServeCommandTest.php b/packages/framework/tests/Feature/Commands/ServeCommandTest.php index 1e7430dae7f..f959d9a01ea 100644 --- a/packages/framework/tests/Feature/Commands/ServeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/ServeCommandTest.php @@ -13,6 +13,7 @@ /** * @covers \Hyde\Console\Commands\ServeCommand + * * @see \Hyde\Framework\Testing\Unit\ServeCommandOptionsUnitTest */ class ServeCommandTest extends TestCase diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 642076eb05b..088c69fc871 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -8,6 +8,7 @@ /** * @covers \Hyde\Console\Commands\ServeCommand + * * @see \Hyde\Framework\Testing\Feature\Commands\ServeCommandTest */ class ServeCommandOptionsUnitTest extends TestCase From 20c87617cac9a6e228eeece907f7ab01f1f26c95 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 13:48:44 +0100 Subject: [PATCH 04/52] Swap order of methods to match call order --- packages/framework/src/Console/Commands/ServeCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index bc738dacb7b..15b920f6ea6 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -46,14 +46,14 @@ public function handle(): int return Command::SUCCESS; } - protected function getPortSelection(): int + protected function getHostSelection(): string { - return (int) ($this->option('port') ?: Config::getInt('hyde.server.port', 8080)); + return (string) $this->option('host') ?: Config::getString('hyde.server.host', 'localhost'); } - protected function getHostSelection(): string + protected function getPortSelection(): int { - return (string) $this->option('host') ?: Config::getString('hyde.server.host', 'localhost'); + return (int) ($this->option('port') ?: Config::getInt('hyde.server.port', 8080)); } protected function getExecutablePath(): string From e65d191a836e87aa672640299e9c582a5bebb4ae Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 13:57:06 +0100 Subject: [PATCH 05/52] Unit test serve command option selection logic --- .../Unit/ServeCommandOptionsUnitTest.php | 92 ++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 088c69fc871..79a7c89b6fa 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Testing\Unit; use Hyde\Testing\TestCase; +use Hyde\Console\Commands\ServeCommand; /** * @covers \Hyde\Console\Commands\ServeCommand @@ -13,5 +14,94 @@ */ class ServeCommandOptionsUnitTest extends TestCase { - // + public function test_getHostSelection() + { + $command = new ServeCommandMock(); + $this->assertSame('localhost', $command->getHostSelection()); + } + + public function test_getHostSelection_withHostOption() + { + $command = new ServeCommandMock(['host' => 'foo']); + $this->assertSame('foo', $command->getHostSelection()); + } + + public function test_getHostSelection_withConfigOption() + { + $this->app['config']->set('hyde.server.host', 'foo'); + $command = new ServeCommandMock(); + $this->assertSame('foo', $command->getHostSelection()); + } + + public function test_getHostSelection_withHostOptionAndConfigOption() + { + $this->app['config']->set('hyde.server.host', 'foo'); + $command = new ServeCommandMock(['host' => 'bar']); + $this->assertSame('bar', $command->getHostSelection()); + } + + public function test_getPortSelection() + { + $command = new ServeCommandMock(); + $this->assertSame(8080, $command->getPortSelection()); + } + + public function test_getPortSelection_withPortOption() + { + $command = new ServeCommandMock(['port' => 8081]); + $this->assertSame(8081, $command->getPortSelection()); + } + + public function test_getPortSelection_withConfigOption() + { + $this->app['config']->set('hyde.server.port', 8082); + $command = new ServeCommandMock(); + $this->assertSame(8082, $command->getPortSelection()); + } + + public function test_getPortSelection_withPortOptionAndConfigOption() + { + $this->app['config']->set('hyde.server.port', 8082); + $command = new ServeCommandMock(['port' => 8081]); + $this->assertSame(8081, $command->getPortSelection()); + } +} + +/** + * @method getHostSelection + * @method getPortSelection + */ +class ServeCommandMock extends ServeCommand +{ + public function __construct(array $options = []) + { + parent::__construct(); + + $this->input = new InputMock($options); + } + + public function __call($method, $parameters) + { + return call_user_func_array([$this, $method], $parameters); + } + + public function option($key = null) + { + return $this->input->getOption($key); + } +} + +class InputMock +{ + protected array $options; + + public function __construct(array $options = []) + { + $this->options = $options; + } + + public function getOption(string $key) + { + return $this->options[$key] ?? null; + } } From 603bc9b5d2a617637f080a711d7af36942aac8d3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 14:02:17 +0100 Subject: [PATCH 06/52] Inline test variables --- .../Unit/ServeCommandOptionsUnitTest.php | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 79a7c89b6fa..fdad0ef7e9d 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -16,54 +16,46 @@ class ServeCommandOptionsUnitTest extends TestCase { public function test_getHostSelection() { - $command = new ServeCommandMock(); - $this->assertSame('localhost', $command->getHostSelection()); + $this->assertSame('localhost', (new ServeCommandMock())->getHostSelection()); } public function test_getHostSelection_withHostOption() { - $command = new ServeCommandMock(['host' => 'foo']); - $this->assertSame('foo', $command->getHostSelection()); + $this->assertSame('foo', (new ServeCommandMock(['host' => 'foo']))->getHostSelection()); } public function test_getHostSelection_withConfigOption() { $this->app['config']->set('hyde.server.host', 'foo'); - $command = new ServeCommandMock(); - $this->assertSame('foo', $command->getHostSelection()); + $this->assertSame('foo', (new ServeCommandMock())->getHostSelection()); } public function test_getHostSelection_withHostOptionAndConfigOption() { $this->app['config']->set('hyde.server.host', 'foo'); - $command = new ServeCommandMock(['host' => 'bar']); - $this->assertSame('bar', $command->getHostSelection()); + $this->assertSame('bar', (new ServeCommandMock(['host' => 'bar']))->getHostSelection()); } public function test_getPortSelection() { - $command = new ServeCommandMock(); - $this->assertSame(8080, $command->getPortSelection()); + $this->assertSame(8080, (new ServeCommandMock())->getPortSelection()); } public function test_getPortSelection_withPortOption() { - $command = new ServeCommandMock(['port' => 8081]); - $this->assertSame(8081, $command->getPortSelection()); + $this->assertSame(8081, (new ServeCommandMock(['port' => 8081]))->getPortSelection()); } public function test_getPortSelection_withConfigOption() { $this->app['config']->set('hyde.server.port', 8082); - $command = new ServeCommandMock(); - $this->assertSame(8082, $command->getPortSelection()); + $this->assertSame(8082, (new ServeCommandMock())->getPortSelection()); } public function test_getPortSelection_withPortOptionAndConfigOption() { $this->app['config']->set('hyde.server.port', 8082); - $command = new ServeCommandMock(['port' => 8081]); - $this->assertSame(8081, $command->getPortSelection()); + $this->assertSame(8081, (new ServeCommandMock(['port' => 8081]))->getPortSelection()); } } From 21989a4f6f817e7eecf469b53afcf7dbdb456a93 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 14:03:46 +0100 Subject: [PATCH 07/52] Extract test method --- .../Unit/ServeCommandOptionsUnitTest.php | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index fdad0ef7e9d..f083d107c2c 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -16,46 +16,51 @@ class ServeCommandOptionsUnitTest extends TestCase { public function test_getHostSelection() { - $this->assertSame('localhost', (new ServeCommandMock())->getHostSelection()); + $this->assertSame('localhost', $this->getMock()->getHostSelection()); } public function test_getHostSelection_withHostOption() { - $this->assertSame('foo', (new ServeCommandMock(['host' => 'foo']))->getHostSelection()); + $this->assertSame('foo', $this->getMock(['host' => 'foo'])->getHostSelection()); } public function test_getHostSelection_withConfigOption() { $this->app['config']->set('hyde.server.host', 'foo'); - $this->assertSame('foo', (new ServeCommandMock())->getHostSelection()); + $this->assertSame('foo', $this->getMock()->getHostSelection()); } public function test_getHostSelection_withHostOptionAndConfigOption() { $this->app['config']->set('hyde.server.host', 'foo'); - $this->assertSame('bar', (new ServeCommandMock(['host' => 'bar']))->getHostSelection()); + $this->assertSame('bar', $this->getMock(['host' => 'bar'])->getHostSelection()); } public function test_getPortSelection() { - $this->assertSame(8080, (new ServeCommandMock())->getPortSelection()); + $this->assertSame(8080, $this->getMock()->getPortSelection()); } public function test_getPortSelection_withPortOption() { - $this->assertSame(8081, (new ServeCommandMock(['port' => 8081]))->getPortSelection()); + $this->assertSame(8081, $this->getMock(['port' => 8081])->getPortSelection()); } public function test_getPortSelection_withConfigOption() { $this->app['config']->set('hyde.server.port', 8082); - $this->assertSame(8082, (new ServeCommandMock())->getPortSelection()); + $this->assertSame(8082, $this->getMock()->getPortSelection()); } public function test_getPortSelection_withPortOptionAndConfigOption() { $this->app['config']->set('hyde.server.port', 8082); - $this->assertSame(8081, (new ServeCommandMock(['port' => 8081]))->getPortSelection()); + $this->assertSame(8081, $this->getMock(['port' => 8081])->getPortSelection()); + } + + protected function getMock(array $options = []): ServeCommandMock + { + return new ServeCommandMock($options); } } From de874bece8c15e12c5a7a847fe5c1b6d2c93d766 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 15:01:10 +0100 Subject: [PATCH 08/52] Create serve command option to enable/disable dashboard --- .../src/Console/Commands/ServeCommand.php | 9 ++++ .../Unit/ServeCommandOptionsUnitTest.php | 41 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 15b920f6ea6..2a3860460fe 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -25,6 +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)} '; /** @var string */ @@ -56,6 +57,13 @@ protected function getPortSelection(): int return (int) ($this->option('port') ?: Config::getInt('hyde.server.port', 8080)); } + protected function getDashboardSelection(): bool + { + return $this->option('dashboard') !== null + ? (bool) $this->option('dashboard') + : Config::getBool('hyde.server.dashboard.enabled', true); + } + protected function getExecutablePath(): string { return Hyde::path('vendor/hyde/realtime-compiler/bin/server.php'); @@ -70,6 +78,7 @@ protected function getEnvironmentVariables(): array { return [ 'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'), + 'SERVER_DASHBOARD' => $this->getDashboardSelection(), ]; } diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index f083d107c2c..607af78ecfb 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -58,6 +58,45 @@ public function test_getPortSelection_withPortOptionAndConfigOption() $this->assertSame(8081, $this->getMock(['port' => 8081])->getPortSelection()); } + public function test_getDashboardSelection() + { + $this->assertSame(true, $this->getMock()->getDashboardSelection()); + } + + public function test_getDashboardSelection_withDashboardOption() + { + $this->assertSame(false, $this->getMock(['dashboard' => false])->getDashboardSelection()); + } + + public function test_getDashboardSelection_withConfigOption() + { + $this->app['config']->set('hyde.server.dashboard.enabled', false); + $this->assertSame(false, $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()); + } + + 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->app['config']->set('hyde.server.dashboard.enabled', true); + $this->assertSame(true, $command->getEnvironmentVariables()['SERVER_DASHBOARD']); + + $command = $this->getMock(['dashboard' => false]); + $this->assertSame(false, $command->getEnvironmentVariables()['SERVER_DASHBOARD']); + + $command = $this->getMock(['dashboard' => true]); + $this->assertSame(true, $command->getEnvironmentVariables()['SERVER_DASHBOARD']); + } + protected function getMock(array $options = []): ServeCommandMock { return new ServeCommandMock($options); @@ -67,6 +106,8 @@ protected function getMock(array $options = []): ServeCommandMock /** * @method getHostSelection * @method getPortSelection + * @method getDashboardSelection + * @method getEnvironmentVariables */ class ServeCommandMock extends ServeCommand { From dfd9efefdd4f7a213eff992f6c23302a14b08e4a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 15:12:47 +0100 Subject: [PATCH 09/52] Extract if condition to block --- .../Foundation/Internal/LoadConfiguration.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index b4b51bf41ca..6c9b0e91af2 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -80,15 +80,17 @@ private static function providePharSupportIfNeeded(array &$files): void private function loadRuntimeConfiguration(Application $app, RepositoryContract $repository): void { - if ($app->runningInConsole() && isset($_SERVER['argv'])) { - // Check if the `--pretty-urls` CLI argument is set, and if so, set the config value accordingly. - if (in_array('--pretty-urls', $_SERVER['argv'], true)) { - $repository->set('hyde.pretty_urls', true); - } - - // Check if the `--no-api` CLI argument is set, and if so, set the config value accordingly. - if (in_array('--no-api', $_SERVER['argv'], true)) { - $repository->set('hyde.api_calls', false); + if ($app->runningInConsole()) { + if (isset($_SERVER['argv'])) { + // Check if the `--pretty-urls` CLI argument is set, and if so, set the config value accordingly. + if (in_array('--pretty-urls', $_SERVER['argv'], true)) { + $repository->set('hyde.pretty_urls', true); + } + + // Check if the `--no-api` CLI argument is set, and if so, set the config value accordingly. + if (in_array('--no-api', $_SERVER['argv'], true)) { + $repository->set('hyde.api_calls', false); + } } } } From b3f46e5fa98b1d658d8f8986c2d7b828c3e53db3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 15:26:12 +0100 Subject: [PATCH 10/52] 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 From 3fe6ad8ecd1d5f7319c5f934d9262b772cb6d13d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 15:42:01 +0100 Subject: [PATCH 11/52] Open up logic for testing --- .../Foundation/Internal/LoadConfiguration.php | 15 ++++++++---- .../tests/Unit/LoadConfigurationTest.php | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index db1f13f5895..7c1243762ca 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -92,12 +92,17 @@ 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'); - } + } + + // Check if HYDE_RC_SERVER_DASHBOARD environment variable is set, and if so, set the config value accordingly. + if ($this->getEnv('HYDE_RC_SERVER_DASHBOARD') !== false) { + $repository->set('hyde.server.dashboard.enabled', $this->getEnv('HYDE_RC_SERVER_DASHBOARD') === 'enabled'); } } } + + protected function getEnv(string $name): string|false + { + return getenv($name); + } } diff --git a/packages/framework/tests/Unit/LoadConfigurationTest.php b/packages/framework/tests/Unit/LoadConfigurationTest.php index d1125608255..f37e13f0c26 100644 --- a/packages/framework/tests/Unit/LoadConfigurationTest.php +++ b/packages/framework/tests/Unit/LoadConfigurationTest.php @@ -33,4 +33,28 @@ public function testItLoadsRuntimeConfiguration() $this->assertFalse(config('hyde.pretty_urls')); $this->assertNull(config('hyde.api_calls')); } + + public function testItLoadsRealtimeCompilerEnvironmentConfiguration() + { + (new LoadConfigurationEnvironmentTestClass(['HYDE_RC_SERVER_DASHBOARD' => 'enabled']))->bootstrap(new Application(getcwd())); + $this->assertTrue(config('hyde.server.dashboard.enabled')); + + (new LoadConfigurationEnvironmentTestClass(['HYDE_RC_SERVER_DASHBOARD' => 'disabled']))->bootstrap(new Application(getcwd())); + $this->assertFalse(config('hyde.server.dashboard.enabled')); + } +} + +class LoadConfigurationEnvironmentTestClass extends LoadConfiguration +{ + protected array $env; + + public function __construct(array $env) + { + $this->env = $env; + } + + protected function getEnv(string $name): string|false + { + return $this->env[$name]; + } } From 52571f67c5d6083c04b2936bd436078f34a97fb5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 15:48:33 +0100 Subject: [PATCH 12/52] Simplify dashboard option logic --- .../src/Console/Commands/ServeCommand.php | 11 +---- .../Unit/ServeCommandOptionsUnitTest.php | 42 ++++--------------- 2 files changed, 11 insertions(+), 42 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 8f778617f48..7c201a40b3a 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -57,13 +57,6 @@ protected function getPortSelection(): int return (int) ($this->option('port') ?: Config::getInt('hyde.server.port', 8080)); } - protected function getDashboardSelection(): ?bool - { - return $this->option('dashboard') !== null - ? $this->option('dashboard') !== 'false' - : null; - } - protected function getExecutablePath(): string { return Hyde::path('vendor/hyde/realtime-compiler/bin/server.php'); @@ -79,8 +72,8 @@ protected function getEnvironmentVariables(): array $vars = [ 'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'), ]; - if ($this->getDashboardSelection() !== null) { - $vars['HYDE_RC_SERVER_DASHBOARD'] = $this->getDashboardSelection() ? 'enabled' : 'disabled'; + if ($this->option('dashboard') !== null) { + $vars['HYDE_RC_SERVER_DASHBOARD'] = $this->option('dashboard') !== 'false' ? 'enabled' : 'disabled'; } return $vars; diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 5065f81991f..7e4ecc62078 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -58,45 +58,22 @@ public function test_getPortSelection_withPortOptionAndConfigOption() $this->assertSame(8081, $this->getMock(['port' => 8081])->getPortSelection()); } - public function test_getDashboardSelection() - { - $this->assertSame(null, $this->getMock()->getDashboardSelection()); - } - - public function test_getDashboardSelection_withDashboardOption() - { - $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(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()); - } - public function test_getDashboardSelection_propagatesToEnvironmentVariables() { - $command = $this->getMock(); - - $this->app['config']->set('hyde.server.dashboard.enabled', false); - $this->assertSame(false, isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); - - $this->app['config']->set('hyde.server.dashboard.enabled', true); - $this->assertSame(false, isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); - $command = $this->getMock(['dashboard' => 'false']); $this->assertSame('disabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); $command = $this->getMock(['dashboard' => 'true']); $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); + + $command = $this->getMock(['dashboard' => '']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); + + $command = $this->getMock(['dashboard' => null]); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); + + $command = $this->getMock(); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); } protected function getMock(array $options = []): ServeCommandMock @@ -108,7 +85,6 @@ protected function getMock(array $options = []): ServeCommandMock /** * @method getHostSelection * @method getPortSelection - * @method getDashboardSelection * @method getEnvironmentVariables */ class ServeCommandMock extends ServeCommand From c07f678296635ea166c30e455186eb00dd31e341 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 18:47:18 +0100 Subject: [PATCH 13/52] Add serve command runtime option for pretty URLs --- packages/framework/src/Console/Commands/ServeCommand.php | 4 ++++ .../framework/src/Foundation/Internal/LoadConfiguration.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 7c201a40b3a..4257b10a2e1 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -26,6 +26,7 @@ class ServeCommand extends Command {--host= : [default: "localhost"]}} {--port= : [default: 8080]} {--dashboard= : Enable the realtime compiler dashboard. (Overrides config setting)} + {--pretty-urls= : Enable pretty URLs. (Overrides config setting)} '; /** @var string */ @@ -75,6 +76,9 @@ protected function getEnvironmentVariables(): array if ($this->option('dashboard') !== null) { $vars['HYDE_RC_SERVER_DASHBOARD'] = $this->option('dashboard') !== 'false' ? 'enabled' : 'disabled'; } + if ($this->option('pretty-urls') !== null) { + $vars['HYDE_PRETTY_URLS'] = $this->option('pretty-urls') !== 'false' ? 'enabled' : 'disabled'; + } return $vars; } diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index 7c1243762ca..d3ec0b705e2 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -98,6 +98,11 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $ if ($this->getEnv('HYDE_RC_SERVER_DASHBOARD') !== false) { $repository->set('hyde.server.dashboard.enabled', $this->getEnv('HYDE_RC_SERVER_DASHBOARD') === 'enabled'); } + + // Check if HYDE_PRETTY_URLS environment variable is set, and if so, set the config value accordingly. + if ($this->getEnv('HYDE_PRETTY_URLS') !== false) { + $repository->set('hyde.pretty_urls', $this->getEnv('HYDE_PRETTY_URLS') === 'enabled'); + } } } From 8d82ee4163b2dedb0c03e6a34e6dc36ec54ef0be Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 18:47:49 +0100 Subject: [PATCH 14/52] Add newlines --- packages/framework/src/Console/Commands/ServeCommand.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 4257b10a2e1..ccbd9577cdd 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -73,9 +73,11 @@ protected function getEnvironmentVariables(): array $vars = [ 'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'), ]; + if ($this->option('dashboard') !== null) { $vars['HYDE_RC_SERVER_DASHBOARD'] = $this->option('dashboard') !== 'false' ? 'enabled' : 'disabled'; } + if ($this->option('pretty-urls') !== null) { $vars['HYDE_PRETTY_URLS'] = $this->option('pretty-urls') !== 'false' ? 'enabled' : 'disabled'; } From b59b04c652c63e6027f5648e6f4b2daf3db8bfd1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 18:48:15 +0100 Subject: [PATCH 15/52] Rename test to match feature --- packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 7e4ecc62078..439d4c89026 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -58,7 +58,7 @@ public function test_getPortSelection_withPortOptionAndConfigOption() $this->assertSame(8081, $this->getMock(['port' => 8081])->getPortSelection()); } - public function test_getDashboardSelection_propagatesToEnvironmentVariables() + public function testDashboardOptionPropagatesToEnvironmentVariables() { $command = $this->getMock(['dashboard' => 'false']); $this->assertSame('disabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); From ed77428d6fe52a3c5c1950f80e079c59ba611df5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 18:48:55 +0100 Subject: [PATCH 16/52] Unit test added option --- .../tests/Unit/ServeCommandOptionsUnitTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 439d4c89026..b8be7cfa885 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -76,6 +76,24 @@ public function testDashboardOptionPropagatesToEnvironmentVariables() $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); } + public function testPrettyUrlsOptionPropagatesToEnvironmentVariables() + { + $command = $this->getMock(['pretty-urls' => 'false']); + $this->assertSame('disabled', $command->getEnvironmentVariables()['HYDE_PRETTY_URLS']); + + $command = $this->getMock(['pretty-urls' => 'true']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_PRETTY_URLS']); + + $command = $this->getMock(['pretty-urls' => '']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_PRETTY_URLS']); + + $command = $this->getMock(['pretty-urls' => null]); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_PRETTY_URLS'])); + + $command = $this->getMock(); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_PRETTY_URLS'])); + } + protected function getMock(array $options = []): ServeCommandMock { return new ServeCommandMock($options); From 7e97e3d51a88d2a3207f3e3bead404f865ef4621 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 18:50:58 +0100 Subject: [PATCH 17/52] Refactor unit test case to extend proper unit test case class --- .../Unit/ServeCommandOptionsUnitTest.php | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index b8be7cfa885..245adcc75a6 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -4,7 +4,7 @@ namespace Hyde\Framework\Testing\Unit; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; use Hyde\Console\Commands\ServeCommand; /** @@ -12,8 +12,16 @@ * * @see \Hyde\Framework\Testing\Feature\Commands\ServeCommandTest */ -class ServeCommandOptionsUnitTest extends TestCase +class ServeCommandOptionsUnitTest extends UnitTestCase { + protected function setUp(): void + { + self::mockConfig([ + 'hyde.server.host' => 'localhost', + 'hyde.server.port' => 8080, + ]); + } + public function test_getHostSelection() { $this->assertSame('localhost', $this->getMock()->getHostSelection()); @@ -26,13 +34,13 @@ public function test_getHostSelection_withHostOption() public function test_getHostSelection_withConfigOption() { - $this->app['config']->set('hyde.server.host', 'foo'); + self::mockConfig(['hyde.server.host' => 'foo']); $this->assertSame('foo', $this->getMock()->getHostSelection()); } public function test_getHostSelection_withHostOptionAndConfigOption() { - $this->app['config']->set('hyde.server.host', 'foo'); + self::mockConfig(['hyde.server.host' => 'foo']); $this->assertSame('bar', $this->getMock(['host' => 'bar'])->getHostSelection()); } @@ -48,13 +56,13 @@ public function test_getPortSelection_withPortOption() public function test_getPortSelection_withConfigOption() { - $this->app['config']->set('hyde.server.port', 8082); + self::mockConfig(['hyde.server.port' => 8082]); $this->assertSame(8082, $this->getMock()->getPortSelection()); } public function test_getPortSelection_withPortOptionAndConfigOption() { - $this->app['config']->set('hyde.server.port', 8082); + self::mockConfig(['hyde.server.port' => 8082]); $this->assertSame(8081, $this->getMock(['port' => 8081])->getPortSelection()); } From f341cdc5f188efd3407d10ab9b558d785fa172be Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:00:44 +0100 Subject: [PATCH 18/52] Allow null to be returned --- .../framework/src/Foundation/Internal/LoadConfiguration.php | 2 +- packages/framework/tests/Unit/LoadConfigurationTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index d3ec0b705e2..9413fd0d768 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -106,7 +106,7 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $ } } - protected function getEnv(string $name): string|false + protected function getEnv(string $name): string|false|null { return getenv($name); } diff --git a/packages/framework/tests/Unit/LoadConfigurationTest.php b/packages/framework/tests/Unit/LoadConfigurationTest.php index f37e13f0c26..a6ce24ef142 100644 --- a/packages/framework/tests/Unit/LoadConfigurationTest.php +++ b/packages/framework/tests/Unit/LoadConfigurationTest.php @@ -53,7 +53,7 @@ public function __construct(array $env) $this->env = $env; } - protected function getEnv(string $name): string|false + protected function getEnv(string $name): string|false|null { return $this->env[$name]; } From dc6c528176a598fdeb07416bdae41d868a7b0dfa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:04:44 +0100 Subject: [PATCH 19/52] Add unit test --- .../framework/tests/Unit/ServeCommandOptionsUnitTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 245adcc75a6..910e12fedfa 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -66,6 +66,13 @@ public function test_getPortSelection_withPortOptionAndConfigOption() $this->assertSame(8081, $this->getMock(['port' => 8081])->getPortSelection()); } + public function test_getEnvironmentVariables() + { + $this->assertSame([ + 'HYDE_RC_REQUEST_OUTPUT' => true, + ], $this->getMock()->getEnvironmentVariables()); + } + public function testDashboardOptionPropagatesToEnvironmentVariables() { $command = $this->getMock(['dashboard' => 'false']); From 8e90f4886fd76a3e2f21b936f4098083c39c96bb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:08:44 +0100 Subject: [PATCH 20/52] Refactor to extract helper method --- .../src/Console/Commands/ServeCommand.php | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index ccbd9577cdd..0e9a4627f88 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -7,6 +7,7 @@ use Closure; use Hyde\Hyde; use Hyde\Facades\Config; +use Illuminate\Support\Arr; use Hyde\RealtimeCompiler\ConsoleOutput; use Illuminate\Support\Facades\Process; use LaravelZero\Framework\Commands\Command; @@ -74,15 +75,10 @@ protected function getEnvironmentVariables(): array 'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'), ]; - if ($this->option('dashboard') !== null) { - $vars['HYDE_RC_SERVER_DASHBOARD'] = $this->option('dashboard') !== 'false' ? 'enabled' : 'disabled'; - } - - if ($this->option('pretty-urls') !== null) { - $vars['HYDE_PRETTY_URLS'] = $this->option('pretty-urls') !== 'false' ? 'enabled' : 'disabled'; - } + $vars['HYDE_RC_SERVER_DASHBOARD'] = $this->parseEnvironmentOption('dashboard'); + $vars['HYDE_PRETTY_URLS'] = $this->parseEnvironmentOption('pretty-urls'); - return $vars; + return Arr::whereNotNull($vars); } protected function configureOutput(): void @@ -110,4 +106,13 @@ protected function useBasicOutput(): bool { return $this->option('no-ansi') || ! class_exists(ConsoleOutput::class); } + + protected function parseEnvironmentOption(string $name): ?string + { + if ($this->option($name) !== null) { + return $this->option($name) !== 'false' ? 'enabled' : 'disabled'; + } else { + return null; + } + } } From 4374437686903d3ff818315dde148236747ca87e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:09:58 +0100 Subject: [PATCH 21/52] Unwrap 'else' --- packages/framework/src/Console/Commands/ServeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 0e9a4627f88..c17557fc8d7 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -111,8 +111,8 @@ protected function parseEnvironmentOption(string $name): ?string { if ($this->option($name) !== null) { return $this->option($name) !== 'false' ? 'enabled' : 'disabled'; - } else { - return null; } + + return null; } } From dc6a8fd8c2cd4000550ecdc22c001c2ebc887467 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:13:02 +0100 Subject: [PATCH 22/52] Add unit test --- .../framework/tests/Unit/ServeCommandOptionsUnitTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 910e12fedfa..1179a32d852 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -73,6 +73,13 @@ public function test_getEnvironmentVariables() ], $this->getMock()->getEnvironmentVariables()); } + public function test_getEnvironmentVariables_withNoAnsiOption() + { + $this->assertSame([ + 'HYDE_RC_REQUEST_OUTPUT' => false, + ], $this->getMock(['no-ansi' => true])->getEnvironmentVariables()); + } + public function testDashboardOptionPropagatesToEnvironmentVariables() { $command = $this->getMock(['dashboard' => 'false']); From dcbf1acf406334f9816ccbeaffe11f6375ed49fa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:28:04 +0100 Subject: [PATCH 23/52] Extend our validating command class --- packages/framework/src/Console/Commands/ServeCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index c17557fc8d7..f924de84ff3 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -11,6 +11,7 @@ use Hyde\RealtimeCompiler\ConsoleOutput; use Illuminate\Support\Facades\Process; use LaravelZero\Framework\Commands\Command; +use Hyde\Publications\Commands\ValidatingCommand; use function sprintf; use function class_exists; @@ -20,7 +21,7 @@ * * @see https://github.com/hydephp/realtime-compiler */ -class ServeCommand extends Command +class ServeCommand extends ValidatingCommand { /** @var string */ protected $signature = 'serve @@ -35,7 +36,7 @@ class ServeCommand extends Command protected ConsoleOutput $console; - public function handle(): int + public function safeHandle(): int { $this->configureOutput(); $this->printStartMessage(); From c46db81c9e04f009907b7f429f08b16008947979 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:28:34 +0100 Subject: [PATCH 24/52] Replace ternary return value with match expression --- packages/framework/src/Console/Commands/ServeCommand.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index f924de84ff3..e803cba4ec9 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -111,7 +111,11 @@ protected function useBasicOutput(): bool protected function parseEnvironmentOption(string $name): ?string { if ($this->option($name) !== null) { - return $this->option($name) !== 'false' ? 'enabled' : 'disabled'; + return match ($this->option($name)) { + 'true', '' => 'enabled', + 'false' => 'disabled', + default => null + }; } return null; From 697e3ebc97efa697214569d65eccc05ac062a747 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:29:29 +0100 Subject: [PATCH 25/52] Throw exception when supplied with invalid boolean value --- packages/framework/src/Console/Commands/ServeCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index e803cba4ec9..72ea15c8de6 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -8,6 +8,7 @@ use Hyde\Hyde; use Hyde\Facades\Config; use Illuminate\Support\Arr; +use InvalidArgumentException; use Hyde\RealtimeCompiler\ConsoleOutput; use Illuminate\Support\Facades\Process; use LaravelZero\Framework\Commands\Command; @@ -114,7 +115,7 @@ protected function parseEnvironmentOption(string $name): ?string return match ($this->option($name)) { 'true', '' => 'enabled', 'false' => 'disabled', - default => null + default => throw new InvalidArgumentException(sprintf('Invalid boolean value for --%s option.', $name)) }; } From bb2ef6f6690118033211f1c72ee7a18fcdbe8a33 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:31:24 +0100 Subject: [PATCH 26/52] Introduce local variable --- packages/framework/src/Console/Commands/ServeCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 72ea15c8de6..a178da6500f 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -111,8 +111,10 @@ protected function useBasicOutput(): bool protected function parseEnvironmentOption(string $name): ?string { - if ($this->option($name) !== null) { - return match ($this->option($name)) { + $value = $this->option($name); + + if ($value !== null) { + return match ($value) { 'true', '' => 'enabled', 'false' => 'disabled', default => throw new InvalidArgumentException(sprintf('Invalid boolean value for --%s option.', $name)) From 9d050cdf26ecd4a91dde25bc7e1e76a001e79d2a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 19:34:29 +0100 Subject: [PATCH 27/52] Add fallback for blank option value --- .../src/Console/Commands/ServeCommand.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index a178da6500f..479189ee4be 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -111,7 +111,7 @@ protected function useBasicOutput(): bool protected function parseEnvironmentOption(string $name): ?string { - $value = $this->option($name); + $value = $this->option($name) ?? $this->checkArgvForOption($name); if ($value !== null) { return match ($value) { @@ -123,4 +123,16 @@ protected function parseEnvironmentOption(string $name): ?string return null; } + + /** Fallback check so that an environment option without a value is acknowledged as true. */ + protected function checkArgvForOption(string $name): ?string + { + if (isset($_SERVER['argv'])) { + if (in_array("--$name", $_SERVER['argv'], true)) { + return 'true'; + } + } + + return null; + } } From 1db079f9de902b1188ed571dd2e7c858f4a9a2cd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 20:43:24 +0100 Subject: [PATCH 28/52] Add unit tests --- .../Unit/ServeCommandOptionsUnitTest.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 1179a32d852..6b4e9069d1d 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -116,6 +116,50 @@ public function testPrettyUrlsOptionPropagatesToEnvironmentVariables() $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_PRETTY_URLS'])); } + public function test_parseEnvironmentOption() + { + $command = $this->getMock(['foo' => 'true']); + $this->assertSame('enabled', $command->parseEnvironmentOption('foo')); + + $command = $this->getMock(['foo' => 'false']); + $this->assertSame('disabled', $command->parseEnvironmentOption('foo')); + } + + public function test_parseEnvironmentOption_withEmptyString() + { + $command = $this->getMock(['foo' => '']); + $this->assertSame('enabled', $command->parseEnvironmentOption('foo')); + } + + public function test_parseEnvironmentOption_withNull() + { + $command = $this->getMock(['foo' => null]); + $this->assertNull($command->parseEnvironmentOption('foo')); + } + + public function test_parseEnvironmentOption_withInvalidValue() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid boolean value for --foo option.'); + + $command = $this->getMock(['foo' => 'bar']); + $command->parseEnvironmentOption('foo'); + } + + public function test_checkArgvForOption() + { + $serverBackup = $_SERVER; + + $_SERVER['argv'] = ['--pretty-urls']; + + $command = $this->getMock(); + + $this->assertSame('true', $command->checkArgvForOption('pretty-urls')); + $this->assertSame(null, $command->checkArgvForOption('dashboard')); + + $_SERVER = $serverBackup; + } + protected function getMock(array $options = []): ServeCommandMock { return new ServeCommandMock($options); @@ -126,6 +170,8 @@ protected function getMock(array $options = []): ServeCommandMock * @method getHostSelection * @method getPortSelection * @method getEnvironmentVariables + * @method parseEnvironmentOption(string $name) + * @method checkArgvForOption(string $name) */ class ServeCommandMock extends ServeCommand { From 694f589bd0dcc65225a1d2ae9fa2b8b815f18267 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 20:48:56 +0100 Subject: [PATCH 29/52] Merge array operations --- .../framework/src/Console/Commands/ServeCommand.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 479189ee4be..16109a1b638 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -73,14 +73,11 @@ protected function runServerProcess(string $command): void protected function getEnvironmentVariables(): array { - $vars = [ + return Arr::whereNotNull([ 'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'), - ]; - - $vars['HYDE_RC_SERVER_DASHBOARD'] = $this->parseEnvironmentOption('dashboard'); - $vars['HYDE_PRETTY_URLS'] = $this->parseEnvironmentOption('pretty-urls'); - - return Arr::whereNotNull($vars); + 'HYDE_RC_SERVER_DASHBOARD' => $this->parseEnvironmentOption('dashboard'), + 'HYDE_PRETTY_URLS' => $this->parseEnvironmentOption('pretty-urls'), + ]); } protected function configureOutput(): void From e3aadbcfc909c8688930209666acba3fa251d103 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 20:50:55 +0100 Subject: [PATCH 30/52] Normalize added environment keys --- .../src/Console/Commands/ServeCommand.php | 4 ++-- .../src/Foundation/Internal/LoadConfiguration.php | 6 +++--- .../tests/Feature/Commands/ServeCommandTest.php | 2 +- .../framework/tests/Unit/LoadConfigurationTest.php | 4 ++-- .../tests/Unit/ServeCommandOptionsUnitTest.php | 14 +++++++------- packages/realtime-compiler/bin/server.php | 2 +- .../src/Http/DashboardController.php | 2 +- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 16109a1b638..002ed78436f 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -74,8 +74,8 @@ protected function runServerProcess(string $command): void protected function getEnvironmentVariables(): array { return Arr::whereNotNull([ - 'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'), - 'HYDE_RC_SERVER_DASHBOARD' => $this->parseEnvironmentOption('dashboard'), + 'HYDE_SERVER_REQUEST_OUTPUT' => ! $this->option('no-ansi'), + 'HYDE_SERVER_DASHBOARD' => $this->parseEnvironmentOption('dashboard'), 'HYDE_PRETTY_URLS' => $this->parseEnvironmentOption('pretty-urls'), ]); } diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index 9413fd0d768..a1445fea776 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -94,9 +94,9 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $ } } - // Check if HYDE_RC_SERVER_DASHBOARD environment variable is set, and if so, set the config value accordingly. - if ($this->getEnv('HYDE_RC_SERVER_DASHBOARD') !== false) { - $repository->set('hyde.server.dashboard.enabled', $this->getEnv('HYDE_RC_SERVER_DASHBOARD') === 'enabled'); + // Check if HYDE_SERVER_DASHBOARD environment variable is set, and if so, set the config value accordingly. + if ($this->getEnv('HYDE_SERVER_DASHBOARD') !== false) { + $repository->set('hyde.server.dashboard.enabled', $this->getEnv('HYDE_SERVER_DASHBOARD') === 'enabled'); } // Check if HYDE_PRETTY_URLS environment variable is set, and if so, set the config value accordingly. diff --git a/packages/framework/tests/Feature/Commands/ServeCommandTest.php b/packages/framework/tests/Feature/Commands/ServeCommandTest.php index f959d9a01ea..7aa1cd8d41d 100644 --- a/packages/framework/tests/Feature/Commands/ServeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/ServeCommandTest.php @@ -146,7 +146,7 @@ public function test_hyde_serve_command_passes_through_process_output() Process::shouldReceive('env') ->once() - ->with(['HYDE_RC_REQUEST_OUTPUT' => false]) + ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false]) ->andReturnSelf(); Process::shouldReceive('run') diff --git a/packages/framework/tests/Unit/LoadConfigurationTest.php b/packages/framework/tests/Unit/LoadConfigurationTest.php index a6ce24ef142..c80bb45e731 100644 --- a/packages/framework/tests/Unit/LoadConfigurationTest.php +++ b/packages/framework/tests/Unit/LoadConfigurationTest.php @@ -36,10 +36,10 @@ public function testItLoadsRuntimeConfiguration() public function testItLoadsRealtimeCompilerEnvironmentConfiguration() { - (new LoadConfigurationEnvironmentTestClass(['HYDE_RC_SERVER_DASHBOARD' => 'enabled']))->bootstrap(new Application(getcwd())); + (new LoadConfigurationEnvironmentTestClass(['HYDE_SERVER_DASHBOARD' => 'enabled']))->bootstrap(new Application(getcwd())); $this->assertTrue(config('hyde.server.dashboard.enabled')); - (new LoadConfigurationEnvironmentTestClass(['HYDE_RC_SERVER_DASHBOARD' => 'disabled']))->bootstrap(new Application(getcwd())); + (new LoadConfigurationEnvironmentTestClass(['HYDE_SERVER_DASHBOARD' => 'disabled']))->bootstrap(new Application(getcwd())); $this->assertFalse(config('hyde.server.dashboard.enabled')); } } diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 6b4e9069d1d..bd2c959d02a 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -69,33 +69,33 @@ public function test_getPortSelection_withPortOptionAndConfigOption() public function test_getEnvironmentVariables() { $this->assertSame([ - 'HYDE_RC_REQUEST_OUTPUT' => true, + 'HYDE_SERVER_REQUEST_OUTPUT' => true, ], $this->getMock()->getEnvironmentVariables()); } public function test_getEnvironmentVariables_withNoAnsiOption() { $this->assertSame([ - 'HYDE_RC_REQUEST_OUTPUT' => false, + 'HYDE_SERVER_REQUEST_OUTPUT' => false, ], $this->getMock(['no-ansi' => true])->getEnvironmentVariables()); } public function testDashboardOptionPropagatesToEnvironmentVariables() { $command = $this->getMock(['dashboard' => 'false']); - $this->assertSame('disabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); + $this->assertSame('disabled', $command->getEnvironmentVariables()['HYDE_SERVER_DASHBOARD']); $command = $this->getMock(['dashboard' => 'true']); - $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_SERVER_DASHBOARD']); $command = $this->getMock(['dashboard' => '']); - $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_SERVER_DASHBOARD']); $command = $this->getMock(['dashboard' => null]); - $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_SERVER_DASHBOARD'])); $command = $this->getMock(); - $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_RC_SERVER_DASHBOARD'])); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_SERVER_DASHBOARD'])); } public function testPrettyUrlsOptionPropagatesToEnvironmentVariables() diff --git a/packages/realtime-compiler/bin/server.php b/packages/realtime-compiler/bin/server.php index a04f351fb99..01f7dd3559e 100755 --- a/packages/realtime-compiler/bin/server.php +++ b/packages/realtime-compiler/bin/server.php @@ -20,7 +20,7 @@ echo '

Initial exception:

'.print_r($exception, true).'
'; echo '

Auxiliary exception:

'.print_r($th, true).'
'; } finally { - if (getenv('HYDE_RC_REQUEST_OUTPUT')) { + if (getenv('HYDE_SERVER_REQUEST_OUTPUT')) { // Write to console to emulate the standard built-in PHP server output $request = \Desilva\Microserve\Request::capture(); file_put_contents('php://stderr', sprintf( diff --git a/packages/realtime-compiler/src/Http/DashboardController.php b/packages/realtime-compiler/src/Http/DashboardController.php index b129a16aedd..aeb982133d6 100644 --- a/packages/realtime-compiler/src/Http/DashboardController.php +++ b/packages/realtime-compiler/src/Http/DashboardController.php @@ -57,7 +57,7 @@ public function __construct() $this->title = config('hyde.name').' - Dashboard'; $this->request = Request::capture(); - if (((bool) env('HYDE_RC_REQUEST_OUTPUT', false)) === true) { + if (((bool) env('HYDE_SERVER_REQUEST_OUTPUT', false)) === true) { $this->console = new ConsoleOutput(); } From 418fafedb29dfc36b4fd5aa4d0f368b08837b6f9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 22:12:28 +0100 Subject: [PATCH 31/52] Extract helper method --- .../Foundation/Internal/LoadConfiguration.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index a1445fea776..ea97ad8f03d 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -94,15 +94,15 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $ } } - // Check if HYDE_SERVER_DASHBOARD environment variable is set, and if so, set the config value accordingly. - if ($this->getEnv('HYDE_SERVER_DASHBOARD') !== false) { - $repository->set('hyde.server.dashboard.enabled', $this->getEnv('HYDE_SERVER_DASHBOARD') === 'enabled'); - } + $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_DASHBOARD', 'hyde.server.dashboard.enabled'); + $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PRETTY_URLS', 'hyde.pretty_urls'); + } + } - // Check if HYDE_PRETTY_URLS environment variable is set, and if so, set the config value accordingly. - if ($this->getEnv('HYDE_PRETTY_URLS') !== false) { - $repository->set('hyde.pretty_urls', $this->getEnv('HYDE_PRETTY_URLS') === 'enabled'); - } + private function mergeRealtimeCompilerEnvironment(RepositoryContract $repository, string $environmentKey, string $configKey): void + { + if ($this->getEnv($environmentKey) !== false) { + $repository->set($configKey, $this->getEnv($environmentKey) === 'enabled'); } } From 0c7b9145994de81e6534264851e23bae4461dfdc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 22:20:18 +0100 Subject: [PATCH 32/52] Extract method to get arguments This makes it easier to mock without having to mess with the superglobals --- .../Foundation/Internal/LoadConfiguration.php | 11 +++++--- .../tests/Unit/LoadConfigurationTest.php | 25 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index ea97ad8f03d..0e7aa8eb502 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -82,14 +82,14 @@ private static function providePharSupportIfNeeded(array &$files): void private function loadRuntimeConfiguration(Application $app, RepositoryContract $repository): void { if ($app->runningInConsole()) { - if (isset($_SERVER['argv'])) { + if ($this->getArgv() !== null) { // Check if the `--pretty-urls` CLI argument is set, and if so, set the config value accordingly. - if (in_array('--pretty-urls', $_SERVER['argv'], true)) { + if (in_array('--pretty-urls', $this->getArgv(), true)) { $repository->set('hyde.pretty_urls', true); } // Check if the `--no-api` CLI argument is set, and if so, set the config value accordingly. - if (in_array('--no-api', $_SERVER['argv'], true)) { + if (in_array('--no-api', $this->getArgv(), true)) { $repository->set('hyde.api_calls', false); } } @@ -106,6 +106,11 @@ private function mergeRealtimeCompilerEnvironment(RepositoryContract $repository } } + protected function getArgv(): ?array + { + return $_SERVER['argv'] ?? null; + } + protected function getEnv(string $name): string|false|null { return getenv($name); diff --git a/packages/framework/tests/Unit/LoadConfigurationTest.php b/packages/framework/tests/Unit/LoadConfigurationTest.php index c80bb45e731..e0a2e43e9ea 100644 --- a/packages/framework/tests/Unit/LoadConfigurationTest.php +++ b/packages/framework/tests/Unit/LoadConfigurationTest.php @@ -15,21 +15,17 @@ class LoadConfigurationTest extends UnitTestCase { public function testItLoadsRuntimeConfiguration() { - $serverBackup = $_SERVER; - - $_SERVER['argv'] = ['--pretty-urls', '--no-api']; - $app = new Application(getcwd()); - $loader = new LoadConfiguration(); + $loader = new LoadConfigurationTestClass(['--pretty-urls', '--no-api']); $loader->bootstrap($app); $this->assertTrue(config('hyde.pretty_urls')); $this->assertFalse(config('hyde.api_calls')); - $_SERVER = $serverBackup; - + $loader = new LoadConfigurationTestClass([]); $loader->bootstrap($app); + $this->assertFalse(config('hyde.pretty_urls')); $this->assertNull(config('hyde.api_calls')); } @@ -44,6 +40,21 @@ public function testItLoadsRealtimeCompilerEnvironmentConfiguration() } } +class LoadConfigurationTestClass extends LoadConfiguration +{ + protected array $argv; + + public function __construct(array $argv) + { + $this->argv = $argv; + } + + protected function getArgv(): ?array + { + return $this->argv; + } +} + class LoadConfigurationEnvironmentTestClass extends LoadConfiguration { protected array $env; From e3c84414c73543a77386fec1a33d07511cc548b0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 11 Nov 2023 22:20:31 +0100 Subject: [PATCH 33/52] Swap assertion order --- .../framework/tests/Unit/LoadConfigurationTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/framework/tests/Unit/LoadConfigurationTest.php b/packages/framework/tests/Unit/LoadConfigurationTest.php index e0a2e43e9ea..4358e8bfd6e 100644 --- a/packages/framework/tests/Unit/LoadConfigurationTest.php +++ b/packages/framework/tests/Unit/LoadConfigurationTest.php @@ -17,17 +17,17 @@ public function testItLoadsRuntimeConfiguration() { $app = new Application(getcwd()); - $loader = new LoadConfigurationTestClass(['--pretty-urls', '--no-api']); - $loader->bootstrap($app); - - $this->assertTrue(config('hyde.pretty_urls')); - $this->assertFalse(config('hyde.api_calls')); - $loader = new LoadConfigurationTestClass([]); $loader->bootstrap($app); $this->assertFalse(config('hyde.pretty_urls')); $this->assertNull(config('hyde.api_calls')); + + $loader = new LoadConfigurationTestClass(['--pretty-urls', '--no-api']); + $loader->bootstrap($app); + + $this->assertTrue(config('hyde.pretty_urls')); + $this->assertFalse(config('hyde.api_calls')); } public function testItLoadsRealtimeCompilerEnvironmentConfiguration() From 03ef853233de98cd7a94eb0326828acd4bad498d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 09:56:42 +0100 Subject: [PATCH 34/52] Extract helper method --- .../Foundation/Internal/LoadConfiguration.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index 0e7aa8eb502..450d7ffd513 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -83,15 +83,8 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $ { if ($app->runningInConsole()) { if ($this->getArgv() !== null) { - // Check if the `--pretty-urls` CLI argument is set, and if so, set the config value accordingly. - if (in_array('--pretty-urls', $this->getArgv(), true)) { - $repository->set('hyde.pretty_urls', true); - } - - // Check if the `--no-api` CLI argument is set, and if so, set the config value accordingly. - if (in_array('--no-api', $this->getArgv(), true)) { - $repository->set('hyde.api_calls', false); - } + $this->mergeCommandLineArguments($repository, '--pretty-urls', 'hyde.pretty_urls', true); + $this->mergeCommandLineArguments($repository, '--no-api', 'hyde.api_calls', false); } $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_DASHBOARD', 'hyde.server.dashboard.enabled'); @@ -99,6 +92,13 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $ } } + private function mergeCommandLineArguments(RepositoryContract $repository, string $argumentName, string $configKey, bool $value): void + { + if (in_array($argumentName, $this->getArgv(), true)) { + $repository->set($configKey, $value); + } + } + private function mergeRealtimeCompilerEnvironment(RepositoryContract $repository, string $environmentKey, string $configKey): void { if ($this->getEnv($environmentKey) !== false) { From 4855a3f9259cf871aaa18151e2250fa2cef7316d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 10:00:33 +0100 Subject: [PATCH 35/52] Inline alias --- .../src/Foundation/Internal/LoadConfiguration.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index 450d7ffd513..3079c9258a8 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -6,7 +6,7 @@ use Phar; use Illuminate\Contracts\Foundation\Application; -use Illuminate\Contracts\Config\Repository as RepositoryContract; +use Illuminate\Contracts\Config\Repository; use Illuminate\Foundation\Bootstrap\LoadConfiguration as BaseLoadConfiguration; use function getenv; @@ -31,7 +31,7 @@ protected function getConfigurationFiles(Application $app): array } /** Load the configuration items from all the files. */ - protected function loadConfigurationFiles(Application $app, RepositoryContract $repository): void + protected function loadConfigurationFiles(Application $app, Repository $repository): void { parent::loadConfigurationFiles($app, $repository); @@ -40,7 +40,7 @@ protected function loadConfigurationFiles(Application $app, RepositoryContract $ $this->loadRuntimeConfiguration($app, $repository); } - private function mergeConfigurationFiles(RepositoryContract $repository): void + private function mergeConfigurationFiles(Repository $repository): void { // These files do commonly not need to be customized by the user, so to get them out of the way, // we don't include them in the default project install. @@ -50,7 +50,7 @@ private function mergeConfigurationFiles(RepositoryContract $repository): void } } - private function mergeConfigurationFile(RepositoryContract $repository, string $file): void + private function mergeConfigurationFile(Repository $repository, string $file): void { // We of course want the user to be able to customize the config files, // if they're present, so we'll merge their changes here. @@ -79,7 +79,7 @@ private static function providePharSupportIfNeeded(array &$files): void } } - private function loadRuntimeConfiguration(Application $app, RepositoryContract $repository): void + private function loadRuntimeConfiguration(Application $app, Repository $repository): void { if ($app->runningInConsole()) { if ($this->getArgv() !== null) { @@ -92,14 +92,14 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $ } } - private function mergeCommandLineArguments(RepositoryContract $repository, string $argumentName, string $configKey, bool $value): void + private function mergeCommandLineArguments(Repository $repository, string $argumentName, string $configKey, bool $value): void { if (in_array($argumentName, $this->getArgv(), true)) { $repository->set($configKey, $value); } } - private function mergeRealtimeCompilerEnvironment(RepositoryContract $repository, string $environmentKey, string $configKey): void + private function mergeRealtimeCompilerEnvironment(Repository $repository, string $environmentKey, string $configKey): void { if ($this->getEnv($environmentKey) !== false) { $repository->set($configKey, $this->getEnv($environmentKey) === 'enabled'); From a3b45ed87c953c124cddc3cdbc9658473f4853a9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 10:04:20 +0100 Subject: [PATCH 36/52] Add serve command runtime option for Play CDN --- .../src/Console/Commands/ServeCommand.php | 2 ++ .../Foundation/Internal/LoadConfiguration.php | 1 + .../tests/Unit/ServeCommandOptionsUnitTest.php | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 002ed78436f..166ea34e4ac 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -30,6 +30,7 @@ class ServeCommand extends ValidatingCommand {--port= : [default: 8080]} {--dashboard= : Enable the realtime compiler dashboard. (Overrides config setting)} {--pretty-urls= : Enable pretty URLs. (Overrides config setting)} + {--play-cdn= : Enable the Tailwind Play CDN. (Overrides config setting)} '; /** @var string */ @@ -77,6 +78,7 @@ protected function getEnvironmentVariables(): array 'HYDE_SERVER_REQUEST_OUTPUT' => ! $this->option('no-ansi'), 'HYDE_SERVER_DASHBOARD' => $this->parseEnvironmentOption('dashboard'), 'HYDE_PRETTY_URLS' => $this->parseEnvironmentOption('pretty-urls'), + 'HYDE_PLAY_CDN' => $this->parseEnvironmentOption('play-cdn'), ]); } diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index 3079c9258a8..5fa364ee040 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -89,6 +89,7 @@ private function loadRuntimeConfiguration(Application $app, Repository $reposito $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_DASHBOARD', 'hyde.server.dashboard.enabled'); $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PRETTY_URLS', 'hyde.pretty_urls'); + $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PLAY_CDN', 'hyde.use_play_cdn'); } } diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index bd2c959d02a..605d24ecc81 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -116,6 +116,24 @@ public function testPrettyUrlsOptionPropagatesToEnvironmentVariables() $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_PRETTY_URLS'])); } + public function testPlayCdnOptionPropagatesToEnvironmentVariables() + { + $command = $this->getMock(['play-cdn' => 'false']); + $this->assertSame('disabled', $command->getEnvironmentVariables()['HYDE_PLAY_CDN']); + + $command = $this->getMock(['play-cdn' => 'true']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_PLAY_CDN']); + + $command = $this->getMock(['play-cdn' => '']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_PLAY_CDN']); + + $command = $this->getMock(['play-cdn' => null]); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_PLAY_CDN'])); + + $command = $this->getMock(); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_PLAY_CDN'])); + } + public function test_parseEnvironmentOption() { $command = $this->getMock(['foo' => 'true']); From af25590a73f8343e5f2e4b17c4b739d136ef996c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 10:09:37 +0100 Subject: [PATCH 37/52] Add serve command runtime option for preview saving --- .../src/Console/Commands/ServeCommand.php | 2 ++ .../Foundation/Internal/LoadConfiguration.php | 1 + .../tests/Unit/ServeCommandOptionsUnitTest.php | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index 166ea34e4ac..adcea1806cd 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -28,6 +28,7 @@ class ServeCommand extends ValidatingCommand protected $signature = 'serve {--host= : [default: "localhost"]}} {--port= : [default: 8080]} + {--save-preview= : Should the served page be saved to disk? (Overrides config setting)} {--dashboard= : Enable the realtime compiler dashboard. (Overrides config setting)} {--pretty-urls= : Enable pretty URLs. (Overrides config setting)} {--play-cdn= : Enable the Tailwind Play CDN. (Overrides config setting)} @@ -76,6 +77,7 @@ protected function getEnvironmentVariables(): array { return Arr::whereNotNull([ 'HYDE_SERVER_REQUEST_OUTPUT' => ! $this->option('no-ansi'), + 'HYDE_SERVER_SAVE_PREVIEW' => $this->parseEnvironmentOption('save-preview'), 'HYDE_SERVER_DASHBOARD' => $this->parseEnvironmentOption('dashboard'), 'HYDE_PRETTY_URLS' => $this->parseEnvironmentOption('pretty-urls'), 'HYDE_PLAY_CDN' => $this->parseEnvironmentOption('play-cdn'), diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php index 5fa364ee040..10bf1418286 100644 --- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php +++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php @@ -87,6 +87,7 @@ private function loadRuntimeConfiguration(Application $app, Repository $reposito $this->mergeCommandLineArguments($repository, '--no-api', 'hyde.api_calls', false); } + $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_SAVE_PREVIEW', 'hyde.server.save_preview'); $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_DASHBOARD', 'hyde.server.dashboard.enabled'); $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PRETTY_URLS', 'hyde.pretty_urls'); $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PLAY_CDN', 'hyde.use_play_cdn'); diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 605d24ecc81..935d9793198 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -80,6 +80,24 @@ public function test_getEnvironmentVariables_withNoAnsiOption() ], $this->getMock(['no-ansi' => true])->getEnvironmentVariables()); } + public function testSavePreviewOptionPropagatesToEnvironmentVariables() + { + $command = $this->getMock(['save-preview' => 'false']); + $this->assertSame('disabled', $command->getEnvironmentVariables()['HYDE_SERVER_SAVE_PREVIEW']); + + $command = $this->getMock(['save-preview' => 'true']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_SERVER_SAVE_PREVIEW']); + + $command = $this->getMock(['save-preview' => '']); + $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_SERVER_SAVE_PREVIEW']); + + $command = $this->getMock(['save-preview' => null]); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_SERVER_SAVE_PREVIEW'])); + + $command = $this->getMock(); + $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_SERVER_SAVE_PREVIEW'])); + } + public function testDashboardOptionPropagatesToEnvironmentVariables() { $command = $this->getMock(['dashboard' => 'false']); From 586cf188a1a35343dbd2659eed4d6e6e95929cfb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 10:28:33 +0100 Subject: [PATCH 38/52] Create ConsoleOutputTest.php --- .../tests/ConsoleOutputTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 packages/realtime-compiler/tests/ConsoleOutputTest.php diff --git a/packages/realtime-compiler/tests/ConsoleOutputTest.php b/packages/realtime-compiler/tests/ConsoleOutputTest.php new file mode 100644 index 00000000000..b554e1e56cf --- /dev/null +++ b/packages/realtime-compiler/tests/ConsoleOutputTest.php @@ -0,0 +1,16 @@ +output = new BufferedOutput()); +}); + +afterEach(function () { + renderUsing(null); + + Styles::flush(); +}); From de97d92367a549ddb4ba008ef7fe1c2d4427618e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 10:42:03 +0100 Subject: [PATCH 39/52] Test start message method --- .../tests/ConsoleOutputTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/realtime-compiler/tests/ConsoleOutputTest.php b/packages/realtime-compiler/tests/ConsoleOutputTest.php index b554e1e56cf..5a30fe55bf2 100644 --- a/packages/realtime-compiler/tests/ConsoleOutputTest.php +++ b/packages/realtime-compiler/tests/ConsoleOutputTest.php @@ -14,3 +14,22 @@ Styles::flush(); }); + +test('printStartMessage method', function () { + // Todo handle version dynamically + + $output = new \Hyde\RealtimeCompiler\ConsoleOutput(); + $output->printStartMessage('localhost', 8000); + $this->assertSame(<<<'TXT' + + ╭────────────────────────────────────╮ + │ │ + │ HydePHP Realtime Compiler v1.3.3 │ + │ │ + │ Listening on http://localhost:8000 │ + │ │ + ╰────────────────────────────────────╯ + + + TXT, str_replace(["\u{A0}", "\r"], [' ', ''], $this->output->fetch())); +}); From 3b8f0939710d8ef0f482db4c11f209e26dbe75b9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 10:42:28 +0100 Subject: [PATCH 40/52] Move up line break Does not change rendered output --- packages/realtime-compiler/src/ConsoleOutput.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 70c420c4c81..3a25e785d44 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -45,8 +45,7 @@ public function printStartMessage(string $host, int $port): void  │{$spacing}│
 │{$line2}│
 │{$spacing}│
- ╰{$lines}╯ -
+ ╰{$lines}╯
HTML); } From 401c78bd514ab5d7138ebefc90b6c33ec46fc6dc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 11:18:20 +0100 Subject: [PATCH 41/52] Refactor to handle start message dynamically --- .../realtime-compiler/src/ConsoleOutput.php | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 3a25e785d44..8742e920995 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -26,28 +26,32 @@ public function __construct(bool $verbose = false, ?SymfonyOutput $output = null public function printStartMessage(string $host, int $port): void { $title = 'HydePHP Realtime Compiler'; - $version = ' v'.Hyde::version(); + $version = 'v'.Hyde::version(); $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); - $width = max(strlen("$title $version"), strlen("Listening on $url") + 1) + 1; - $spacing = str_repeat(' ', $width); - $lines = str_repeat('─', $width); - - $line1 = ' '.sprintf('%s %s', $title, $version).str_repeat(' ', $width - strlen("$title $version")); - $line2 = ' '.sprintf('Listening on  %s', $url, $url).str_repeat(' ', $width - strlen("Listening on $url") - 1); - render(<< -
- ╭{$lines}╮
- │{$spacing}│
- │{$line1}│
- │{$spacing}│
- │{$line2}│
- │{$spacing}│
- ╰{$lines}╯
- -HTML); + $lines = [ + sprintf('%s %s', $title, $version), + '', + sprintf('Listening on %s', $url, $url), + ]; + + $lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); + + $lines = array_map(function (string $line) use ($lineLength): string { + return sprintf(' │ %s%s│', + $line, str_repeat(' ', ($lineLength - strlen(strip_tags($line))) + 1) + ); + }, array_merge([''], $lines, [''])); + + $topLine = sprintf(' ╭%s╮', str_repeat('─', $lineLength + 2)); + $bottomLine = sprintf(' ╰%s╯', str_repeat('─', $lineLength + 2)); + + $lines = array_merge([$topLine], $lines, [$bottomLine]); + + $body = implode('
', array_merge([''], $lines, [''])); + + render("
$body
"); } public function getFormatter(): Closure From b9405c158bc2f9acf7e90492e508c64299897077 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 12:23:06 +0100 Subject: [PATCH 42/52] Merge array merges --- packages/realtime-compiler/src/ConsoleOutput.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 8742e920995..9d701ba3d79 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -47,9 +47,7 @@ public function printStartMessage(string $host, int $port): void $topLine = sprintf(' ╭%s╮', str_repeat('─', $lineLength + 2)); $bottomLine = sprintf(' ╰%s╯', str_repeat('─', $lineLength + 2)); - $lines = array_merge([$topLine], $lines, [$bottomLine]); - - $body = implode('
', array_merge([''], $lines, [''])); + $body = implode('
', array_merge([''], [$topLine], $lines, [$bottomLine], [''])); render("
$body
"); } From 9b432691eb26a7691dfc8a087c67618e54b02789 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 12:24:16 +0100 Subject: [PATCH 43/52] Inline local variables --- packages/realtime-compiler/src/ConsoleOutput.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 9d701ba3d79..ba700699024 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -25,13 +25,10 @@ public function __construct(bool $verbose = false, ?SymfonyOutput $output = null public function printStartMessage(string $host, int $port): void { - $title = 'HydePHP Realtime Compiler'; - $version = 'v'.Hyde::version(); - $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); $lines = [ - sprintf('%s %s', $title, $version), + sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::version()), '', sprintf('Listening on %s', $url, $url), ]; From e666159924b359f5129fd65398371b2e1599d495 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 12:32:06 +0100 Subject: [PATCH 44/52] Get version from instance so it can be mocked Just saves on testing logic as we can fix the version --- packages/realtime-compiler/src/ConsoleOutput.php | 2 +- .../realtime-compiler/tests/ConsoleOutputTest.php | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index ba700699024..9f089c9a469 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -28,7 +28,7 @@ public function printStartMessage(string $host, int $port): void $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); $lines = [ - sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::version()), + sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), '', sprintf('Listening on %s', $url, $url), ]; diff --git a/packages/realtime-compiler/tests/ConsoleOutputTest.php b/packages/realtime-compiler/tests/ConsoleOutputTest.php index 5a30fe55bf2..bc201767aef 100644 --- a/packages/realtime-compiler/tests/ConsoleOutputTest.php +++ b/packages/realtime-compiler/tests/ConsoleOutputTest.php @@ -1,5 +1,6 @@ printStartMessage('localhost', 8000); @@ -24,7 +33,7 @@ ╭────────────────────────────────────╮ │ │ - │ HydePHP Realtime Compiler v1.3.3 │ + │ HydePHP Realtime Compiler v1.2.3 │ │ │ │ Listening on http://localhost:8000 │ │ │ From b2a27295374346c20f5382c3fb8dee9930d7e703 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 12:43:15 +0100 Subject: [PATCH 45/52] Place merged items in initial array --- packages/realtime-compiler/src/ConsoleOutput.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 9f089c9a469..2260130948e 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -28,9 +28,11 @@ public function printStartMessage(string $host, int $port): void $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); $lines = [ + '', sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), '', sprintf('Listening on %s', $url, $url), + '', ]; $lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); @@ -39,7 +41,7 @@ public function printStartMessage(string $host, int $port): void return sprintf(' │ %s%s│', $line, str_repeat(' ', ($lineLength - strlen(strip_tags($line))) + 1) ); - }, array_merge([''], $lines, [''])); + }, $lines); $topLine = sprintf(' ╭%s╮', str_repeat('─', $lineLength + 2)); $bottomLine = sprintf(' ╰%s╯', str_repeat('─', $lineLength + 2)); From 2c2e710d47b4acd9e17dbb62a48c8dd4f2277496 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 12:56:19 +0100 Subject: [PATCH 46/52] Create initial display of runtime options in start message --- .../src/Console/Commands/ServeCommand.php | 2 +- packages/realtime-compiler/src/ConsoleOutput.php | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index adcea1806cd..804151fdc0c 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -95,7 +95,7 @@ protected function printStartMessage(): void { $this->useBasicOutput() ? $this->output->writeln('Starting the HydeRC server... Press Ctrl+C to stop') - : $this->console->printStartMessage($this->getHostSelection(), $this->getPortSelection()); + : $this->console->printStartMessage($this->getHostSelection(), $this->getPortSelection(), $this->getEnvironmentVariables()); } protected function getOutputHandler(): Closure diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 2260130948e..01e6dd60e24 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -7,6 +7,7 @@ use Closure; use Hyde\Hyde; use Illuminate\Support\Str; +use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Symfony\Component\Console\Output\ConsoleOutput as SymfonyOutput; @@ -23,16 +24,29 @@ public function __construct(bool $verbose = false, ?SymfonyOutput $output = null $this->output = $output ?? new SymfonyOutput(); } - public function printStartMessage(string $host, int $port): void + public function printStartMessage(string $host, int $port, array $environment): void { $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); + $statusOptions = [ + 'enabled' => 'green-500', + 'disabled' => 'red-500', + 'overridden' => 'yellow-500', + ]; + + $dashboardStatusValue = config('hyde.server.dashboard.enabled'); + $dashboardOverridden = Arr::has($environment, 'HYDE_SERVER_DASHBOARD'); + $dashboardStatus = $dashboardOverridden ? 'overridden' : ($dashboardStatusValue ? 'enabled' : 'disabled'); + $dashboardStatusMessage = sprintf('Dashboard: %s', $statusOptions[$dashboardStatus], $dashboardStatusValue ? 'enabled' : 'disabled'); + $lines = [ '', sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), '', sprintf('Listening on %s', $url, $url), '', + $dashboardStatusMessage, + '', ]; $lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); From b1b155eaf62ea4f9c09ab7d5d5b0bb585ad4edff Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 13:01:20 +0100 Subject: [PATCH 47/52] Only show statuses when changed --- .../realtime-compiler/src/ConsoleOutput.php | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 01e6dd60e24..ecc0d7c6b0d 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -24,31 +24,41 @@ public function __construct(bool $verbose = false, ?SymfonyOutput $output = null $this->output = $output ?? new SymfonyOutput(); } - public function printStartMessage(string $host, int $port, array $environment): void + public function printStartMessage(string $host, int $port, array $environment = []): void { $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); - $statusOptions = [ - 'enabled' => 'green-500', - 'disabled' => 'red-500', - 'overridden' => 'yellow-500', - ]; - - $dashboardStatusValue = config('hyde.server.dashboard.enabled'); - $dashboardOverridden = Arr::has($environment, 'HYDE_SERVER_DASHBOARD'); - $dashboardStatus = $dashboardOverridden ? 'overridden' : ($dashboardStatusValue ? 'enabled' : 'disabled'); - $dashboardStatusMessage = sprintf('Dashboard: %s', $statusOptions[$dashboardStatus], $dashboardStatusValue ? 'enabled' : 'disabled'); - $lines = [ '', sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), '', sprintf('Listening on %s', $url, $url), '', - $dashboardStatusMessage, - '', ]; + if ($environment !== []) { + $statusOptions = [ + 'enabled' => 'green-500', + 'disabled' => 'red-500', + ]; + + if (Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) { + $dashboardStatus = Arr::get($environment, 'HYDE_SERVER_DASHBOARD'); + $dashboardStatusValue = $dashboardStatus === 'enabled'; + $dashboardStatusMessage = sprintf('Dashboard: %s', $statusOptions[$dashboardStatus], $dashboardStatusValue ? 'enabled' : 'disabled'); + } + + $optionLines = Arr::whereNotNull([ + $dashboardStatusMessage ?? null, + ]); + + if ($optionLines !== []) { + $optionLines[] = ''; + } + + $lines = array_merge($lines, $optionLines); + } + $lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); $lines = array_map(function (string $line) use ($lineLength): string { From 7b6b30ac6392d8166a92770865c215cb354222ad Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 13:03:28 +0100 Subject: [PATCH 48/52] Revert "Only show statuses when changed" This reverts commit b1b155eaf62ea4f9c09ab7d5d5b0bb585ad4edff. --- .../realtime-compiler/src/ConsoleOutput.php | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index ecc0d7c6b0d..01e6dd60e24 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -24,41 +24,31 @@ public function __construct(bool $verbose = false, ?SymfonyOutput $output = null $this->output = $output ?? new SymfonyOutput(); } - public function printStartMessage(string $host, int $port, array $environment = []): void + public function printStartMessage(string $host, int $port, array $environment): void { $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); + $statusOptions = [ + 'enabled' => 'green-500', + 'disabled' => 'red-500', + 'overridden' => 'yellow-500', + ]; + + $dashboardStatusValue = config('hyde.server.dashboard.enabled'); + $dashboardOverridden = Arr::has($environment, 'HYDE_SERVER_DASHBOARD'); + $dashboardStatus = $dashboardOverridden ? 'overridden' : ($dashboardStatusValue ? 'enabled' : 'disabled'); + $dashboardStatusMessage = sprintf('Dashboard: %s', $statusOptions[$dashboardStatus], $dashboardStatusValue ? 'enabled' : 'disabled'); + $lines = [ '', sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), '', sprintf('Listening on %s', $url, $url), '', + $dashboardStatusMessage, + '', ]; - if ($environment !== []) { - $statusOptions = [ - 'enabled' => 'green-500', - 'disabled' => 'red-500', - ]; - - if (Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) { - $dashboardStatus = Arr::get($environment, 'HYDE_SERVER_DASHBOARD'); - $dashboardStatusValue = $dashboardStatus === 'enabled'; - $dashboardStatusMessage = sprintf('Dashboard: %s', $statusOptions[$dashboardStatus], $dashboardStatusValue ? 'enabled' : 'disabled'); - } - - $optionLines = Arr::whereNotNull([ - $dashboardStatusMessage ?? null, - ]); - - if ($optionLines !== []) { - $optionLines[] = ''; - } - - $lines = array_merge($lines, $optionLines); - } - $lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); $lines = array_map(function (string $line) use ($lineLength): string { From 938805e9023772c667147e9242368011262e978c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 13:11:23 +0100 Subject: [PATCH 49/52] Add dashboard link to start message --- packages/realtime-compiler/src/ConsoleOutput.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 01e6dd60e24..c056763efe7 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -39,15 +39,16 @@ public function printStartMessage(string $host, int $port, array $environment): $dashboardStatus = $dashboardOverridden ? 'overridden' : ($dashboardStatusValue ? 'enabled' : 'disabled'); $dashboardStatusMessage = sprintf('Dashboard: %s', $statusOptions[$dashboardStatus], $dashboardStatusValue ? 'enabled' : 'disabled'); - $lines = [ + $isDashboardEnabled = (config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled'; + $lines = Arr::whereNotNull([ '', sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), '', - sprintf('Listening on %s', $url, $url), + sprintf('Listening on: %s', $url, $url), + $isDashboardEnabled ? + sprintf('Live dashboard: %s/dashboard', $url, $url) : null, '', - $dashboardStatusMessage, - '', - ]; + ]); $lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); From 36dc855d7aae5a4534d5bd818e24ff2526745f27 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 13:15:15 +0100 Subject: [PATCH 50/52] Remove option status printing This just feels bloaty at the moment, plus the user should be able to assume that the flag they provided is applied. --- packages/realtime-compiler/src/ConsoleOutput.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index c056763efe7..7b1f768b209 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -28,17 +28,6 @@ public function printStartMessage(string $host, int $port, array $environment): { $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); - $statusOptions = [ - 'enabled' => 'green-500', - 'disabled' => 'red-500', - 'overridden' => 'yellow-500', - ]; - - $dashboardStatusValue = config('hyde.server.dashboard.enabled'); - $dashboardOverridden = Arr::has($environment, 'HYDE_SERVER_DASHBOARD'); - $dashboardStatus = $dashboardOverridden ? 'overridden' : ($dashboardStatusValue ? 'enabled' : 'disabled'); - $dashboardStatusMessage = sprintf('Dashboard: %s', $statusOptions[$dashboardStatus], $dashboardStatusValue ? 'enabled' : 'disabled'); - $isDashboardEnabled = (config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled'; $lines = Arr::whereNotNull([ '', From 1f5cb5149fe5e517cd385957de8024448bb99871 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 13:16:01 +0100 Subject: [PATCH 51/52] Cleanup code --- packages/realtime-compiler/src/ConsoleOutput.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 7b1f768b209..8e33beebca1 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -28,16 +28,15 @@ public function printStartMessage(string $host, int $port, array $environment): { $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); - $isDashboardEnabled = (config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled'; - $lines = Arr::whereNotNull([ + $lines = [ '', sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), '', sprintf('Listening on: %s', $url, $url), - $isDashboardEnabled ? + (config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled' ? sprintf('Live dashboard: %s/dashboard', $url, $url) : null, '', - ]); + ]; $lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); From 5e6501c492150e4314e8156c0d7a3ae9d6f36e9a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 12 Nov 2023 13:20:13 +0100 Subject: [PATCH 52/52] Cleanup code --- .../realtime-compiler/src/ConsoleOutput.php | 6 +++--- .../tests/ConsoleOutputTest.php | 18 +++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index 8e33beebca1..799aa5ae7aa 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -24,11 +24,11 @@ public function __construct(bool $verbose = false, ?SymfonyOutput $output = null $this->output = $output ?? new SymfonyOutput(); } - public function printStartMessage(string $host, int $port, array $environment): void + public function printStartMessage(string $host, int $port, array $environment = []): void { $url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); - $lines = [ + $lines = Arr::whereNotNull([ '', sprintf('%s %s', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), '', @@ -36,7 +36,7 @@ public function printStartMessage(string $host, int $port, array $environment): (config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled' ? sprintf('Live dashboard: %s/dashboard', $url, $url) : null, '', - ]; + ]); $lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); diff --git a/packages/realtime-compiler/tests/ConsoleOutputTest.php b/packages/realtime-compiler/tests/ConsoleOutputTest.php index bc201767aef..16c1e8a274c 100644 --- a/packages/realtime-compiler/tests/ConsoleOutputTest.php +++ b/packages/realtime-compiler/tests/ConsoleOutputTest.php @@ -6,8 +6,12 @@ use function Termwind\{renderUsing}; +uses(\Hyde\Testing\UnitTestCase::class); + beforeEach(function () { renderUsing($this->output = new BufferedOutput()); + + $this::mockConfig(); }); afterEach(function () { @@ -31,13 +35,13 @@ public static function version(): string $output->printStartMessage('localhost', 8000); $this->assertSame(<<<'TXT' - ╭────────────────────────────────────╮ - │ │ - │ HydePHP Realtime Compiler v1.2.3 │ - │ │ - │ Listening on http://localhost:8000 │ - │ │ - ╰────────────────────────────────────╯ + ╭─────────────────────────────────────╮ + │ │ + │ HydePHP Realtime Compiler v1.2.3 │ + │ │ + │ Listening on: http://localhost:8000 │ + │ │ + ╰─────────────────────────────────────╯ TXT, str_replace(["\u{A0}", "\r"], [' ', ''], $this->output->fetch()));