Skip to content

Commit

Permalink
Introduce requireEnv helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmichot committed Aug 31, 2023
1 parent 4989e6d commit 7b7dd6b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/Illuminate/Support/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dotenv\Repository\Adapter\PutenvAdapter;
use Dotenv\Repository\RepositoryBuilder;
use PhpOption\Option;
use RuntimeException;

class Env
{
Expand Down Expand Up @@ -65,13 +66,12 @@ public static function getRepository()
}

/**
* Gets the value of an environment variable.
* Get the possible option for this environment variable.
*
* @param string $key
* @param mixed $default
* @return mixed
* @return \PhpOption\Option|\PhpOption\Some
*/
public static function get($key, $default = null)
protected static function getOption($key)
{
return Option::fromValue(static::getRepository()->get($key))
->map(function ($value) {
Expand All @@ -95,7 +95,31 @@ public static function get($key, $default = null)
}

return $value;
})
->getOrCall(fn () => value($default));
});
}

/**
* Gets the value of a required environment variable.
*
* @param string $key
* @return mixed
*
* @throws \RuntimeException
*/
public static function getRequired($key)
{
return self::getOption($key)->getOrThrow(new RuntimeException("[$key] has no value"));
}

/**
* Gets the value of an environment variable.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($key, $default = null)
{
return self::getOption($key)->getOrCall(fn () => value($default));
}
}
15 changes: 15 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ function env($key, $default = null)
}
}

if (! function_exists('requireEnv')) {
/**
* Gets the value of a required environment variable.
*
* @param string $key
* @return mixed
*
* @throws \RuntimeException
*/
function requireEnv($key)
{
return Env::getRequired($key);
}
}

if (! function_exists('filled')) {
/**
* Determine if a value is "filled".
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class SupportHelpersTest extends TestCase
protected function tearDown(): void
{
m::close();

parent::tearDown();
}

public function testE()
Expand Down Expand Up @@ -1011,6 +1013,19 @@ public function testGetFromSERVERFirst()
$this->assertSame('From $_SERVER', env('foo'));
}

public function testRequiredEnvVariableThrowsAnExceptionWhenNotFound(): void
{
$this->expectExceptionObject(new RuntimeException('[required-does-not-exist] has no value'));

requireEnv('required-does-not-exist');
}

public function testRequiredEnvReturnsValue(): void
{
$_SERVER['required-exists'] = 'some-value';
$this->assertSame('some-value', requireEnv('required-exists'));
}

public static function providesPregReplaceArrayData()
{
$pointerArray = ['Taylor', 'Otwell'];
Expand Down

0 comments on commit 7b7dd6b

Please sign in to comment.