Last active
September 11, 2021 20:56
-
-
Save progmars/5c18235360c04b621ed7 to your computer and use it in GitHub Desktop.
Workaround for Dotenv and Laravel 5.x issues with concurrent requests failing (especially on Windows) because of PHP threaded getenv/putenv implementation. To make this work on Laravel, you will need also https://gist.github.com/progmars/1e545d96dd48676a2aa7 patch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a workaround patch for `getenv()` unstable behavior in multithreaded "PHP as Apache module" environments | |
(e.g. some prepackaged WAMP stacks). | |
See | |
https://github.com/vlucas/phpdotenv/issues/76 | |
https://github.com/vlucas/phpdotenv/issues/163 | |
This fix is intended only for development environments, the same as Dotenv itself. | |
In production, you should use Laravel's `config:cache` command to avoid `env` helper and Dotenv altogether. | |
The paths are based on Composer's vendor directory. | |
diff --git a/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/DetectEnvironment.php b/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/DetectEnvironment.php | |
index 673ce3a..f250729 100644 | |
--- a/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/DetectEnvironment.php | |
+++ b/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/DetectEnvironment.php | |
@@ -17,11 +17,18 @@ class DetectEnvironment | |
public function bootstrap(Application $app) | |
{ | |
if (! $app->configurationIsCached()) { | |
+ // Patched to support more reliable environment value lookup | |
+ // in some environments. | |
+ // See | |
+ // https://github.com/vlucas/phpdotenv/issues/76 | |
+ // https://github.com/vlucas/phpdotenv/issues/163 | |
+ $dotenv = new Dotenv($app->environmentPath(), $app->environmentFile()); | |
try { | |
- (new Dotenv($app->environmentPath(), $app->environmentFile()))->load(); | |
+ $dotenv->load(); | |
} catch (InvalidPathException $e) { | |
// | |
} | |
+ $app->instance('dotenv.loader', $dotenv->getLoader()); | |
} | |
} | |
} | |
diff --git a/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php b/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php | |
index 05541f7..65905fd 100644 | |
--- a/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php | |
+++ b/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php | |
@@ -330,9 +330,15 @@ if (! function_exists('env')) { | |
*/ | |
function env($key, $default = null) | |
{ | |
- $value = getenv($key); | |
- | |
- if ($value === false) { | |
+ // Patched to support more reliable environment value lookup | |
+ // in some environments. | |
+ // See | |
+ // https://github.com/vlucas/phpdotenv/issues/76 | |
+ // https://github.com/vlucas/phpdotenv/issues/163 | |
+ $value = app('dotenv.loader')->getEnvironmentVariable($key); | |
+ | |
+ // Changed to `null` for getEnvironmentVariable() result check. | |
+ if ($value === null) { | |
return value($default); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment