codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.35k stars 1.9k forks source link

Bug: can't get environment variable with dot #4026

Closed kenjis closed 3 years ago

kenjis commented 3 years ago

Describe the bug We can't get environment variable that name has dot on some environment. So we can't change Config values.

docker-compose.yml:

services:
  web:
    ...
    environment:
      - database.default.port=${DB_PORT:-3306}
      - database_default_port=${DB_PORT:-3306}

httpd.conf:

    PassEnv database.default.port
    PassEnv database_default_port

controller code:

dd(getenv('database.default.port')); // => false 
dd(getenv('database_default_port')); // => string 3306

CodeIgniter 4 version 4.0.4

Affected module(s) Out of CodeIgniter?

Expected behavior, and steps to reproduce if appropriate Environment variable name with dot is difficult to handle. It is better to support variable name without dot.

Context

michalsn commented 3 years ago

It's a Docker thing (it can't handle the dots) and there is nothing we can do about it. You can use __construct() in your config class to override the variables you want.

Changing dots to underscores on a framework level won't happen at this stage - besides this is an "issue" only for Docker and there is a workaround for it anyway.

I'm closing this, but feel free to continue the conversation - especially if you have ideas to solve it better. Thanks.

kenjis commented 3 years ago

@michalsn It is not a Docker thing. Because I can get the variable in the bash and Perl.

$ docker-compose exec web bash
# perl -e 'print $ENV{"database.default.port"}."\n"'
3306

But I can't get the variable with PHP. Is it a PHP thing?

# php -r 'echo $_ENV["database.default.port"];'
[02-Jan-2021 11:36:05 UTC] PHP Notice:  Undefined index: database.default.port in Command line code on line 1

Notice: Undefined index: database.default.port in Command line code on line 1

And it seems heroku also can't handle environment variables with dot. Is there any environment that can handle environment variables with dot?

kenjis commented 3 years ago

It is not a PHP thing.

# php -r 'echo getenv("database.default.port").PHP_EOL;'
3306

Is it an Apache thing?

michalsn commented 3 years ago

I guess $_ENV is not populated? I'm not an expert on stuff like this but maybe changing config for variabls-order will help? Or it just can't be handled via $_ENV because of the dots.

Watching at your second example - where you use getenv() maybe it's a deeper thing that should be configured - passing variables from Apache to PHP? Maybe using SetEnv? I'm not gonna lie, that I'm a DevOps expert - it's just a shot.

Maybe I wasn't right when I said it's a Docker thing but what I meant was that environment variables with dots are not valid names on Unix systems. Maybe there is a way to make it work - as I said, I'm not a DevOps expert in any way, but I don't think it's a CodeIgniter issue.

kenjis commented 3 years ago

CloudFormation also can't handle environment variables with dot.

After googling around I came to the conclusion that it is a combination of issues here but the main culprit is the bash-shell (which Ubuntu is using, which is the base of the Docker image) defining variable names as strictly alphanumeric and underscore - dot is not allowed! https://forum.codeigniter.com/thread-78375.html

kenjis commented 3 years ago

@michalsn

I'm closing this, but feel free to continue the conversation - especially if you have ideas to solve it better. Thanks.

See #5156

pandaatrail commented 2 years ago

Hi,

It's a little bit comic to read it's a "Docker thing". How many people use Docker in the world ?...and how many people use CI ? If I take a look at Symfony's doc, I don't see any dot anywhere. Maybe because the "default" dotenv notation is pretty standard now ?

When we know CI env variables implementation came ages after other actors on the market, I really wonder why:

Cheers,