Skip to content

Move loading .env files to bootstrap.php #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php

App\Kernel::bootstrapEnv('test');
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = 'test');
require dirname(__DIR__, 2).'/src/.bootstrap.php';
3 changes: 2 additions & 1 deletion behat/symfony2-extension/2.1/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"copy-from-recipe": {
"behat.yml.dist": "behat.yml.dist",
"features/": "features/"
"features/": "features/",
"src/": "%SRC_DIR%/"
},
"aliases": ["behat"],
"gitignore": ["behat.yml"]
Expand Down
1 change: 1 addition & 0 deletions behat/symfony2-extension/2.1/src/.bootstrap.php
3 changes: 2 additions & 1 deletion phpunit/phpunit/4.7/.env.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# define your env variables for the test env here
KERNEL_CLASS=App\\Kernel
KERNEL_CLASS='App\Kernel'
APP_SECRET='s$cretf0rt3st'
SHELL_VERBOSITY=-1
SYMFONY_DEPRECATIONS_HELPER=999999
1 change: 1 addition & 0 deletions phpunit/phpunit/4.7/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"copy-from-recipe": {
".env.test": ".env.test",
"phpunit.xml.dist": "phpunit.xml.dist",
"src/": "%SRC_DIR%/",
"tests/": "tests/"
},
"gitignore": [
Expand Down
3 changes: 2 additions & 1 deletion phpunit/phpunit/4.7/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
bootstrap="src/.bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="APP_ENV" value="test" />
</php>

<testsuites>
Expand Down
1 change: 1 addition & 0 deletions phpunit/phpunit/4.7/src/.bootstrap.php
Empty file.
5 changes: 0 additions & 5 deletions phpunit/phpunit/4.7/tests/bootstrap.php

This file was deleted.

19 changes: 15 additions & 4 deletions symfony/console/3.3/bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@

use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;

set_time_limit(0);

require dirname(__DIR__).'/vendor/autoload.php';

if (!class_exists(Application::class)) {
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
throw new RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}

Kernel::bootstrapCli($_SERVER['argv']);
Kernel::bootstrapEnv();
$input = new ArgvInput();
if (null !== $_ENV['APP_ENV'] = $input->getParameterOption(['--env', '-e'], null, true)) {
putenv('APP_ENV='.$_ENV['APP_ENV']);
// force loading .env files when --env is defined
$_SERVER['APP_ENV'] = null;
}

if ($input->hasParameterOption('--no-debug', true)) {
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
}

require dirname(__DIR__).'/src/.bootstrap.php';

if ($_SERVER['APP_DEBUG']) {
umask(0000);
Expand All @@ -26,4 +37,4 @@ if ($_SERVER['APP_DEBUG']) {

$kernel = new Kernel($_SERVER['APP_ENV'], $_SERVER['APP_DEBUG']);
$application = new Application($kernel);
$application->run();
$application->run($input);
3 changes: 2 additions & 1 deletion symfony/console/3.3/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"copy-from-recipe": {
"bin/": "%BIN_DIR%/"
"bin/": "%BIN_DIR%/",
"src/": "%SRC_DIR%/"
},
"aliases": ["cli"]
}
1 change: 1 addition & 0 deletions symfony/console/3.3/src/.bootstrap.php
1 change: 1 addition & 0 deletions symfony/flex/1.0/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This file defines all environment variables that the application needs.
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE.
# Use ".env.local" for local overrides during development.
# Use real environment variables when deploying to production.
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration
4 changes: 1 addition & 3 deletions symfony/framework-bundle/3.3/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;

require dirname(__DIR__).'/vendor/autoload.php';

Kernel::bootstrapEnv();
require dirname(__DIR__).'/src/.bootstrap.php';

if ($_SERVER['APP_DEBUG']) {
umask(0000);
Expand Down
51 changes: 51 additions & 0 deletions symfony/framework-bundle/3.3/src/.bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use Symfony\Component\Dotenv\Dotenv;

require dirname(__DIR__).'/vendor/autoload.php';

if (!array_key_exists('APP_ENV', $_SERVER)) {
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] ?? null;
}

if ('prod' !== $_SERVER['APP_ENV']) {
if (!class_exists(Dotenv::class)) {
throw new RuntimeException('The "APP_ENV" environment variable is not set to "prod". Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
}

$path = dirname(__DIR__).'/.env';
$dotenv = new Dotenv();

if (method_exists($dotenv, 'loadEnv')) {
$dotenv->loadEnv($path);
} else {
// fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added)

if (file_exists($path) || !file_exists($p = "$path.dist")) {
$dotenv->load($path);
} else {
$dotenv->load($p);
}

if (null === $env = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) {
$dotenv->populate(array('APP_ENV' => $env = 'dev'));
}

if ('test' !== $env && file_exists($p = "$path.local")) {
$dotenv->load($p);
$env = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? $env;
}

if (file_exists($p = "$path.$env")) {
$dotenv->load($p);
}

if (file_exists($p = "$path.$env.local")) {
$dotenv->load($p);
}
}
}

$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $_SERVER['APP_ENV'] ?: $_ENV['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';
81 changes: 1 addition & 80 deletions symfony/framework-bundle/3.3/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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;

Expand All @@ -30,7 +29,7 @@ public function registerBundles()
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}
Expand Down Expand Up @@ -59,82 +58,4 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
$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

// when using symfony/console v4.2 or higher, this should
// be replaced by a call to Application::bootstrapEnv()

for ($i = 0; $i < \count($argv) && '--' !== $v = $argv[$i]; ++$i) {
if ('--no-debug' === $v) {
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
$argvUnset[$i] = true;
break;
}
}

for ($i = 0; $i < \count($argv) && '--' !== $v = $argv[$i]; ++$i) {
if (!$v || '-' !== $v[0] || !preg_match('/^-(?:-env(?:=|$)|e=?)(.*)$/D', $v, $v)) {
continue;
}
if (!empty($v[1]) || !empty($argv[1 + $i])) {
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = empty($v[1]) ? $argv[1 + $i] : $v[1]);
$argvUnset[$i] = $argvUnset[$i + empty($v[1])] = true;
}
break;
}

if (!empty($argvUnset)) {
$argv = array_values(array_diff_key($argv, $argvUnset));
}
}

public static function bootstrapEnv($env = null)
{
if (null !== $env) {
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $env);
}

if ('prod' !== $_SERVER['APP_ENV'] = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['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.');
}

// when using symfony/dotenv v4.2 or higher, this call and the related methods
// below should be replaced by a call to the new Dotenv::loadEnv() method
self::loadEnv(new Dotenv(), \dirname(__DIR__).'/.env');
}

$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';
$_SERVER['APP_DEBUG'] = isset($_SERVER['APP_DEBUG']) ? $_SERVER['APP_DEBUG'] : (isset($_ENV['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';
}

private static function loadEnv(Dotenv $dotenv, $path)
{
if (file_exists($path) || !file_exists($p = "$path.dist")) {
$dotenv->load($path);
} else {
$dotenv->load($p);
}

if (null === $env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : null)) {
$dotenv->populate(array('APP_ENV' => $env = 'dev'));
}

if ('test' !== $env && file_exists($p = "$path.local")) {
$dotenv->load($p);
$env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : $env);
}

if (file_exists($p = "$path.$env")) {
$dotenv->load($p);
}

if (file_exists($p = "$path.$env.local")) {
$dotenv->load($p);
}
}
}
4 changes: 1 addition & 3 deletions symfony/framework-bundle/4.2/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;

require dirname(__DIR__).'/vendor/autoload.php';

Kernel::bootstrapEnv();
require dirname(__DIR__).'/src/.bootstrap.php';

if ($_SERVER['APP_DEBUG']) {
umask(0000);
Expand Down
21 changes: 21 additions & 0 deletions symfony/framework-bundle/4.2/src/.bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Symfony\Component\Dotenv\Dotenv;

require dirname(__DIR__).'/vendor/autoload.php';

if (!array_key_exists('APP_ENV', $_SERVER)) {
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] ?? null;
}

if ('prod' !== $_SERVER['APP_ENV']) {
if (!class_exists(Dotenv::class)) {
throw new RuntimeException('The "APP_ENV" environment variable is not set to "prod". Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
}

(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
}

$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $_SERVER['APP_ENV'] ?: $_ENV['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';
29 changes: 1 addition & 28 deletions symfony/framework-bundle/4.2/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,7 +29,7 @@ public function registerBundles()
{
$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();
}
}
Expand All @@ -57,29 +55,4 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
$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';
}
}
3 changes: 1 addition & 2 deletions symfony/phpunit-bridge/3.3/.env.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# define your env variables for the test env here
KERNEL_CLASS=App\\Kernel
KERNEL_CLASS='App\Kernel'
APP_SECRET='s$cretf0rt3st'
SHELL_VERBOSITY=-1
SYMFONY_DEPRECATIONS_HELPER=999999
SYMFONY_PHPUNIT_VERSION=6.5
7 changes: 3 additions & 4 deletions symfony/phpunit-bridge/3.3/bin/phpunit
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ if (!file_exists(dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-php
exit(1);
}

$classLoader = require dirname(__DIR__).'/vendor/autoload.php';
App\Kernel::bootstrapEnv('test');
$classLoader->unregister();

if (false === getenv('SYMFONY_PHPUNIT_VERSION')) {
putenv('SYMFONY_PHPUNIT_VERSION=6.5');
}
if (false === getenv('SYMFONY_PHPUNIT_REMOVE')) {
putenv('SYMFONY_PHPUNIT_REMOVE=symfony/yaml');
}
Expand Down
1 change: 1 addition & 0 deletions symfony/phpunit-bridge/3.3/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"bin/": "%BIN_DIR%/",
"config/": "%CONFIG_DIR%/",
"phpunit.xml.dist": "phpunit.xml.dist",
"src/": "%SRC_DIR%/",
"tests/": "tests/"
},
"gitignore": [
Expand Down
Loading