Env.php
128 lines
<?php
<?php
namespace Illuminate\Support;
namespace Illuminate\Support;
use Dotenv\Environment\Adapter\EnvConstAdapter;
use Dotenv\Repository\Adapter\EnvConstAdapter;
use Dotenv\Environment\Adapter\PutenvAdapter;
use Dotenv\Repository\Adapter\PutenvAdapter;
use Dotenv\Environment\Adapter\ServerConstAdapter;
use Dotenv\Repository\Adapter\ServerConstAdapter;
use Dotenv\Environment\DotenvFactory;
use Dotenv\Repository\RepositoryBuilder;
use PhpOption\Option;
use PhpOption\Option;
class Env
class Env
{
{
/**
/**
* Indicates if the putenv adapter is enabled.
* Indicates if the putenv adapter is enabled.
*
*
* @var bool
* @var bool
*/
*/
protected static $putenv = true;
protected static $putenv = true;
/**
/**
* The environment factory instance.
* The environment repository instance.
*
* @var \Dotenv\Environment\FactoryInterface|null
*/
protected static $factory;
/**
* The environment variables instance.
*
*
* @var \Dotenv\Environment\VariablesInterface|null
* @var \Dotenv\Repository\RepositoryInterface|null
*/
*/
protected static $variables;
protected static $repository;
/**
/**
* Enable the putenv adapter.
* Enable the putenv adapter.
*
*
* @return void
* @return void
*/
*/
public static function enablePutenv()
public static function enablePutenv()
{
{
static::$putenv = true;
static::$putenv = true;
static::$factory = null;
static::$repository = null;
static::$variables = null;
}
}
/**
/**
* Disable the putenv adapter.
* Disable the putenv adapter.
*
*
* @return void
* @return void
*/
*/
public static function disablePutenv()
public static function disablePutenv()
{
{
static::$putenv = false;
static::$putenv = false;
static::$factory = null;
static::$repository = null;
static::$variables = null;
}
}
/**
/**
* Get the environment factory instance.
* Get the environment repository instance.
*
*
* @return \Dotenv\Environment\FactoryInterface
* @return \Dotenv\Repository\RepositoryInterface
*/
*/
public static function getFactory()
public static function getRepository()
{
{
if (static::$factory === null) {
if (static::$repository === null) {
$adapters = array_merge(
$adapters = array_merge(
[new EnvConstAdapter, new ServerConstAdapter],
[new EnvConstAdapter, new ServerConstAdapter],
static::$putenv ? [new PutenvAdapter] : []
static::$putenv ? [new PutenvAdapter] : []
);
);
static::$factory = new DotenvFactory($adapters);
static::$repository = RepositoryBuilder::create()
}
->withReaders($adapters)
->withWriters($adapters)
return static::$factory;
->immutable()
}
->make();
/**
* Get the environment variables instance.
*
* @return \Dotenv\Environment\VariablesInterface
*/
public static function getVariables()
{
if (static::$variables === null) {
static::$variables = static::getFactory()->createImmutable();
}
}
return static::$variables;
return static::$repository;
}
}
/**
/**
* Gets the value of an environment variable.
* Gets the value of an environment variable.
*
*
* @param string $key
* @param string $key
* @param mixed $default
* @param mixed $default
* @return mixed
* @return mixed
*/
*/
public static function get($key, $default = null)
public static function get($key, $default = null)
{
{
return Option::fromValue(static::getVariables()->get($key))
return Option::fromValue(static::getRepository()->get($key))
->map(function ($value) {
->map(function ($value) {
switch (strtolower($value)) {
switch (strtolower($value)) {
case 'true':
case 'true':
case '(true)':
case '(true)':
return true;
return true;
case 'false':
case 'false':
case '(false)':
case '(false)':
return false;
return false;
case 'empty':
case 'empty':
case '(empty)':
case '(empty)':
return '';
return '';
case 'null':
case 'null':
case '(null)':
case '(null)':
return;
return;
}
}
if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
return $matches[2];
return $matches[2];
}
}
return $value;
return $value;
})
})
->getOrCall(function () use ($default) {
->getOrCall(function () use ($default) {
return value($default);
return value($default);
});
});
}
}
}
}