yii-starter-kit / yii2-starter-kit

Yii2 Starter Kit
http://yii2-starter-kit.terentev.net
Other
1.42k stars 648 forks source link

Environment variables bug #762

Closed andrewpros closed 4 years ago

andrewpros commented 4 years ago

This will never look further if the variables are not in the environment already.

https://github.com/yii2-starter-kit/yii2-starter-kit/blob/master/common/helpers.php#L51

If getenv returns false it will never look in $_ENV or $_SERVER as ?? operator will only trigger the right side if the getenv result is NULL and it will be never NULL.

getenv could not have the environment variables due to nginx php-fpm config

XzAeRo commented 4 years ago

We are actually using the vlucas/phpdotenv package, which loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically, so actually that piece of code is put there for redundancy.

You can see that this gets bootstrapped here: https://github.com/yii2-starter-kit/yii2-starter-kit/blob/master/common/env.php and called on all the yii.php files in all the backend, frontend, console, storage and api applications.

andrewpros commented 4 years ago

Yeah i know this and where it is used as it is not working and i already researched this, using nginx fast cgi on windows trying to run it as single domain.

And it is not redundancy as this code could never work, its wrong, if those variables are not there after calling getenv it will never check $_ENV or $_SERVER as getenv never returns NULL if there are no variables so ?? operator will never work.

It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

On my setup, for some reason getenv doesn't have the .env variables, but $_ENV and $_SERVER has.

This is supported by the docs https://www.php.net/manual/en/function.getenv.php

If PHP is running in a SAPI such as Fast CGI, this function will always return the value of an environment variable set by the SAPI, even if putenv() has been used to set a local environment variable of the same name. Use the local_only parameter to return the value of locally-set environment variables.

And phpdotenv is using putenv.

Then why did you close an apparent bug?

XzAeRo commented 4 years ago

Ok, changing the ?? operand makes sense.

On my setup, for some reason getenv doesn't have the .env variables, but $_ENV and $_SERVER has.

This might be due to Windows shenanigans. So, can you provide an example to test this bug?

andrewpros commented 4 years ago

Well i did debug it, its just the clean files, i dont see anything that could broke getenv in that helpers file.

So basically getenv should work always, but u are probably right, it will be some Windows thing connected with that phpdotenv pack or some nginx config issue under windows as the getenv is working if i just run with the default vhost config, with multiple domains and visit frontend that way, when i do changes to work as single domain suddenly getenv doesn't have the vars.|

I will try to investigate it more, but other than that its just a minor issue, im sure it will work on linux host.

andrewpros commented 4 years ago

Ok never mind i found it, has nothing to do with the app, well almost.

It was php.ini issue, for some reason the config provided with WinNMP manager i use has a disable_functions setting with a lot of settings disabled, and u guessed it, putenv was disabled, but phpdotenv use function_exists check (it doesn't exists if its disabled), so initially there is no error at all so didn't notice it, that the only thing in files that was tricky.

So it was that odd WinNMP php config issue, clean offical php packs do not have set any disable_functions, didn't know there is such thing.

So either way, we dont need that ?? operator, getenv should always work.