Skip to content

Instantly share code, notes, and snippets.

@progmars
Last active March 10, 2016 10:53
Show Gist options
  • Save progmars/e750f46c8c12e21a10ea to your computer and use it in GitHub Desktop.
Save progmars/e750f46c8c12e21a10ea to your computer and use it in GitHub Desktop.
Fix for Dotenv 1.1.1 and Laravel 5.1.7 issues with concurrent requests failing because of PHP threaded getenv/putenv implementation. This fix does not work for Laravel 5.2, see https://gist.github.com/progmars/5c18235360c04b621ed7 for the L5.2 fix.
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
@@ -625,10 +625,15 @@ 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
@@ -13,7 +13,8 @@ class Dotenv
* @var bool
*/
protected static $immutable = true;
-
+ protected static $cache = [];
+
/**
* Load `.env` file in given directory.
*
@@ -77,12 +78,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;
@@ -263,5 +282,11 @@ 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