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

Add env var support #3

Merged
merged 1 commit into from
Jun 26, 2024
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
17 changes: 17 additions & 0 deletions src/SuperGlobals/SuperGlobals.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ public static function get_post_var( string $var, $default = null ) {
return static::sanitize_deep( $unsafe );
}

/**
* Gets a value from `$_ENV`.
*
* @since 1.3.0
*
* @see Arr::get()
*
* @param string $var
* @param mixed $default
*
* @return mixed
*/
public static function get_env_var( string $var, $default = null ) {
$unsafe = Arr::get( (array) $_ENV, $var, $default );
return static::sanitize_deep( $unsafe );
}

/**
* Gets the requested superglobal variable.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/SuperGlobalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use StellarWP\SuperGlobals\SuperGlobals;

final class SuperGlobalsTest extends TestCase {

/**
* @test
*/
Expand Down Expand Up @@ -69,6 +70,19 @@ public function it_should_get_var_from_post() {
unset( $_POST['bork'] );
}

/**
* @test
*/
public function it_should_get_env_var() {
$_ENV['bork'] = 'my env var';

$this->assertEquals( $_ENV['bork'], SuperGlobals::get_env_var( 'bork', 'default' ) );
$this->assertNotEquals( 'whee', SuperGlobals::get_env_var( 'bork', 'default' ) );
$this->assertEquals( 'default', SuperGlobals::get_env_var( 'hello', 'default' ) );

unset( $_ENV['bork'] );
}

/**
* @test
*/
Expand Down
Loading