Skip to content

Commit

Permalink
Merge pull request #3 from xp-forge/feature/env-variables
Browse files Browse the repository at this point in the history
Access environment variable accessors
  • Loading branch information
thekid authored Dec 9, 2023
2 parents d857d83 + aae7223 commit 6cc6662
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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'));
}
}

0 comments on commit 6cc6662

Please sign in to comment.