-
-
Notifications
You must be signed in to change notification settings - Fork 495
Allow to override .env files for specific environments #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request does not pass validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request does not pass validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
symfony/flex#318 would be required after this one for Flex to stop updating the phpunit.xml.dist
file in favor of the .env.test
file.
@maxhelias @ogizanagi I was thinking about introducing an env var simply called To run the command in prod env: WDYT? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like the idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
To upgrade, users will have to use a script like this one: cat composer.json | jq --indent 4 '.require["symfony/flex"] = "^2.0"' > composer.tmp
mv composer.tmp composer.json
cp .env .env.local
cp .env.dist .env
sed -i'' -e 's/\/.env/\/.env.local\
\/.env.*.local/' .gitignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
Should we also deprecate the default value notation of services files? # config/services.yaml
parameters:
env(DATABASE_HOST): localhost https://symfony.com/doc/current/configuration/external_parameters.html#environment-variables |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request passes validation.
|
||
Debug::enable(); | ||
} | ||
$kernel = require __DIR__.'/../src/bootstrap.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of having a bootstrap.php
file. what about moving the logic to the Kernel
class instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it won't work for PHPUnit (it is not able to call a method to bootstrap).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The src/bootstrap.php
file doesn't bother me. But if you prefer, we could move this logic to the kernel and only keep a test/bootstrap.php
file used by phpunit? That's already common to need such a file to add specific code before running tests and would avoid requiring 2 bootstrap.php
files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a static method on Kernel and then reusing it in a bootstrap file for PHPUnit sounds like a better idea to me.
@B-Galati : What do you mean in #466 (comment)? |
@ogizanagi it does not relate sorry. I will open an issue on Symfony repository. |
|
||
$envFromEnv = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null; | ||
$env = $envFromEnv ?? 'dev'; | ||
$prod = 'prod' === $env; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to go one step further and to not load .env
if the environment name doesn't start with dev
or test
(.env.test-e2e
will be loaded, but not .staging
). WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My opinion is that it complicates everything to introduce more and more exceptions and would make it far from intuitive. The prod env restriction has a reason, it's a safeguard for real production environments.
For other env, I don't understand the reasons yet. The main reason for this feature to exist is env.test
and that's how it'll be used 95% of the time. But some applications defines their own specific symfony env(s) and may also benefit from this. And if you don't, then you shouldn't care putting arbitrary restrictions on a feature already based on conventions :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m fine with this
|
||
$kernel = require __DIR__.'/../src/bootstrap.php'; | ||
$application = new Application($kernel); | ||
$application->run($input); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this variable $input
is not defined though the code cannot work
|
||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
|
||
if (!class_exists(Application::class)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the autoload has not yet been called at this point so this condition is always false and always throw the exception even if framework-bundle
is installed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe because there is no require __DIR__.'/../vendor/autoload.php';
in this file
@@ -0,0 +1,29 @@ | |||
<?php |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this class lives under src/
which is autoloaded, so composer excepts a class which this file is not. It gives the following error :
In FileLoader.php line 168:
The autoloader expected class "App\bootstrap" to be defined in file "/srv/app/vendor/composer/../../src/bootstrap.php". The file was found but the c
lass was not in it, the class name or namespace probably has a typo in /srv/app/config/services.yaml (which is loaded in resource "/srv/app/config/s
ervices.yaml").
In DebugClassLoader.php line 204:
The autoloader expected class "App\bootstrap" to be defined in file "/srv/app/vendor/composer/../../src/bootstrap.php". The file was found but the c
lass was not in it, the class name or namespace probably has a typo.
Closing in favor of #481 |
As discussed in #465 and symfony/symfony#28533, this PR makes Symfony behaving like Create React App, Rails or Sinatra: the following files, starting from the bottom. The first value set (or those already defined in the environment) take precedence:
.env
- The Original®.env.dev
,.env.test
,.env.prod
... - Environment-specific settings..env.local
- Local overrides. This file is loaded for all environments excepttest
..env.dev.local
,.env.test.local
,.env.prod.local
... - Local overrides of environment-specific settings.The default envs are
env
andtest
.It also fixes #366 (comment): when running
bin/console -e=prod
, the.env.prod
file will be loaded (ifAPP_ENV
is not already defined).Needs:
Closes #465, #366, #408.