Skip to content

Commit

Permalink
Adds Env helper class (#329)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
  • Loading branch information
huangdijia and huangdijia committed Sep 5, 2023
1 parent c9e2d26 commit d889c80
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 5 deletions.
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"email": "huangdijia@gmail.com"
}
],
"support": {
"issues": "https://github.com/friendsofhyperf/components/issues",
"source": "https://github.com/friendsofhyperf/components"
},
"require": {
"hyperf/di": "~3.1.0",
"hyperf/collection": "~3.1.0",
Expand Down Expand Up @@ -38,15 +42,12 @@
}
},
"suggest": {
"hyperf/async-queue": "Required to use Hyperf\\AsyncQueue\\Job.(~3.1.0)"
"hyperf/async-queue": "Required to use Hyperf\\AsyncQueue\\Job.(~3.1.0)",
"vlucas/phpdotenv": "Required to use Dotenv.(^5.0)"
},
"minimum-stability": "dev",
"config": {
"sort-packages": true,
"optimize-autoloader": true
},
"support": {
"issues": "https://github.com/friendsofhyperf/components/issues",
"source": "https://github.com/friendsofhyperf/components"
}
}
132 changes: 132 additions & 0 deletions src/Env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Support;

use Dotenv\Repository\Adapter\PutenvAdapter;
use Dotenv\Repository\RepositoryBuilder;
use PhpOption\Option;
use RuntimeException;

use function Hyperf\Support\value;

class Env
{
/**
* Indicates if the putenv adapter is enabled.
*
* @var bool
*/
protected static $putenv = true;

/**
* The environment repository instance.
*
* @var \Dotenv\Repository\RepositoryInterface|null
*/
protected static $repository;

/**
* Enable the putenv adapter.
*/
public static function enablePutenv()
{
static::$putenv = true;
static::$repository = null;
}

/**
* Disable the putenv adapter.
*/
public static function disablePutenv()
{
static::$putenv = false;
static::$repository = null;
}

/**
* Get the environment repository instance.
*
* @return \Dotenv\Repository\RepositoryInterface
*/
public static function getRepository()
{
if (static::$repository === null) {
$builder = RepositoryBuilder::createWithDefaultAdapters();

if (static::$putenv) {
$builder = $builder->addAdapter(PutenvAdapter::class);
}

static::$repository = $builder->immutable()->make();
}

return static::$repository;
}

/**
* Get 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));
}

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

/**
* Get the possible option for this environment variable.
*
* @param string $key
* @return \PhpOption\Option|\PhpOption\Some
*/
protected static function getOption($key)
{
return Option::fromValue(static::getRepository()->get($key))
->map(function ($value) {
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return;
}

if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
return $matches[2];
}

return $value;
});
}
}

0 comments on commit d889c80

Please sign in to comment.