Skip to content

Commit

Permalink
feature Sylius#10089 Switch to Symfony's dotenv file handling (pamil)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.2-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.4
| Bug fix?        | no
| New feature?    | yes
| BC breaks?      | maybe, but not in the code (Behat configuration, needs some testing)
| Deprecations?   | no
| Related tickets | -
| License         | MIT

See https://symfony.com/doc/current/configuration/dot-env-changes.html for reference.

TODO (after merge):
 - Port this change to Sylius/Sylius-Standard (Sylius/Sylius-Standard#316)
 - Port this change to Sylius/PluginSkeleton (Sylius/PluginSkeleton#155)

<!--
 - Bug fixes must be submitted against the 1.2 or 1.3 branch (the lowest possible)
 - Features and deprecations must be submitted against the master branch
 - Make sure that the correct base branch is set
-->


Commits
-------

7ed7239 Switch to Symfony's dotenv file handling
  • Loading branch information
pamil committed Jan 13, 2019
2 parents 6667183 + 7ed7239 commit 10d6b88
Show file tree
Hide file tree
Showing 27 changed files with 85 additions and 163 deletions.
File renamed without changes.
3 changes: 3 additions & 0 deletions .env.test
@@ -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
@@ -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.

18 changes: 8 additions & 10 deletions .gitignore
@@ -1,8 +1,4 @@
/var/*
!/var/.gitkeep

/public/assets
/public/bundles
/public/css
/public/js
/public/media
Expand All @@ -15,7 +11,6 @@
/docs/.doctrees

/composer.lock
/vendor
/node_modules

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

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

###> symfony/web-server-bundle ###
/.web-server-pid
Expand Down
4 changes: 1 addition & 3 deletions .travis.yml
Expand Up @@ -6,6 +6,7 @@ sudo: false

env:
global:
- APP_ENV=test_cached
- SYLIUS_CACHE_DIR=$HOME/.sylius-cache
- SYLIUS_BUILD_DIR=etc/build

Expand Down Expand Up @@ -71,9 +72,6 @@ cache:
- $SYLIUS_CACHE_DIR

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

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

install:
Expand Down
20 changes: 0 additions & 20 deletions app/AppKernel.php

This file was deleted.

20 changes: 0 additions & 20 deletions app/TestAppKernel.php

This file was deleted.

2 changes: 1 addition & 1 deletion behat.yml.dist
@@ -1,7 +1,7 @@
# This file is part of the Sylius package.
# (c) Paweł Jędrzejewski

# This file is referenced in Sylius-Standard v1.0.0 - v1.2.x
# This file is referenced in Sylius-Standard v1.0.0 - v1.3.x

imports:
- src/Sylius/Behat/Resources/config/profiles.yml
Expand Down
26 changes: 12 additions & 14 deletions bin/console
@@ -1,40 +1,38 @@
#!/usr/bin/env php
<?php

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
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
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -121,7 +121,7 @@
"sylius-labs/coding-standard": "^3.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/flex": "^1.1",
"symfony/intl": "^3.4|^4.1",
"symfony/web-profiler-bundle": "^3.4|^4.1",
Expand Down Expand Up @@ -254,7 +254,7 @@
"spec\\Sylius\\Bundle\\UiBundle\\": "src/Sylius/Bundle/UiBundle/spec/",
"spec\\Sylius\\Bundle\\UserBundle\\": "src/Sylius/Bundle/UserBundle/spec/"
},
"classmap": ["app/AppKernel.php", "app/TestAppKernel.php", "src/Kernel.php"]
"classmap": ["src/Kernel.php"]
},
"scripts": {
"auto-scripts": {
Expand Down
21 changes: 21 additions & 0 deletions config/bootstrap.php
@@ -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';
10 changes: 5 additions & 5 deletions etc/travis/suites/application/before_script.sh
Expand Up @@ -4,13 +4,13 @@ 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" "Sylius"
run_command "bin/console doctrine:database:create --env=test_cached -vvv" || exit $? # Have to be run with debug = true, to omit generating proxies before setting up the database
run_command "APP_DEBUG=1 bin/console cache:warmup --env=dev -vvv" || exit $? # For PHPStan
run_command "bin/console cache:warmup --env=test_cached --no-debug -vvv" || exit $? # For tests
run_command "bin/console doctrine:migrations:migrate --no-interaction --env=test_cached --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" "Sylius"
run_command "bin/console assets:install public --env=test_cached --no-debug -vvv" || exit $?
run_command "bin/console assets:install public -vvv" || exit $?
run_command "yarn build" || exit $?

print_header "Making filesystem readonly" "Sylius"
Expand Down
Expand Up @@ -28,7 +28,7 @@ prepare_for_behat_with_js() {
run_command "java -Dwebdriver.chrome.driver=$SYLIUS_CACHE_DIR/chromedriver -jar $SYLIUS_CACHE_DIR/selenium.jar > /dev/null 2>&1 &"

# Run webserver
run_command "bin/console server:run 127.0.0.1:8080 -d public --env=test_cached --no-debug --quiet > /dev/null 2>&1 &"
run_command "bin/console server:run 127.0.0.1:8080 -d 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)" "Sylius"
retry_run_command "bin/console sylius:fixtures:load default --env=test_cached --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)" "Sylius"
run_command "bin/console sylius:install --no-interaction --env=test_cached --no-debug -vvv"
run_command "bin/console sylius: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)" "Sylius"
run_command "bin/console doctrine:schema:validate --env=test_cached --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)" "Sylius"
run_command "bin/console lint:twig src --env=test_cached --no-debug"
run_command "bin/console lint:twig src"
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)" "Sylius"
run_command "bin/console lint:yaml src --env=test_cached --no-debug"
run_command "bin/console lint:yaml src"
14 changes: 5 additions & 9 deletions phpunit.xml.dist
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php">
bootstrap="config/bootstrap.php"
>
<testsuites>
<testsuite name="Sylius Test Suite">
<directory>tests</directory>
Expand All @@ -14,16 +16,10 @@
<ini name="error_reporting" value="-1" />

<!-- ###+ symfony/framework-bundle ### -->
<server name="APP_ENV" value="test"/>
<server name="APP_DEBUG" value="1" />
<server name="APP_SECRET" value="10719b14109895993491c4c604fcb24e"/>
<!-- env name="TRUSTED_PROXIES" value="127.0.0.1,127.0.0.2" -->
<!-- env name="TRUSTED_HOSTS" value="localhost,example.com" -->
<env name="APP_ENV" value="test"/>
<env name="SHELL_VERBOSITY" value="-1" />
<!-- ###- symfony/framework-bundle ### -->

<server name="KERNEL_CLASS" value="Kernel" />

<server name="SHELL_VERBOSITY" value="-1" />
<server name="IS_DOCTRINE_ORM_SUPPORTED" value="true" />
<server name="ESCAPE_JSON" value="true" />
</php>
Expand Down

0 comments on commit 10d6b88

Please sign in to comment.