Env.php
TLDR
This file, Env.php
, defines a class called Env
which provides methods for interacting with environment variables. It also has methods to enable or disable the putenv adapter and a method to retrieve the environment repository instance.
Methods
enablePutenv
This method enables the putenv adapter.
disablePutenv
This method disables the putenv adapter.
getRepository
This method returns the environment repository instance used to retrieve and manage environment variables.
get
This method retrieves the value of an environment variable. It accepts two parameters: $key
(the variable name) and $default
(optional, the value to return if the variable is not found).
getOrFail
This method retrieves the value of a required environment variable. It accepts one parameter: $key
(the variable name). If the variable is not found, it throws a RuntimeException
with a message indicating that the variable has no value.
getOption
This protected method retrieves the possible option for the specified environment variable. It accepts one parameter: $key
(the variable name). It returns an Option
object from the PhpOption
library, which represents the value of the variable or an empty value. The value is processed and transformed according to certain conditions (e.g., converting strings "true"
and "false"
to boolean values, removing quotes from quoted strings, etc.).
Classes
There are no additional classes in this file.
<?php
namespace Illuminate\Support;
use Dotenv\Repository\Adapter\PutenvAdapter;
use Dotenv\Repository\RepositoryBuilder;
use PhpOption\Option;
use RuntimeException;
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.
*
* @return void
*/
public static function enablePutenv()
{
static::$putenv = true;
static::$repository = null;
}
/**
* Disable the putenv adapter.
*
* @return void
*/
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;
});
}
}