Commit 73f7f504 authored by Emma's avatar Emma 🦉

better handling of environment variables

parent e5204794
Pipeline #48771458 passed with stages
in 8 minutes and 15 seconds
# This file is a "template" of which env vars need to be defined for your application
# Copy this file to .env file for development, create environment variables when deploying to production
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration
################################################################################
# Don't change this file! Create a .env.local file containing the key/value #
# pairs you want to override! Alternatively, real environment values will take #
# priority over values declared here. #
################################################################################
SITE_NAME=Postmill
NO_REPLY_ADDRESS="no-reply@example.com"
......@@ -16,9 +18,8 @@ APP_SECRET="change this in prod or die"
###< symfony/framework-bundle ###
###> doctrine/doctrine-bundle ###
# Change the username, password, host, database name and server version
# according to your setup. Don't even think about trying another kind of
# database, it won't work
# Copy this line to .env.local and change it according to your database setup.
# Don't even think about trying another kind of database, it won't work.
DATABASE_URL=pgsql://db_user:db_password@localhost:5432/db_name?serverVersion=9.6
###< doctrine/doctrine-bundle ###
......
KERNEL_CLASS='App\Kernel'
APP_SECRET=33a8a493d8fffeab937480b451ebb289
APP_ENABLE_WEBHOOKS=1
APP_ENABLE_EXTERNAL_SEARCH=1
......@@ -33,7 +33,9 @@ docker-compose.override.yml
/web/
###> symfony/framework-bundle ###
.env
/.env.local
/.env.local.php
/.env.*.local
/public/bundles/
/var/
/vendor/
......
......@@ -37,24 +37,17 @@ build-assets:prod:
services:
- postgres:9.4-alpine
variables:
APP_ENABLE_WEBHOOKS: 1
APP_ENABLE_EXTERNAL_SEARCH: 1
APP_ENV: test
APP_LOCALE: en
APP_SECRET: foo
DATABASE_URL: "pgsql://postmill@postgres/postmill?serverVersion=9.4"
NO_REPLY_ADDRESS: "no-reply@example.com"
POSTGRES_USER: postmill
POSTGRES_PASSWORD: ""
SITE_NAME: Postmill
SYMFONY_DEPRECATIONS_HELPER: disabled
APP_ENV: test
before_script:
- apt-get update -yqq
- apt-get install -yqq git libpq-dev libcurl4-gnutls-dev libicu-dev
libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev
libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev
libldap2-dev unixodbc-dev libsqlite3-dev libaspell-dev libsnmp-dev
libpcre3-dev libtidy-dev
libpcre3-dev libtidy-dev libzip-dev
- docker-php-ext-install mbstring pdo_pgsql curl json intl gd xml zip
bz2 opcache
- pecl install apcu
......@@ -81,3 +74,7 @@ test:7.1:
test:7.2:
<<: *php-test
image: php:7.2
test:7.3:
<<: *php-test
image: php:7.3
......@@ -31,11 +31,17 @@ build-prod`.
1. Run `composer install`.
2. You should now have a `.env` file in the project root. Edit this to your
liking or leave the defaults alone, but you *must* change `DATABASE_URL`.
2. Create a `.env.local` file in the project root. At minimum, you must define
your database configuration here:
~~~bash
DATABASE_URL='pgsql://db_user:db_password@localhost:5432/db_name?serverVersion=9.6'
~~~
Instructions for setting up a database can be found at
[docs/database-setup.md](docs/database-setup.md).
You can copy other values from `.env` to override them in your installation.
3. Run `vendor/bin/requirements-checker` to ensure your environment meets
necessary requirements needed to run Postmill. Fix any errors that arise.
......
......@@ -5,11 +5,10 @@ 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 __DIR__.'/../config/bootstrap.php';
if (!class_exists(Application::class)) {
throw new \RuntimeException(
......@@ -17,18 +16,6 @@ if (!class_exists(Application::class)) {
);
}
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');
$debug = ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption(['--no-debug', '']);
......
......@@ -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": "12d4b2e3527807ee530f73d81b3d93f9",
"content-hash": "ccc10338595928f5a50965915167dfb6",
"packages": [
{
"name": "composer/ca-bundle",
......@@ -4195,6 +4195,63 @@
"homepage": "https://symfony.com",
"time": "2019-01-16T20:35:37+00:00"
},
{
"name": "symfony/dotenv",
"version": "v4.2.3",
"source": {
"type": "git",
"url": "https://github.com/symfony/dotenv.git",
"reference": "9a3bdfcd7a0d9602754894d76c614d15ca366535"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/dotenv/zipball/9a3bdfcd7a0d9602754894d76c614d15ca366535",
"reference": "9a3bdfcd7a0d9602754894d76c614d15ca366535",
"shasum": ""
},
"require": {
"php": "^7.1.3"
},
"require-dev": {
"symfony/process": "~3.4|~4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.2-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Dotenv\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Registers environment variables from a .env file",
"homepage": "https://symfony.com",
"keywords": [
"dotenv",
"env",
"environment"
],
"time": "2019-01-24T21:39:51+00:00"
},
{
"name": "symfony/event-dispatcher",
"version": "v4.2.3",
......@@ -7953,63 +8010,6 @@
"description": "A debug pack for Symfony projects",
"time": "2018-12-10T12:11:11+00:00"
},
{
"name": "symfony/dotenv",
"version": "v4.2.3",
"source": {
"type": "git",
"url": "https://github.com/symfony/dotenv.git",
"reference": "9a3bdfcd7a0d9602754894d76c614d15ca366535"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/dotenv/zipball/9a3bdfcd7a0d9602754894d76c614d15ca366535",
"reference": "9a3bdfcd7a0d9602754894d76c614d15ca366535",
"shasum": ""
},
"require": {
"php": "^7.1.3"
},
"require-dev": {
"symfony/process": "~3.4|~4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.2-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Dotenv\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Registers environment variables from a .env file",
"homepage": "https://symfony.com",
"keywords": [
"dotenv",
"env",
"environment"
],
"time": "2019-01-24T21:39:51+00:00"
},
{
"name": "symfony/maker-bundle",
"version": "v1.11.3",
......
<?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';
......@@ -23,15 +23,8 @@ services:
php:
build: docker/php
environment:
- APP_ENABLE_WEBHOOKS=1
- APP_ENABLE_EXTERNAL_SEARCH=1
- APP_ENV=dev
- APP_SECRET=very secret, wow!
- COMPOSER_HOME=/.composer
- DATABASE_URL=pgsql://postgres@db/postgres?serverVersion=11
- NO_REPLY_ADDRESS=no-reply@example.com
- MAILER_URL=null://localhost
- SITE_NAME=Postmill
links:
- db
volumes:
......
......@@ -3875,8 +3875,8 @@
"dev": true,
"optional": true,
"requires": {
"co": "4.6.0",
"json-stable-stringify": "1.0.1"
"co": "^4.6.0",
"json-stable-stringify": "^1.0.1"
}
},
"ansi-regex": {
......@@ -3956,7 +3956,6 @@
"version": "2.10.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"hoek": "2.x.x"
}
......@@ -4073,7 +4072,7 @@
"dev": true,
"optional": true,
"requires": {
"jsbn": "0.1.1"
"jsbn": "~0.1.0"
}
},
"extend": {
......@@ -4194,8 +4193,8 @@
"dev": true,
"optional": true,
"requires": {
"ajv": "4.11.8",
"har-schema": "1.0.5"
"ajv": "^4.9.1",
"har-schema": "^1.0.5"
}
},
"has-unicode": {
......@@ -4283,7 +4282,7 @@
"dev": true,
"optional": true,
"requires": {
"jsbn": "0.1.1"
"jsbn": "~0.1.0"
}
},
"jsbn": {
......@@ -4304,7 +4303,7 @@
"dev": true,
"optional": true,
"requires": {
"jsonify": "0.0.0"
"jsonify": "~0.0.0"
}
},
"json-stringify-safe": {
......@@ -4402,8 +4401,8 @@
"dev": true,
"optional": true,
"requires": {
"abbrev": "1.1.0",
"osenv": "0.1.4"
"abbrev": "1",
"osenv": "^0.1.4"
}
},
"npmlog": {
......@@ -4461,8 +4460,8 @@
"dev": true,
"optional": true,
"requires": {
"os-homedir": "1.0.2",
"os-tmpdir": "1.0.2"
"os-homedir": "^1.0.0",
"os-tmpdir": "^1.0.0"
}
},
"path-is-absolute": {
......
......@@ -4,7 +4,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="config/bootstrap.php"
>
<filter>
<whitelist>
......@@ -14,23 +14,7 @@
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="App\Kernel" />
<env name="SITE_NAME" value="Postmill" />
<env name="NO_REPLY_ADDRESS" value="no-reply@example.com" />
<!-- ###+ symfony/swiftmailer-bundle ### -->
<env name="MAILER_URL" value="null://localhost"/>
<!-- ###- symfony/swiftmailer-bundle ### -->
<!-- ###+ symfony/framework-bundle ### -->
<env name="APP_ENV" value="test"/>
<env name="APP_SECRET" value="33a8a493d8fffeab937480b451ebb289"/>
<!-- ###- symfony/framework-bundle ### -->
<!-- ###+ doctrine/doctrine-bundle ### -->
<!--<env name="DATABASE_URL" value="mysql://db_user:db_password@127.0.0.1:3306/db_name"/>-->
<!-- ###- doctrine/doctrine-bundle ### -->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled" />
</php>
<testsuites>
......
......@@ -2,28 +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';
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'] ?? 'dev';
$debug = $_SERVER['APP_DEBUG'] ?? ('prod' !== $env);
if ($debug) {
if ($_SERVER['APP_DEBUG']) {
umask(0000);
Debug::enable();
......@@ -32,18 +15,18 @@ if ($debug) {
@include __DIR__.'/../var/maintenance.php';
}
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) {
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts(explode(',', $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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment