diff --git a/.env.dist b/.env similarity index 100% rename from .env.dist rename to .env diff --git a/.env.prod b/.env.prod new file mode 100644 index 0000000000..4f53349b60 --- /dev/null +++ b/.env.prod @@ -0,0 +1,2 @@ +APP_DEBUG=0 +APP_SECRET='s$cretf0rt3st' diff --git a/.env.prod.dist b/.env.prod.dist deleted file mode 100644 index 16f0780758..0000000000 --- a/.env.prod.dist +++ /dev/null @@ -1,23 +0,0 @@ -# This file is a "template" of which env vars needs to be defined in your configuration or in an .env file -# Set variables here that may be different on each deployment target of the app, e.g. development, staging, production. -# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration - -###> symfony/framework-bundle ### -APP_ENV=prod -APP_DEBUG=0 -APP_SECRET=EDITME -###< symfony/framework-bundle ### - -###> doctrine/doctrine-bundle ### -# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url -# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db" -# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls -DATABASE_URL=mysql://root@127.0.0.1/sylius_%kernel.environment% -###< doctrine/doctrine-bundle ### - -###> symfony/swiftmailer-bundle ### -# For Gmail as a transport, use: "gmail://username:password@localhost" -# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" -# Delivery is disabled by default via "null://localhost" -MAILER_URL=smtp://localhost -###< symfony/swiftmailer-bundle ### diff --git a/.env.test b/.env.test new file mode 100644 index 0000000000..1556a53206 --- /dev/null +++ b/.env.test @@ -0,0 +1,3 @@ +APP_SECRET='s$cretf0rt3st' + +KERNEL_CLASS='App\Kernel' diff --git a/.env.test.dist b/.env.test.dist deleted file mode 100644 index 919b4ad083..0000000000 --- a/.env.test.dist +++ /dev/null @@ -1,23 +0,0 @@ -# This file is a "template" of which env vars needs to be defined in your configuration or in an .env file -# Set variables here that may be different on each deployment target of the app, e.g. development, staging, production. -# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration - -###> symfony/framework-bundle ### -APP_ENV=test -APP_DEBUG=1 -APP_SECRET=EDITME -###< symfony/framework-bundle ### - -###> doctrine/doctrine-bundle ### -# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url -# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db" -# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls -DATABASE_URL=mysql://root@127.0.0.1/sylius_%kernel.environment% -###< doctrine/doctrine-bundle ### - -###> symfony/swiftmailer-bundle ### -# For Gmail as a transport, use: "gmail://username:password@localhost" -# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" -# Delivery is disabled by default via "null://localhost" -MAILER_URL=null://localhost -###< symfony/swiftmailer-bundle ### diff --git a/.env.test_cached b/.env.test_cached new file mode 100644 index 0000000000..dc9cf85b70 --- /dev/null +++ b/.env.test_cached @@ -0,0 +1,4 @@ +APP_DEBUG=0 +APP_SECRET='s$cretf0rt3st' + +KERNEL_CLASS='App\Kernel' diff --git a/.env.test_cached.dist b/.env.test_cached.dist deleted file mode 100644 index 9788dd0749..0000000000 --- a/.env.test_cached.dist +++ /dev/null @@ -1,23 +0,0 @@ -# This file is a "template" of which env vars needs to be defined in your configuration or in an .env file -# Set variables here that may be different on each deployment target of the app, e.g. development, staging, production. -# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration - -###> symfony/framework-bundle ### -APP_ENV=test_cached -APP_DEBUG=0 -APP_SECRET=EDITME -###< symfony/framework-bundle ### - -###> doctrine/doctrine-bundle ### -# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url -# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db" -# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls -DATABASE_URL=mysql://root@127.0.0.1/sylius_${APP_ENV} -###< doctrine/doctrine-bundle ### - -###> symfony/swiftmailer-bundle ### -# For Gmail as a transport, use: "gmail://username:password@localhost" -# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" -# Delivery is disabled by default via "null://localhost" -MAILER_URL=null://localhost -###< symfony/swiftmailer-bundle ### diff --git a/.gitignore b/.gitignore index fbeae651e4..37184004ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,8 @@ -/var/* -!/var/.gitignore - /public/assets -/public/bundles /public/css /public/js /public/media -/vendor /node_modules /etc/build/* @@ -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 ### diff --git a/.travis.yml b/.travis.yml index 2acd5af2e9..0607f68955 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,10 @@ dist: trusty php: - 7.2 +env: + global: + - APP_ENV=test_cached + jobs: include: - &test @@ -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 @@ -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 @@ -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" @@ -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 diff --git a/Dockerfile b/Dockerfile index e6adaf02e4..88546cd124 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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/ diff --git a/bin/console b/bin/console index 2d1d067c49..52fd3989fb 100755 --- a/bin/console +++ b/bin/console @@ -1,36 +1,31 @@ #!/usr/bin/env php 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)) { @@ -38,6 +33,6 @@ if ($debug) { } } -$kernel = new Kernel($env, $debug); +$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $application = new Application($kernel); $application->run($input); diff --git a/bin/require-symfony-version b/bin/require-symfony-version index 8d7beafeeb..d8f6a961ca 100755 --- a/bin/require-symfony-version +++ b/bin/require-symfony-version @@ -13,7 +13,6 @@ $packages = [ 'symfony/dependency-injection', 'symfony/doctrine-bridge', 'symfony/dom-crawler', - 'symfony/dotenv', 'symfony/event-dispatcher', 'symfony/expression-language', 'symfony/filesystem', diff --git a/composer.json b/composer.json index 65ed2baa17..1edaca7568 100644 --- a/composer.json +++ b/composer.json @@ -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" @@ -54,7 +54,8 @@ "autoload-dev": { "psr-4": { "App\\Tests\\": "tests/" - } + }, + "classmap": ["src/Kernel.php"] }, "scripts": { "auto-scripts": { diff --git a/composer.lock b/composer.lock index 89d1673176..c27ef1dee7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ac6d91c568fd7c0fbfd8639984e1b9ec", + "content-hash": "47bdcb78c73375605723aa3a98d13b39", "packages": [ { "name": "behat/transliterator", diff --git a/config/bootstrap.php b/config/bootstrap.php new file mode 100644 index 0000000000..f6afb40429 --- /dev/null +++ b/config/bootstrap.php @@ -0,0 +1,21 @@ +=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'; diff --git a/public/index.php b/public/index.php index ff75735978..e30f90c035 100644 --- a/public/index.php +++ b/public/index.php @@ -1,42 +1,26 @@ 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();