8000 bug #29154 [FrameworkBundle] Define APP_ENV/APP_DEBUG from argv via A… · symfony/symfony@9253199 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 9253199

Browse files
bug #29154 [FrameworkBundle] Define APP_ENV/APP_DEBUG from argv via Application::bootstrapEnv() (chalasr)
This PR was merged into the 4.2-dev branch. Discussion ---------- [FrameworkBundle] Define APP_ENV/APP_DEBUG from argv via Application::bootstrapEnv() | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28984, #29126 | License | MIT | Doc PR | todo Replaces #29126. Commits ------- bbd5682 [FrameworkBundle] Define APP_ENV/APP_DEBUG from argv via Application::bootstrapEnv()
2 parents 664a032 + bbd5682 commit 9253199

File tree

5 files changed

+136
-12
lines changed

5 files changed

+136
-12
lines changed

UPGRADE-4.2.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,29 @@ FrameworkBundle
175175
```
176176
* The `ContainerAwareCommand` class has been deprecated, use `Symfony\Component\Console\Command\Command`
177177
with dependency injection instead.
178-
* The `--env` console option and its "-e" shortcut have been deprecated,
179-
set the "APP_ENV" environment variable instead.
180-
* The `--no-debug` console option has been deprecated,
181-
set the "APP_DEBUG" environment variable to "0" instead.
178+
* The `--env` and `--no-debug` console options have been deprecated, define the `APP_ENV` and
179+
`APP_DEBUG` environment variables instead.
180+
If you want to keep using `--env` and `--no-debug`, update your `bin/console` file to make it call
181+
`Application::bootstrapEnv()`.
182+
183+
Before:
184+
```php
185+
$input = new ArgvInput();
186+
$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true);
187+
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption('--no-debug', true);
188+
$kernel = new Kernel($env, $debug);
189+
$application = new Application($kernel);
190+
$application->run($input);
191+
```
192+
193+
After:
194+
```php
195+
Application::bootstrapEnv($_SERVER['argv'];
196+
$kernel = new Kernel($_SERVER['APP_ENV'], $_SERVER['APP_DEBUG']);
197+
$application = new Application($kernel);
198+
$application->run();
199+
```
200+
182201
* The `Templating\Helper\TranslatorHelper::transChoice()` method has been deprecated, use the `trans()` one instead with a `%count%` parameter.
183202
* Deprecated support for legacy translations directories `src/Resources/translations/` and `src/Resources/<BundleName>/translations/`, use `translations/` instead.
184203
* Support for the legacy directory structure in `translation:update` and `debug:translation` commands has been deprecated.

UPGRADE-5.0.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,29 @@ FrameworkBundle
164164
* 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.
165165
* The `ContainerAwareCommand` class has been removed, use `Symfony\Component\Console\Command\Command`
166166
with dependency injection instead.
167-
* The `--env` console option and its "-e" shortcut have been removed,
168-
set the "APP_ENV" environment variable instead.
169-
* The `--no-debug` console option has been removed,
170-
set the "APP_DEBUG" environment variable to "0" instead.
167+
* The `--env` and `--no-debug` console options have been removed, define the `APP_ENV` and
168+
`APP_DEBUG` environment variables instead.
169+
If you want to keep using `--env` and `--no-debug`, update your `bin/console` file to make it call
170+
`Application::bootstrapEnv()`.
171+
172+
Before:
173+
```php
174+
$input = new ArgvInput();
175+
$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true);
176+
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption('--no-debug', true);
177+
$kernel = new Kernel($env, $debug);
178+
$application = new Application($kernel);
179+
$application->run($input);
180+
```
181+
182+
After:
183+
```php
184+
Application::bootstrapEnv($_SERVER['argv'];
185+
$kernel = new Kernel($_SERVER['APP_ENV'], $_SERVER['APP_DEBUG']);
186+
$application = new Application($kernel);
187+
$application->run();
188+
```
189+
171190
* The `Templating\Helper\TranslatorHelper::transChoice()` method has been removed, use the `trans()` one instead with a `%count%` parameter.
172191
* Removed support for legacy translations directories `src/Resources/translations/` and `src/Resources/<BundleName>/translations/`, use `translations/` instead.
173192
* Support for the legacy directory structure in `translation:update` and `debug:translation` commands has been removed.

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ CHANGELOG
1515
* Removed the `framework.messenger.encoder` and `framework.messenger.decoder` options. Use the `framework.messenger.serializer.id` option to replace the Messenger serializer.
1616
* Deprecated the `ContainerAwareCommand` class in favor of `Symfony\Component\Console\Command\Command`
1717
* Made `debug:container` and `debug:autowiring` ignore backslashes in service ids
18-
* Deprecated the `--env` console option and its "-e" shortcut, set
19-
the "APP_ENV" environment variable instead.
20-
* Deprecated the `--no-debug` console option, set the "APP_DEBUG"
21-
environment variable to "0" instead.
18+
* Deprecated the `--env` console option and its "-e" shortcut, set the "APP_ENV" environment variable
19+
or use `Application::bootstrapEnv()` instead.
20+
* Deprecated the `--no-debug` console option, set the "APP_DEBUG" environment variable to "0"
21+
or use `Application::bootstrapEnv()` instead.
2222
* Deprecated the `Templating\Helper\TranslatorHelper::transChoice()` method, use the `trans()` one instead with a `%count%` parameter
2323
* Deprecated `CacheCollectorPass`. Use `Symfony\Component\Cache\DependencyInjection\CacheCollectorPass` instead.
2424
* Deprecated `CachePoolClearerPass`. Use `Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass` instead.

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,35 @@ protected function registerCommands()
207207
}
208208
}
209209

210+
/**
211+
* Defines the "APP_ENV" and "APP_DEBUG" environment variables by consuming --env and --no-debug from the command line arguments.
212+
*/
213+
public static function bootstrapEnv(array &$argv)
214+
{
215+
for ($i = 0; $i < \count($argv) && '--' !== $v = $argv[$i]; ++$i) {
216+
if ('--no-debug' === $v) {
217+
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
218+
$argvUnset[$i] = true;
219+
break;
220+
}
221+
}
222+
223+
for ($i = 0; $i < \count($argv) && '--' !== $v = $argv[$i]; ++$i) {
224+
if (!$v || '-' !== $v[0] || !preg_match('/^-(?:-env(?:=|$)|e=?)(.*)$/D', $v, $v)) {
225+
continue;
226+
}
227+
if (!empty($v[1]) || !empty($argv[1 + $i])) {
228+
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = empty($v[1]) ? $argv[1 + $i] : $v[1]);
229+
$argvUnset[$i] = $argvUnset[$i + empty($v[1])] = true;
230+
}
231+
break;
232+
}
233+
234+
if (!empty($argvUnset)) {
235+
$argv = array_values(array_diff_key($argv, $argvUnset));
236+
}
237+
}
238+
210239
private function renderRegistrationErrors(InputInterface $input, OutputInterface $output)
211240
{
212241
if ($output instanceof ConsoleOutputInterface) {

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,63 @@ private function createBundleMock(array $commands)
283283

284284
return $bundle;
285285
}
286+
287+
public function testBootstrapEnv()
288+
{
289+
$argv = array('--no-debug', '--env=testBootstrapEnv', 'foo=bar');
290+
Application::bootstrapEnv($argv);
291+
292+
$this->assertSame($_SERVER['APP_DEBUG'], '0');
293+
$this->assertSame($_ENV['APP_DEBUG'], '0');
294+
$this->assertSame('0', getenv('APP_DEBUG'));
295+
$this->assertSame('testBootstrapEnv', $_SERVER['APP_ENV']);
296+
$this->assertSame('testBootstrapEnv', $_ENV['APP_ENV']);
297+
$this->assertSame('testBootstrapEnv', getenv('APP_ENV'));
298+
$this->assertSame(array('foo=bar'), $argv);
299+
300+
unset($_SERVER['APP_ENV']);
301+
unset($_ENV['APP_ENV']);
302+
putenv('APP_ENV');
303+
unset($_SERVER['APP_DEBUG']);
304+
unset($_ENV['APP_DEBUG']);
305+
putenv('APP_DEBUG');
306+
307+
$argv = array('--env', 'testBootstrapEnv', 'foo=bar');
308+
Application::bootstrapEnv($argv);
309+
310+
$this->assertSame('testBootstrapEnv', $_SERVER['APP_ENV']);
311+
$this->assertSame('testBootstrapEnv', $_ENV['APP_ENV']);
312+
$this->assertSame('testBootstrapEnv', getenv('APP_ENV'));
313+
$this->assertSame(array('foo=bar'), $argv);
314+
315+
unset($_SERVER['APP_ENV']);
316+
unset($_ENV['APP_ENV']);
317+
putenv('APP_ENV');
318+
319+
$argv = array('-e', 'testBootstrapEnv', 'foo=bar');
320+
Application::bootstrapEnv($argv);
321+
322+
$this->assertSame('testBootstrapEnv', $_SERVER['APP_ENV']);
323+
$this->assertSame('testBootstrapEnv', $_ENV['APP_ENV']);
324+
$this->assertSame('testBootstrapEnv', getenv('APP_ENV'));
325+
$this->assertSame(array('foo=bar'), $argv);
326+
327+
unset($_SERVER['APP_ENV']);
328+
unset($_ENV['APP_ENV']);
329+
putenv('APP_ENV');
330+
331+
$argv = array('-e=testBootstrapEnv', 'foo=bar');
332+
Application::bootstrapEnv($argv);
333+
334+
$this->assertSame('testBootstrapEnv', $_SERVER['APP_ENV']);
335+
$this->assertSame('testBootstrapEnv', $_ENV['APP_ENV']);
336+
$this->assertSame('testBootstrapEnv', getenv('APP_ENV'));
337+
$this->assertSame(array('foo=bar'), $argv);
338+
339+
unset($_SERVER['APP_ENV']);
340+
unset($_ENV['APP_ENV']);
341+
putenv('APP_ENV');
342+
}
286343
}
287344

288345
class ThrowingCommand extends Command

0 commit comments

Comments
 (0)
0