mirror of
https://github.com/symfony/recipes.git
synced 2026-09-12 23:56:25 +03:00
Move loading .env files to bootstrap.php
This commit is contained in:
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user