dracoblue.net

Make symfony dotenv use putenv

The other day a friend of mine was using google-auth-library-php in combination with google-cloud-php-pubsub and wanted to set the path for the google credentials file via GOOGLE_APPLICATION_CREDENTIALS in .env.

So we figured that this did not work anymore in the newer version of dotenv (see commit in 4.3) the default value for use_putenv changed to false. And in symfony 5 there was a breaking change to switch use_putenv from true to false (see the putenv deprecation pull request.

The suggestions to activate use_putenv again in Dotenv via calling

$dotenv->usePutenv(true);

But it shows the reason why it goet deprecated and removed in symfony:

Beware that putenv() is not thread safe, that's why this setting defaults to false

But in symfony you don't have access to the dotenv instance and cannot preinitiate it. Some days later by accident I found that you can set runtime options for symfony runtime and that they support use_putenv.

So if you edit your index.php and bin/console files before loading the autoload runtime from

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

to:

$_SERVER['APP_RUNTIME_OPTIONS']['use_putenv'] = true;
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

you can change some of the runtime options. See SymfonyRuntime::_construct for all the available options.

For google auth php library I created a pull request to change it's usage from getenv to $_ENV.

In dotenv, gcloud, php, symfony by
@ 16 Jan 2023, Comments at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About