fuel / core

Fuel PHP Framework - The core of the Fuel v1 framework
http://fuelphp.com
811 stars 347 forks source link

How to set Environment with PHP built-in server? #1983

Closed Art4 closed 8 years ago

Art4 commented 8 years ago

I want to use the PHP built-in server for test purpose but have trouble with setting the right environment.

I'm starting the server like this:

env FUEL_ENV=test php -d variables_order=EGPCS -S localhost:8000 -t public/

But the problem is that in the built-in server the FUEL_ENV appears only in $_ENV but not in $_SERVER. I'd workaround this with adding this line in fuel/app/bootstrap.php:

Fuel::$env = (isset($_SERVER['FUEL_ENV']) ? $_SERVER['FUEL_ENV'] : (isset($_ENV['FUEL_ENV']) ? $_ENV['FUEL_ENV'] : Fuel::DEVELOPMENT));

This works, but seems ugly to me. Am I missing something and there is a better way to achieve this problem?

WanWizard commented 8 years ago

https://bugs.php.net/bug.php?id=67808

Which suggests the SAPI behaviour (of copying the data to $_SERVER) is percieved as wrong. How about:

Fuel::$env = Arr::get($_SERVER, 'FUEL_ENV', Arr::get($_ENV, 'FUEL_ENV', Fuel::DEVELOPMENT));

We could probably even switch to simply using $_ENV, that should always work.

WanWizard commented 8 years ago

Using $_ENV instead of $_SERVER seems to work fine.

WanWizard commented 8 years ago

I've committed the change to the Fuel repo.

Art4 commented 8 years ago

Great, thank you very much!

I'll use the changes from https://github.com/fuel/fuel/commit/7b0a92425a01320135f3fdb5e7d1cbe9ff318c30

WanWizard commented 8 years ago

Nope, it is more complex.

So both need to be checked.

Art4 commented 8 years ago

Symfony simply merged $_ENV into $_SERVER for the same issue, see https://github.com/symfony/symfony/commit/61763452b7d265e883f5edaf76b50d15074a8e81

WanWizard commented 8 years ago

I find that a bit of a hack, as it obscures what happens. And not very secure as well, as it allows a SERVER variable to be overwritten by an ENV variable, of which the source might not be known.