Skip to content

Instantly share code, notes, and snippets.

@progmars
Last active May 19, 2016 13:03
Show Gist options
  • Save progmars/db5b8e2331e8723dd637 to your computer and use it in GitHub Desktop.
Save progmars/db5b8e2331e8723dd637 to your computer and use it in GitHub Desktop.
Fix for Dotenv 1.1.0 and Laravel 5.0.27 issues with concurrent requests failing because of PHP threaded getenv/putenv implementation.
Fix for Dotenv and Laravel issues with concurrent requests failing because of PHP threaded getenv/putenv implementation.
diff --git a/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php b/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
index de5939e..715673d 100644
--- a/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
+++ b/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
@@ -603,9 +603,14 @@ if ( ! function_exists('env'))
*/
function env($key, $default = null)
{
- $value = getenv($key);
-
- if ($value === false) return value($default);
+ // workaround for
+ // https://github.com/laravel/framework/pull/8187
+ // and
+ // https://github.com/vlucas/phpdotenv/issues/76
+ $value = Dotenv::findEnvironmentVariable($key);
+
+ // false to null for findEnvironmentVariable() result check
+ if ($value === null) return value($default);
switch (strtolower($value))
{
diff --git a/vendor/vlucas/phpdotenv/src/Dotenv.php b/vendor/vlucas/phpdotenv/src/Dotenv.php
index f91c848..4f377c4 100644
--- a/vendor/vlucas/phpdotenv/src/Dotenv.php
+++ b/vendor/vlucas/phpdotenv/src/Dotenv.php
@@ -11,7 +11,8 @@ class Dotenv
* @var bool
*/
protected static $immutable = true;
-
+ protected static $cache = [];
+
/**
* Load `.env` file in given directory
*/
@@ -66,12 +67,30 @@ class Dotenv
{
list($name, $value) = static::normaliseEnvironmentVariable($name, $value);
+ // workaround for
+ // https://github.com/laravel/framework/pull/8187
+ // and
+ // https://github.com/vlucas/phpdotenv/issues/76
+
+ // don't rely upon findEnvironmentVariable because there might be situations
+ // when getenv() or $_ENV is set, but $cached[$name] is not,
+ // and then for later requests getenv() and $_ENV are both empty and also no value in cache,
+ // therefore this additional fix is here to ensure that we always cache the value
+
+ // but first keep the value while we haven't updated the cache because after that the value will be returned from the cache
+ // thus completely ignoring ENV, which is not what we intended
+ $oldVal = static::findEnvironmentVariable($name);
+
+ if (static::$immutable === false || !array_key_exists($name, static::$cache)){
+ static::$cache[$name] = $value;
+ }
+
// Don't overwrite existing environment variables if we're immutable
// Ruby's dotenv does this with `ENV[key] ||= value`.
- if (static::$immutable === true && !is_null(static::findEnvironmentVariable($name))) {
+ if (static::$immutable === true && !is_null($oldVal)) {
return;
}
-
+
putenv("$name=$value");
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
@@ -236,5 +255,12 @@ class Dotenv
return $_ENV[$name];
case array_key_exists($name, $_SERVER):
return $_SERVER[$name];
+ // workaround for
+ // https://github.com/laravel/framework/pull/8187
+ // and
+ // https://github.com/vlucas/phpdotenv/issues/76
+ case array_key_exists($name, static::$cache):
+ return static::$cache[$name];
+
default:
$value = getenv($name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment