Skip to content

Commit 9327873

Browse files
author
Robin Chalas
committed
[FrameworkBundle] Define APP_ENV/APP_DEBUG from argv via Application::bootstrapEnv()
1 parent 62ddd10 commit 9327873

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

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.

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) {

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)