Move loading .env files to bootstrap.php

This commit is contained in:
Nicolas Grekas
2018-11-15 00:20:15 +01:00
parent 8f1a28ffc9
commit 7ff50214ee
29 changed files with 121 additions and 170 deletions
+1 -28
View File
@@ -2,12 +2,10 @@
namespace App;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
@@ -31,7 +29,7 @@ class Kernel extends BaseKernel
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if (($envs['all'] ?? false) && ($envs[$this->environment] ?? true) || ($envs[$this->environment] ?? false)) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}
@@ -57,29 +55,4 @@ class Kernel extends BaseKernel
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
}
public static function bootstrapCli(array &$argv)
{
// consume --env and --no-debug from the command line
Application::bootstrapEnv($argv);
}
public static function bootstrapEnv(string $env = null)
{
if (null !== $env) {
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $env);
}
if ('prod' !== $_SERVER['APP_ENV'] = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('The "APP_ENV" environment variable is not defined. You need to set it or run "composer require symfony/dotenv" to load it from a ".env" file.');
}
(new Dotenv())->loadEnv(\dirname(__DIR__).'/.env');
}
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $_SERVER['APP_ENV'] ?? $_SERVER['APP_ENV'] ?? 'dev';
$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV'];
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';
}
}