-
Notifications
You must be signed in to change notification settings - Fork 24
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
Require bootstrap.php if exists, to load all necessary .env files #4
Conversation
@TavoNiievez via slack:
a solution can be that we use the kernel constant to handle the dotenv loading correctly between the different symfony versions. something like: if (Kernel::VERSION_ID < 50000) {
// load bootstrap file
} else if (...) {
// just instance the dotenv component
} most current version (symfony 5.2) to boot up dotenv if (!class_exists(Application::class) || !class_exists(Dotenv::class)) {
throw new LogicException('You need to add "symfony/framework-bundle" and "symfony/dotenv" as Composer dependencies.');
}
$input = new ArgvInput();
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env);
}
if ($input->hasParameterOption('--no-debug', true)) {
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
}
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
if ($_SERVER['APP_DEBUG']) {
umask(0000);
if (class_exists(Debug::class)) {
Debug::enable();
}
}
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$application = new Application($kernel);
$application->run($input); so we will end up to something like //should already be autoloaded here i assume
if (!class_exists(Dotenv::class)) {
throw new LogicException('You need to add "symfony/dotenv" as Composer dependencies.');
}
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $this->config['environment']); //$this->config['environment'] should have `test` as default
//maybe also add a `debug` config setting
//read the kernel version but allow the user to override it via config. of course we should use better names (constants) than `1`,`2` and `3`. default should be `0` to use the kernel version by default.
//don't checked which version did the change. i think it was 5.0 but we have to check the dotenv package
if (Kernel::VERSION_ID >= 50000 || $config[env_handling] == '3') {
//https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/5.1/public/index.php#L10
//https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/5.2/public/index.php#L10
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
} else if ((Kernel::VERSION_ID < 50000 && file_exists) || $config[env_handling] == '2') {
//check if file exists. the bootstrap.php file is only generated by flex on new projects. so it's possible to have symfony 3.4 projects with the old loading behavior
//https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/3.4/config/bootstrap.php //same as 3.3
//https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.2/config/bootstrap.php
//https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.4/config/bootstrap.php
require dirname(__DIR__).'/config/bootstrap.php';
} else if (Kernel::VERSION_ID <= 30400 || $config[env_handling] == '1') {
//older symfony versions loaded their data via parameters and prefixed environment variables
//https://github.com/symfony/symfony-standard/blob/3.4/bin/console
} else {
//exception
}
// looks like the kernel should be booted after the environment is set
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
can't we put it into 1.x with the same behavior then before and do a deprecated warning. with 2.x we change the default behavior and remove the deprecated warning. references: |
after i posted i had an even better idea. the https://github.com/symfony/dotenv/blob/5.x/CHANGELOG.md https://packagist.org/packages/composer/package-versions-deprecated (we should use the deprecated version of this package as the version ocramius requires to high php versions most of the time.
then parse it and comare it with |
cb0fcde
to
d0a2879
Compare
@Naktibalda I'm ready to release version I never had the issue that this PR solves, maybe because I added If this requires a lot more work and is a BC it can be redirected to Milestone Update: This is what I did. It works in Symfony 5.0+ but something is wrong in lower ones, I still don't know exactly what: private function handleEnviroment(): void
{
$_ENV['APP_ENV'] = $this->config['environment'];
$handled = false;
if(Kernel::VERSION_ID >= 50000) {
if ($handled = method_exists(Dotenv::class, 'bootEnv')) {
(new Dotenv())->bootEnv('.env');
}
} else {
$bootstrapFile = $this->kernel->getProjectDir() . '/config/bootstrap.php';
if ($handled = file_exists($bootstrapFile)) {
require $bootstrapFile;
}
}
if (!$handled) {
throw new \LogicException(
"Please update your symfony/framework-bundle recipe by running:\n"
. "composer recipes:install symfony/framework-bundle --force"
);
}
} |
to load all necessary .env files
d0a2879
to
cd61f6a
Compare
Replaced by #190. |
Fixes https://github.com/Codeception/Codeception/issues/5411
Must be released as 2.0.0