Skip to content

Bootstrap environment #408

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

Closed
wants to merge 5 commits into from
Closed
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
9 changes: 4 additions & 5 deletions phpunit/phpunit/4.7/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<env name="APP_ENV" value="test" />
<env name="APP_DEBUG" value="1" />
<env name="APP_SECRET" value="s$cretf0rt3st" />
<env name="SHELL_VERBOSITY" value="-1" />
<!-- define your env variables for the test env here -->

<!-- override or set env variables for the test env here -->
<env name="APP_ENV" value="test" />
</php>

<testsuites>
Expand Down
18 changes: 18 additions & 0 deletions phpunit/phpunit/4.7/tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require __DIR__.'/../vendor/autoload.php';

use App\Kernel;

/*
* Environment variables can also be specified in phpunit.xml.dist.
* Those variables will override any defined in .env.
*/

Kernel::bootstrapEnvironment($_ENV['APP_ENV'] ?? null);

$debug = $_SERVER['APP_DEBUG'] ?? true;

if ($debug) {
umask(0000);
}
13 changes: 4 additions & 9 deletions symfony/console/3.3/bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;

set_time_limit(0);

Expand All @@ -15,15 +14,11 @@ if (!class_exists(Application::class)) {
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}

if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
}

$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true);
$env = $input->getParameterOption(['--env', '-e'], null, true);

Kernel::bootstrapEnvironment($env);

$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption('--no-debug', true);

if ($debug) {
Expand Down
9 changes: 1 addition & 8 deletions symfony/framework-bundle/3.3/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

use App\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;

require __DIR__.'/../vendor/autoload.php';

// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
}
Kernel::bootstrapEnvironment();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should SET $_SERVER['APP_ENV'] inside Kernel::bootstrapEnvironment();. Basically, move this logic there. We would:

  1. Load .env when needed
  2. If APP_ENV is still not set, set $_SERVER['APP_ENV'] = 'dev';
  3. Except, if $env is passed to bootstrapEnvironment() (like for console), set that $env value into $_SERVER['APP_ENV'].

We could do the same for $_SERVER['APP_DEBUG']. The idea would be to simplify / de-duplicate all of the APP_ENV and APP_DEBUG logic. We could even move the umask & Debug code into bootstrapEnvironment()


$env = $_SERVER['APP_ENV'] ?? 'dev';
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env));
Expand Down
33 changes: 33 additions & 0 deletions symfony/framework-bundle/3.3/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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 Down Expand Up @@ -35,6 +36,38 @@ public function registerBundles()
}
}

/**
* Bootstrap the environment if APP_ENV has not been set yet
* It will load the generic .env file and the .env for the APP_ENV set in the generic (if it exists)
*
* @param string|null $env If set it will load an additional ".env" file
*/
public static function bootstrapEnvironment(string $env = null)
{
if (isset($_SERVER['APP_ENV'])) {
// environment is set, do not load .env
return;
}

$projectDir = __DIR__.'/..';
if (!class_exists(Dotenv::class) || !file_exists($projectDir.'/.env')) {
// Dotenv is not installed or generic env file is not present
return;
}

// Load default .env file
$dotEnv = new Dotenv();
$dotEnv->load($projectDir.'/.env');
$appEnv = $_SERVER['APP_ENV'] ?? 'dev';
if ($env && file_exists($file = "$projectDir/.env.$env")) {
// Load a specific environment requested by the user
$dotEnv->load($file);
} elseif ($appEnv && $appEnv !== $env && file_exists($file = "$projectDir/.env.$appEnv")) {
// Load the environment for the app set in the generic
$dotEnv->load($file);
}
}

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
$container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php'));
Expand Down
8 changes: 4 additions & 4 deletions symfony/phpunit-bridge/3.3/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we update phpunit bridge configuration too ? or should this be done in phpunit bridge script instead?

>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<env name="SHELL_VERBOSITY" value="-1" />

<!-- override or set env variables for the test env here -->
<env name="APP_ENV" value="test" />
<env name="APP_DEBUG" value="1" />
<env name="APP_SECRET" value="s$cretf0rt3st" />
<env name="SHELL_VERBOSITY" value="-1" />
<!-- define your env variables for the test env here -->
</php>

<testsuites>
Expand Down
18 changes: 18 additions & 0 deletions symfony/phpunit-bridge/3.3/tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require __DIR__.'/../vendor/autoload.php';

use App\Kernel;

/*
* Environment variables can also be specified in phpunit.xml.dist.
* Those variables will override any defined in .env.
*/

Kernel::bootstrapEnvironment($_ENV['APP_ENV'] ?? null);

$debug = $_SERVER['APP_DEBUG'] ?? true;

if ($debug) {
umask(0000);
}