Skip to content

Commit 97e6d89

Browse files
committed
Fix .env loading in the console
The console didn't load .env files at all. Fixed it by creating a new bin/console file based on the Symfony 4.4 flex recipe.
1 parent 1b9d66f commit 97e6d89

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

bin/console

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
#!/usr/bin/env php
22
<?php
33

4+
// based on https://github.com/symfony/recipes/blob/984d337aabb837ab5f20322af0290111f98c164a/symfony/console/4.4/bin/console
5+
46
use Symfony\Bundle\FrameworkBundle\Console\Application;
57
use Symfony\Component\Console\Input\ArgvInput;
6-
use Symfony\Component\Debug\Debug;
8+
use Symfony\Component\ErrorHandler\Debug;
9+
10+
if (!in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
11+
echo 'Warning: The console should be invoked via the CLI version of PHP, not the '.PHP_SAPI.' SAPI'.PHP_EOL;
12+
}
713

814
set_time_limit(0);
915

10-
require __DIR__ . '/../vendor/autoload.php';
16+
require dirname(__DIR__).'/vendor/autoload.php';
17+
18+
if (!class_exists(Application::class)) {
19+
throw new LogicException('You need to add "symfony/framework-bundle" as a Composer dependency.');
20+
}
1121

1222
$input = new ArgvInput();
13-
$env = $input->getParameterOption(['--env', '-e'], getenv('APP_ENV') ?: 'development');
14-
$debug = getenv('APP_DEBUG') != '0' && !$input->hasParameterOption(['--no-debug', '']);
23+
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
24+
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env);
25+
}
26+
27+
if ($input->hasParameterOption('--no-debug', true)) {
28+
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
29+
}
30+
31+
require dirname(__DIR__).'/config/bootstrap.php';
32+
33+
if ($_SERVER['APP_DEBUG']) {
34+
umask(0000);
1535

16-
if ($debug) {
17-
Debug::enable();
36+
if (class_exists(Debug::class)) {
37+
Debug::enable();
38+
}
1839
}
1940

20-
$kernel = new AppKernel($env, $debug);
41+
$kernel = new AppKernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
2142
$application = new Application($kernel);
2243
$application->run($input);

0 commit comments

Comments
 (0)