Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access environment variable accessors #3

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/php/websocket/Environment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
17 changes: 17 additions & 0 deletions src/test/php/websocket/unittest/EnvironmentTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
2 changes: 1 addition & 1 deletion src/test/php/websocket/unittest/ProtocolTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading