Skip to content
This repository has been archived by the owner on Mar 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #102 from loic425/features/switch-to-dotenv-file-h…
Browse files Browse the repository at this point in the history
…andling

Switch to dotenv file handling
  • Loading branch information
loic425 committed Jan 23, 2019
2 parents b32c235 + 4502702 commit 004628f
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 92 deletions.
File renamed without changes.
3 changes: 3 additions & 0 deletions .env.test
@@ -0,0 +1,3 @@
APP_SECRET=67d829bf61dc5f87a73fd814e2c9f629

KERNEL_CLASS='App\Kernel'
23 changes: 0 additions & 23 deletions .env.test.dist

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -20,7 +20,9 @@
!/etc/build/.gitkeep

###> symfony/framework-bundle ###
/.env
/.env.*.local
/.env.local
/.env.local.php
/public/bundles/
/public/uploads/img/*
/public/uploads/avatar/*
Expand Down
4 changes: 1 addition & 3 deletions .travis.yml
Expand Up @@ -4,6 +4,7 @@ language: php

env:
global:
- APP_ENV=test
- APP_NAME_CACHE_DIR=$HOME/.app_name-cache
- APP_NAME_BUILD_DIR=etc/build

Expand Down Expand Up @@ -36,9 +37,6 @@ cache:
- $APP_NAME_CACHE_DIR

before_install:
- mv .env.test.dist .env
- set -a && source .env && set +a

- etc/travis/run-suite before_install "${APP_NAME_SUITE}"

install:
Expand Down
25 changes: 12 additions & 13 deletions bin/console
Expand Up @@ -5,35 +5,34 @@ 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);

require __DIR__.'/../vendor/autoload.php';
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.');
}

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();
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env);
}

$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true);
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption('--no-debug', true);
if ($input->hasParameterOption('--no-debug', true)) {
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
}

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

if ($debug) {
if ($_SERVER['APP_DEBUG']) {
umask(0000);

if (class_exists(Debug::class)) {
Debug::enable();
}
}

$kernel = new Kernel($env, $debug);
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$application = new Application($kernel);
$application->run($input);
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -70,6 +70,7 @@
"phpstan/phpstan-webmozart-assert": "^0.10",
"se/selenium-server-standalone": " ^2.52",
"symfony/debug-bundle": "^4.2",
"symfony/dotenv": "^4.2",
"symfony/web-profiler-bundle": "^4.2",
"symfony/web-server-bundle": "^4.2"
},
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions config/bootstrap.php
Expand Up @@ -4,18 +4,18 @@

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.');
}

// Load cached env vars if the .env.local.php file exists
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
$_SERVER += $env;
$_ENV += $env;
} elseif (!class_exists(Dotenv::class)) {
throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
} else {
// load all the .env files
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
}

$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $_SERVER['APP_ENV'] ?: $_ENV['APP_ENV'] ?: 'dev';
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: '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';
9 changes: 5 additions & 4 deletions etc/travis/suites/application/before_script.sh
Expand Up @@ -4,10 +4,11 @@ source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../../bash/common.lib.s
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../../bash/application.sh"

print_header "Setting the application up" "AppName"
run_command "bin/console doctrine:database:create --env=test -vvv" || exit $? # Have to be run with debug = true, to omit generating proxies before setting up the database
run_command "bin/console cache:warmup --env=test --no-debug -vvv" || exit $?
run_command "bin/console doctrine:migrations:migrate --no-interaction --env=test --no-debug -vvv" || exit $?
run_command "APP_DEBUG=1 bin/console doctrine:database:create -vvv" || exit $? # Have to be run with debug = true, to omit generating proxies before setting up the database
run_command "APP_DEBUG=1 APP_ENV=dev bin/console cache:warmup -vvv" || exit $? # For PHPStan
run_command "bin/console cache:warmup -vvv" || exit $? # For tests
run_command "bin/console doctrine:migrations:migrate --no-interaction -vvv" || exit $?

print_header "Setting the web assets up" "AppName"
run_command "bin/console assets:install --env=test --no-debug -vvv" || exit $?
run_command "bin/console assets:install public -vvv" || exit $?
run_command "yarn install && yarn run gulp" || exit $?
Expand Up @@ -28,7 +28,7 @@ prepare_for_behat_with_js() {
run_command "java -Dwebdriver.chrome.driver=$APP_NAME_CACHE_DIR/chromedriver -jar $APP_NAME_CACHE_DIR/selenium.jar > /dev/null 2>&1 &"

# Run webserver
run_command "bin/console server:run 127.0.0.1:8080 --docroot=public --env=test --no-debug --quiet > /dev/null 2>&1 &"
run_command "bin/console server:run 127.0.0.1:8080 --docroot=public --quiet > /dev/null 2>&1 &"
}

run_behat() {
Expand Down
2 changes: 1 addition & 1 deletion etc/travis/suites/application/script/test-fixtures
Expand Up @@ -3,4 +3,4 @@
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../../../bash/common.lib.sh"

print_header "Testing (Fixtures)" "AppName"
retry_run_command "bin/console sylius:fixtures:load default --env=test --no-interaction --no-debug"
retry_run_command "bin/console sylius:fixtures:load default --no-interaction"
2 changes: 1 addition & 1 deletion etc/travis/suites/application/script/test-installer
Expand Up @@ -3,4 +3,4 @@
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../../../bash/common.lib.sh"

print_header "Testing (Installer)" "AppName"
run_command "bin/console app:install --no-interaction --env=test --no-debug -vvv"
run_command "bin/console app:install --no-interaction -vvv"
Expand Up @@ -3,4 +3,4 @@
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../../../bash/common.lib.sh"

print_header "Validating (Doctrine schema)" "AppName"
run_command "bin/console doctrine:schema:validate --env=test --no-debug -vvv"
run_command "bin/console doctrine:schema:validate -vvv"
2 changes: 1 addition & 1 deletion etc/travis/suites/application/script/validate-twig
Expand Up @@ -3,4 +3,4 @@
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../../../bash/common.lib.sh"

print_header "Validating (Twig templates)" "AppName"
run_command "bin/console lint:twig templates --env=test --no-debug"
run_command "bin/console lint:twig templates"
2 changes: 1 addition & 1 deletion etc/travis/suites/application/script/validate-yaml-files
Expand Up @@ -3,4 +3,4 @@
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../../../bash/common.lib.sh"

print_header "Validating (Yaml files)" "AppName"
run_command "bin/console lint:yaml config --env=test --no-debug"
run_command "bin/console lint:yaml config"
8 changes: 2 additions & 6 deletions phpunit.xml.dist
Expand Up @@ -5,18 +5,14 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="config/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="App\Kernel" />

<!-- ###+ symfony/framework-bundle ### -->
<env name="APP_ENV" value="test"/>
<env name="APP_DEBUG" value="1"/>
<env name="APP_SECRET" value="5a79a1c866efef9ca1800f971d689f3e"/>
<!-- env name="TRUSTED_PROXIES" value="127.0.0.1,127.0.0.2" -->
<!-- env name="TRUSTED_HOSTS" value="'^localhost|example\.com$'" -->
<env name="SHELL_VERBOSITY" value="-1" />
<!-- ###- symfony/framework-bundle ### -->

<!-- ###+ doctrine/doctrine-bundle ### -->
Expand Down
24 changes: 6 additions & 18 deletions public/index.php
Expand Up @@ -2,37 +2,25 @@

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

require __DIR__.'/../vendor/autoload.php';
require dirname(__DIR__).'/config/bootstrap.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');
}

$env = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'dev';
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? ('prod' !== $env));

if ($debug) {
if ($_SERVER['APP_DEBUG']) {
umask(0000);

Debug::enable();
}

if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) {
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}

if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts(explode(',', $trustedHosts));
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts([$trustedHosts]);
}

$kernel = new Kernel($env, $debug);
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
Expand Down
4 changes: 2 additions & 2 deletions src/Behat/Resources/config/profiles/default.yml
Expand Up @@ -32,12 +32,12 @@ default:
- "no-sandbox"

FriendsOfBehat\SymfonyExtension:
env_file: .env
kernel:
bootstrap: ~
bootstrap: config/bootstrap.php
path: src/Kernel.php
class: 'App\Kernel'
debug: true
env: test

FriendsOfBehat\ContextServiceExtension:
imports:
Expand Down

0 comments on commit 004628f

Please sign in to comment.