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 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