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

[5.8] Reworked env to allow getenv/putenv to be disabled #28740

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace Illuminate\Foundation\Bootstrap;

use Dotenv\Dotenv;
use Dotenv\Environment\DotenvFactory;
use Illuminate\Support\Env;
use Dotenv\Exception\InvalidFileException;
use Dotenv\Environment\Adapter\PutenvAdapter;
use Symfony\Component\Console\Input\ArgvInput;
use Dotenv\Environment\Adapter\EnvConstAdapter;
use Illuminate\Contracts\Foundation\Application;
use Dotenv\Environment\Adapter\ServerConstAdapter;
use Symfony\Component\Console\Output\ConsoleOutput;

class LoadEnvironmentVariables
Expand Down Expand Up @@ -89,7 +86,7 @@ protected function createDotenv($app)
return Dotenv::create(
$app->environmentPath(),
$app->environmentFile(),
new DotenvFactory([new EnvConstAdapter, new ServerConstAdapter, new PutenvAdapter])
Env::getFactory()
);
}

Expand Down
127 changes: 127 additions & 0 deletions src/Illuminate/Support/Env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Illuminate\Support;

use PhpOption\Option;
use Dotenv\Environment\DotenvFactory;
use Dotenv\Environment\Adapter\PutenvAdapter;
use Dotenv\Environment\Adapter\EnvConstAdapter;
use Dotenv\Environment\Adapter\ServerConstAdapter;

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

/**
* The environment factory instance.
*
* @var \Dotenv\Environment\FactoryInterface|null
*/
protected static $factory;

/**
* The environment variables instance.
*
* @var \Dotenv\Environment\VariablesInterface|null
*/
protected static $variables;

/**
* Enable the putenv adapter.
*
* @var bool
*/
public static function enablePutenv()
{
static::$putenv = true;
static::$factory = null;
static::$variables = null;
}

/**
* Disable the putenv adapter.
*
* @var bool
*/
public static function disablePutenv()
{
static::$putenv = false;
static::$factory = null;
static::$variables = null;
}

/**
* Get the environment factory instance.
*
* @return \Dotenv\Environment\FactoryInterface
*/
public static function getFactory()
{
if (static::$factory === null) {
$adapters = array_merge(
[new EnvConstAdapter, new ServerConstAdapter],
static::$putenv ? [new PutenvAdapter] : []
);

static::$factory = new DotenvFactory($adapters);
}

return static::$factory;
}

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

/**
* Gets the value of an environment variable.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($key, $default = null)
{
return Option::fromValue(static::getVariables()->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;
})
->getOrCall(function () use ($default) {
return value($default);
});
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Support/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"ramsey/uuid": "Required to use Str::uuid() (^3.7).",
"symfony/process": "Required to use the composer class (^4.2).",
"symfony/var-dumper": "Required to use the dd function (^4.2).",
"vlucas/phpdotenv": "Required to use the env helper (^3.3)."
"vlucas/phpdotenv": "Required to use the Env class and env helper (^3.3)."
},
"config": {
"sort-packages": true
Expand Down
39 changes: 2 additions & 37 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php

use PhpOption\Option;
use Illuminate\Support\Arr;
use Illuminate\Support\Env;
use Illuminate\Support\Str;
use Illuminate\Support\Optional;
use Illuminate\Support\Collection;
use Dotenv\Environment\DotenvFactory;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\HigherOrderTapProxy;
use Dotenv\Environment\Adapter\PutenvAdapter;
use Dotenv\Environment\Adapter\EnvConstAdapter;
use Dotenv\Environment\Adapter\ServerConstAdapter;

if (! function_exists('append_config')) {
/**
Expand Down Expand Up @@ -640,38 +636,7 @@ function ends_with($haystack, $needles)
*/
function env($key, $default = null)
{
static $variables;

if ($variables === null) {
$variables = (new DotenvFactory([new EnvConstAdapter, new PutenvAdapter, new ServerConstAdapter]))->createImmutable();
}

return Option::fromValue($variables->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;
})
->getOrCall(function () use ($default) {
return value($default);
});
return Env::get($key, $default);
}
}

Expand Down