googleapis / google-auth-library-php

Google Auth Library for PHP
http://googleapis.github.io/google-auth-library-php/
Apache License 2.0
1.32k stars 189 forks source link

feat: add support for using $_ENV instead of getenv/putenv #432

Open bshaffer opened 1 year ago

bshaffer commented 1 year ago

getenv and putenv are considered not threadsafe. As a result, We should expose a way (similar to symphony's usePutenv function) to use the threadsafe version ($_ENV) instead.

As @DracoBlue mentioned in https://github.com/googleapis/google-auth-library-php/pull/429:

The current version of google auth library php uses getenv/putenv to read and set environment variables in php. But the function putenv is not threadsafe, which made libraries (Dotenv https://github.com/symfony/dotenv/blob/6.2/Dotenv.php#L61 and phpdotenv https://github.com/vlucas/phpdotenv#putenv-and-getenv) to disable putenv support by default. Frameworks like symfony (deprecated in 4.3 https://github.com/symfony/symfony/pull/31062 and removed in 5.0) and also in laravel there was a similiar solution to not use it anymore directly (https://github.com/vlucas/phpdotenv/issues/76).

Thus here is a pull request to change all the ocurences of:

  • getenv($variable) to array_key_exists($variable, $_ENV) ? $_ENV[$variable] : false
  • putenv($variable) to unset($_ENV[$variable])
  • putenv($variable . "=") to $_ENV[$variable]=''
  • putenv($variable . "=" . $value) to $_ENV[$variable]=$value

Threads about this:

DracoBlue commented 1 year ago

Thanks for creating the feature request!

faizanakram99 commented 4 hours ago

Isn't this change safe and non BC?

-        $path = getenv(self::ENV_VAR);
+        $path = getenv(self::ENV_VAR) ?: $_ENV[self::ENV_VAR] ?: $_SERVER[self::ENV_VAR];

here https://github.com/googleapis/google-auth-library-php/blob/e10dc3fb43b6f76c717d10f553104431181a7686/src/CredentialsLoader.php#L79