diff --git a/UPGRADE-4.2.md b/UPGRADE-4.2.md index 16e89a239f58..7dcec3f52fec 100644 --- a/UPGRADE-4.2.md +++ b/UPGRADE-4.2.md @@ -175,10 +175,29 @@ FrameworkBundle ``` * The `ContainerAwareCommand` class has been deprecated, use `Symfony\Component\Console\Command\Command` with dependency injection instead. - * The `--env` console option and its "-e" shortcut have been deprecated, - set the "APP_ENV" environment variable instead. - * The `--no-debug` console option has been deprecated, - set the "APP_DEBUG" environment variable to "0" instead. + * The `--env` and `--no-debug` console options have been deprecated, define the `APP_ENV` and + `APP_DEBUG` environment variables instead. + If you want to keep using `--env` and `--no-debug`, update your `bin/console` file to make it call + `Application::bootstrapEnv()`. + + Before: + ```php + $input = new ArgvInput(); + $env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true); + $debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption('--no-debug', true); + $kernel = new Kernel($env, $debug); + $application = new Application($kernel); + $application->run($input); + ``` + + After: + ```php + Application::bootstrapEnv($_SERVER['argv']; + $kernel = new Kernel($_SERVER['APP_ENV'], $_SERVER['APP_DEBUG']); + $application = new Application($kernel); + $application->run(); + ``` + * The `Templating\Helper\TranslatorHelper::transChoice()` method has been deprecated, use the `trans()` one instead with a `%count%` parameter. * Deprecated support for legacy translations directories `src/Resources/translations/` and `src/Resources//translations/`, use `translations/` instead. * Support for the legacy directory structure in `translation:update` and `debug:translation` commands has been deprecated. diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index 58e266faf650..00110a716dce 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -164,10 +164,29 @@ FrameworkBundle * Added support for the SameSite attribute for session cookies. It is highly recommended to set this setting (`framework.session.cookie_samesite`) to `lax` for increased security against CSRF attacks. * The `ContainerAwareCommand` class has been removed, use `Symfony\Component\Console\Command\Command` with dependency injection instead. - * The `--env` console option and its "-e" shortcut have been removed, - set the "APP_ENV" environment variable instead. - * The `--no-debug` console option has been removed, - set the "APP_DEBUG" environment variable to "0" instead. + * The `--env` and `--no-debug` console options have been removed, define the `APP_ENV` and + `APP_DEBUG` environment variables instead. + If you want to keep using `--env` and `--no-debug`, update your `bin/console` file to make it call + `Application::bootstrapEnv()`. + + Before: + ```php + $input = new ArgvInput(); + $env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true); + $debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption('--no-debug', true); + $kernel = new Kernel($env, $debug); + $application = new Application($kernel); + $application->run($input); + ``` + + After: + ```php + Application::bootstrapEnv($_SERVER['argv']; + $kernel = new Kernel($_SERVER['APP_ENV'], $_SERVER['APP_DEBUG']); + $application = new Application($kernel); + $application->run(); + ``` + * The `Templating\Helper\TranslatorHelper::transChoice()` method has been removed, use the `trans()` one instead with a `%count%` parameter. * Removed support for legacy translations directories `src/Resources/translations/` and `src/Resources//translations/`, use `translations/` instead. * Support for the legacy directory structure in `translation:update` and `debug:translation` commands has been removed. diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 56d8f1950499..f984cd71abd0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -15,10 +15,10 @@ CHANGELOG * Removed the `framework.messenger.encoder` and `framework.messenger.decoder` options. Use the `framework.messenger.serializer.id` option to replace the Messenger serializer. * Deprecated the `ContainerAwareCommand` class in favor of `Symfony\Component\Console\Command\Command` * Made `debug:container` and `debug:autowiring` ignore backslashes in service ids - * Deprecated the `--env` console option and its "-e" shortcut, set - the "APP_ENV" environment variable instead. - * Deprecated the `--no-debug` console option, set the "APP_DEBUG" - environment variable to "0" instead. + * Deprecated the `--env` console option and its "-e" shortcut, set the "APP_ENV" environment variable + or use `Application::bootstrapEnv()` instead. + * Deprecated the `--no-debug` console option, set the "APP_DEBUG" environment variable to "0" + or use `Application::bootstrapEnv()` instead. * Deprecated the `Templating\Helper\TranslatorHelper::transChoice()` method, use the `trans()` one instead with a `%count%` parameter * Deprecated `CacheCollectorPass`. Use `Symfony\Component\Cache\DependencyInjection\CacheCollectorPass` instead. * Deprecated `CachePoolClearerPass`. Use `Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass` instead. diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Application.php b/src/Symfony/Bundle/FrameworkBundle/Console/Application.php index e885dfeaa5b5..a6cabe494b40 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Application.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Application.php @@ -207,6 +207,35 @@ protected function registerCommands() } } + /** + * Defines the "APP_ENV" and "APP_DEBUG" environment variables by consuming --env and --no-debug from the command line arguments. + */ + public static function bootstrapEnv(array &$argv) + { + for ($i = 0; $i < \count($argv) && '--' !== $v = $argv[$i]; ++$i) { + if ('--no-debug' === $v) { + putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'); + $argvUnset[$i] = true; + break; + } + } + + for ($i = 0; $i < \count($argv) && '--' !== $v = $argv[$i]; ++$i) { + if (!$v || '-' !== $v[0] || !preg_match('/^-(?:-env(?:=|$)|e=?)(.*)$/D', $v, $v)) { + continue; + } + if (!empty($v[1]) || !empty($argv[1 + $i])) { + putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = empty($v[1]) ? $argv[1 + $i] : $v[1]); + $argvUnset[$i] = $argvUnset[$i + empty($v[1])] = true; + } + break; + } + + if (!empty($argvUnset)) { + $argv = array_values(array_diff_key($argv, $argvUnset)); + } + } + private function renderRegistrationErrors(InputInterface $input, OutputInterface $output) { if ($output instanceof ConsoleOutputInterface) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php index 6ac413a226fd..616711dd24a2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php @@ -283,6 +283,63 @@ private function createBundleMock(array $commands) return $bundle; } + + public function testBootstrapEnv() + { + $argv = array('--no-debug', '--env=testBootstrapEnv', 'foo=bar'); + Application::bootstrapEnv($argv); + + $this->assertSame($_SERVER['APP_DEBUG'], '0'); + $this->assertSame($_ENV['APP_DEBUG'], '0'); + $this->assertSame('0', getenv('APP_DEBUG')); + $this->assertSame('testBootstrapEnv', $_SERVER['APP_ENV']); + $this->assertSame('testBootstrapEnv', $_ENV['APP_ENV']); + $this->assertSame('testBootstrapEnv', getenv('APP_ENV')); + $this->assertSame(array('foo=bar'), $argv); + + unset($_SERVER['APP_ENV']); + unset($_ENV['APP_ENV']); + putenv('APP_ENV'); + unset($_SERVER['APP_DEBUG']); + unset($_ENV['APP_DEBUG']); + putenv('APP_DEBUG'); + + $argv = array('--env', 'testBootstrapEnv', 'foo=bar'); + Application::bootstrapEnv($argv); + + $this->assertSame('testBootstrapEnv', $_SERVER['APP_ENV']); + $this->assertSame('testBootstrapEnv', $_ENV['APP_ENV']); + $this->assertSame('testBootstrapEnv', getenv('APP_ENV')); + $this->assertSame(array('foo=bar'), $argv); + + unset($_SERVER['APP_ENV']); + unset($_ENV['APP_ENV']); + putenv('APP_ENV'); + + $argv = array('-e', 'testBootstrapEnv', 'foo=bar'); + Application::bootstrapEnv($argv); + + $this->assertSame('testBootstrapEnv', $_SERVER['APP_ENV']); + $this->assertSame('testBootstrapEnv', $_ENV['APP_ENV']); + $this->assertSame('testBootstrapEnv', getenv('APP_ENV')); + $this->assertSame(array('foo=bar'), $argv); + + unset($_SERVER['APP_ENV']); + unset($_ENV['APP_ENV']); + putenv('APP_ENV'); + + $argv = array('-e=testBootstrapEnv', 'foo=bar'); + Application::bootstrapEnv($argv); + + $this->assertSame('testBootstrapEnv', $_SERVER['APP_ENV']); + $this->assertSame('testBootstrapEnv', $_ENV['APP_ENV']); + $this->assertSame('testBootstrapEnv', getenv('APP_ENV')); + $this->assertSame(array('foo=bar'), $argv); + + unset($_SERVER['APP_ENV']); + unset($_ENV['APP_ENV']); + putenv('APP_ENV'); + } } class ThrowingCommand extends Command