From c4547394ac911625006b7720f588bbd0feb3c5ec Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 9 Dec 2023 21:44:34 +0100 Subject: [PATCH 1/3] Access environment variable accessors --- src/main/php/websocket/Environment.class.php | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/php/websocket/Environment.class.php b/src/main/php/websocket/Environment.class.php index 432d7d4..46fafa7 100755 --- a/src/main/php/websocket/Environment.class.php +++ b/src/main/php/websocket/Environment.class.php @@ -71,4 +71,31 @@ public function arguments() { return $this->arguments; } /** @return web.Logging */ public function logging() { return $this->logging; } + + /** + * Returns a given environment variable + * + * @param string $name + * @return ?string + */ + public function variable($name) { + return false === ($env= getenv($name)) ? null : $env; + } + + /** + * Pass a given environment variable and value. Pass NULL in value to + * remove this environment variable. + * + * @param string $name + * @param ?string $value + * @return self + */ + public function export($name, $value) { + if (null === $value) { + putenv($name); + } else { + putenv($name.'='.$value); + } + return $this; + } } \ No newline at end of file From 94203c0976bcaea797b5b72ab1e6186eab622085 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 9 Dec 2023 21:48:13 +0100 Subject: [PATCH 2/3] Fix import --- src/test/php/websocket/unittest/ProtocolTest.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/php/websocket/unittest/ProtocolTest.class.php b/src/test/php/websocket/unittest/ProtocolTest.class.php index 520ba6c..8abbb6d 100755 --- a/src/test/php/websocket/unittest/ProtocolTest.class.php +++ b/src/test/php/websocket/unittest/ProtocolTest.class.php @@ -2,7 +2,7 @@ use test\{Assert, Before, Test}; use websocket\{Dispatch, Environment, Listeners, Logging}; -use xp\ws\{Events, Protocol}; +use xp\websockets\{Events, Protocol}; class ProtocolTest { const ID= 42; From aae7223a6579ee6d72a26931e766a23b897a718d Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 9 Dec 2023 21:49:46 +0100 Subject: [PATCH 3/3] Add tests for environment variables --- .../unittest/EnvironmentTest.class.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/php/websocket/unittest/EnvironmentTest.class.php b/src/test/php/websocket/unittest/EnvironmentTest.class.php index 745f9c2..e414b4f 100755 --- a/src/test/php/websocket/unittest/EnvironmentTest.class.php +++ b/src/test/php/websocket/unittest/EnvironmentTest.class.php @@ -59,4 +59,21 @@ public function properties_from_directory() { public function arguments($arguments) { Assert::equals($arguments, (new Environment('dev', [], $arguments))->arguments()); } + + #[Test] + public function env_variable() { + putenv('TEST=true'); + Assert::equals('true', (new Environment('dev'))->variable('TEST')); + } + + #[Test] + public function unset_variable() { + putenv('TEST'); + Assert::null((new Environment('dev'))->variable('TEST')); + } + + #[Test] + public function export_variable() { + Assert::equals('true', (new Environment('dev'))->export('TEST', 'true')->variable('TEST')); + } } \ No newline at end of file