Skip to content

Commit

Permalink
Merge pull request #93 from xp-forge/feature/with-environment
Browse files Browse the repository at this point in the history
Allow passing or removing environment variables
  • Loading branch information
thekid authored Dec 3, 2023
2 parents 8d32176 + d907ac8 commit f526b15
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/php/web/Environment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ 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;
}

/**
* Gets properties
*
Expand Down
10 changes: 10 additions & 0 deletions src/test/php/web/unittest/EnvironmentTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public function variable() {
Assert::equals('abc', (new Environment('dev', '.', 'static', []))->variable('XP_TEST'));
}

#[Test, Values(['abc', ''])]
public function set_variable($value) {
Assert::equals($value, (new Environment('dev', '.', 'static', []))->export('test', $value)->variable('test'));
}

#[Test]
public function unset_variable() {
Assert::null((new Environment('dev', '.', 'static', []))->export('test', null)->variable('test'));
}

#[Test]
public function tempDir() {
Assert::true(is_dir((new Environment('dev', '.', 'static', []))->tempDir()));
Expand Down

0 comments on commit f526b15

Please sign in to comment.