Skip to content

Commit

Permalink
minor #323 Incorporate dotenv changes from Symfony (Zales0123, pamil)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.3-dev branch.

Discussion
----------



Commits
-------

a126fa7 Incorporate dotenv changes from Symfony
7ed29ac Fix the build
c113467 Fix the Docker build
  • Loading branch information
Zales0123 authored Jan 23, 2019
2 parents 3b0615c + c113467 commit 29c5820
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 133 deletions.
File renamed without changes.
2 changes: 2 additions & 0 deletions .env.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
APP_DEBUG=0
APP_SECRET='s$cretf0rt3st'
23 changes: 0 additions & 23 deletions .env.prod.dist

This file was deleted.

3 changes: 3 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
APP_SECRET='s$cretf0rt3st'

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

This file was deleted.

4 changes: 4 additions & 0 deletions .env.test_cached
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
APP_DEBUG=0
APP_SECRET='s$cretf0rt3st'

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

This file was deleted.

20 changes: 9 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/var/*
!/var/.gitignore

/public/assets
/public/bundles
/public/css
/public/js
/public/media

/vendor
/node_modules

/etc/build/*
Expand All @@ -16,16 +11,19 @@
/behat.yml
/phpspec.yml

/.env
/.env.prod
/.env.staging
/.env.test
/.env.test_cached

/docker-compose.override.yaml
/docker-compose.override.yml
/docker/mysql/data/

###> symfony/framework-bundle ###
/.env.*.local
/.env.local
/.env.local.php
/public/bundles
/var/
/vendor/
###< symfony/framework-bundle ###

###> symfony/web-server-bundle ###
/.web-server-pid
###< symfony/web-server-bundle ###
Expand Down
23 changes: 12 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ dist: trusty
php:
- 7.2

env:
global:
- APP_ENV=test_cached

jobs:
include:
- &test
Expand All @@ -31,20 +35,17 @@ jobs:
- echo "memory_limit=4096M" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini

- cp .env.test_cached.dist .env.test_cached
- set -a && source .env.test_cached && set +a

install:
- if [ ! -z "${SYMFONY_VERSION}" ]; then bin/require-symfony-version composer.json "${SYMFONY_VERSION}"; fi
- composer update --no-interaction --prefer-dist
- yarn install

before_script:
- bin/console doctrine:database:create --env=test_cached -vvv # Have to be run with debug = true, to omit generating proxies before setting up the database
- bin/console cache:warmup --env=test_cached --no-debug -vvv
- bin/console doctrine:migrations:migrate --no-interaction --env=test_cached --no-debug -vvv
- APP_DEBUG=1 bin/console doctrine:database:create -vvv # Have to be run with debug = true, to omit generating proxies before setting up the database
- bin/console cache:warmup -vvv
- bin/console doctrine:migrations:migrate --no-interaction -vvv

- bin/console assets:install public --env=test_cached --no-debug -vvv
- bin/console assets:install public -vvv
- yarn build

# Configure display
Expand Down Expand Up @@ -74,7 +75,7 @@ jobs:
- java -Dwebdriver.chrome.driver=$SYLIUS_CACHE_DIR/chromedriver -jar $SYLIUS_CACHE_DIR/selenium.jar > /dev/null 2>&1 &

# Run webserver
- bin/console server:run localhost:8080 -d public --env=test_cached --no-debug --quiet > /dev/null 2>&1 &
- bin/console server:run localhost:8080 -d public --quiet > /dev/null 2>&1 &

script:
- composer validate --strict
Expand All @@ -85,7 +86,7 @@ jobs:

- vendor/bin/phpspec run --no-interaction -f dot

- bin/console sylius:fixtures:load --no-interaction --env=test_cached --no-debug -vvv
- bin/console sylius:fixtures:load --no-interaction -vvv

- echo "Testing (Behat, without javascript scenarios; ~@javascript && ~@todo && ~@cli)" "Sylius"
- vendor/bin/behat --strict --no-interaction -vvv -f progress --tags="~@javascript && ~@todo && ~@cli"
Expand All @@ -99,9 +100,9 @@ jobs:

- <<: *test

name: "Symfony 4.1.* build"
name: "Symfony 4.2.* build"

env: SYMFONY_VERSION="4.1.*" SYLIUS_CACHE_DIR=$HOME/.sylius-cache SYLIUS_BUILD_DIR=etc/build
env: SYMFONY_VERSION="4.2.*" SYLIUS_CACHE_DIR=$HOME/.sylius-cache SYLIUS_BUILD_DIR=etc/build

-
stage: test
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ RUN set -eux; \
composer clear-cache

# copy only specifically what we need
COPY .env .env.prod .env.test .env.test_cached ./
COPY bin bin/
COPY config config/
COPY public public/
Expand Down
27 changes: 11 additions & 16 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

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

$envFile = file_exists(__DIR__.'/../.env') ? __DIR__.'/../.env' : __DIR__.'/../.env.dist';
(new Dotenv())->load($envFile);
if ($input->hasParameterOption('--no-debug', true)) {
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
}

$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);
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: 0 additions & 1 deletion bin/require-symfony-version
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ $packages = [
'symfony/dependency-injection',
'symfony/doctrine-bridge',
'symfony/dom-crawler',
'symfony/dotenv',
'symfony/event-dispatcher',
'symfony/expression-language',
'symfony/filesystem',
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"sylius-labs/coding-standard": "^2.0",
"symfony/browser-kit": "^3.4|^4.1",
"symfony/debug-bundle": "^3.4|^4.1",
"symfony/dotenv": "^3.4|^4.1",
"symfony/dotenv": "^4.2",
"symfony/intl": "^3.4|^4.1",
"symfony/web-profiler-bundle": "^3.4|^4.1",
"symfony/web-server-bundle": "^3.4|^4.1"
Expand All @@ -54,7 +54,8 @@
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"classmap": ["src/Kernel.php"]
},
"scripts": {
"auto-scripts": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

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

21 changes: 21 additions & 0 deletions config/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';

// 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'] ?? 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';
28 changes: 6 additions & 22 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
<?php

declare(strict_types=1);

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']) && !isset($_ENV['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.');
}

$envFile = file_exists(__DIR__.'/../.env') ? __DIR__.'/../.env' : __DIR__.'/../.env.dist';
(new Dotenv())->load($envFile);
}

$env = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'dev';
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? ('prod' !== $env));
require dirname(__DIR__).'/config/bootstrap.php';

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

0 comments on commit 29c5820

Please sign in to comment.