Skip to content

Commit 84de97a

Browse files
committed
bug #399 Use dynamic env params for critical settings and restore Heroku deploys (dzuelke)
This PR was squashed before being merged into the master branch (closes #399). Discussion ---------- Use dynamic env params for critical settings and restore Heroku deploys Database, log destination, and secrets are now configurable via environment variables (via the quasi standard `DATABASE_URL`, and `SYMFONY_LOG`, and `SYMFONY_SECRET`). All three fall back to defaults from `parameters.yml(.dist)`. For simplicity (one param versus many), a URL is now used again for SQLite, because that works fine since Doctrine DBAL 2.5.5. Deploys to Heroku are now fixed again; @bocharsky-bw's PR #377 was not a fix for #371, because SQLite does not scale horizontally :p The original problem was @javiereguiluz's 56cfa66 change to Symfony 3.1, which used individual parameters for config. Again, no longer a problem since DBAL 2.5.5. It also uses `php://stderr` for env `prod` on Heroku only, which @javiereguiluz had also removed in 56cfa66 (for the future, I'd encourage separate commits for stuff like this, as it makes it much easier to untangle individual parts and reasons for changes). This demo app ships with sample data in SQLite, but for other databases, the data needs to be seeded, and that can even happen in a prod env. @javiereguiluz' commit e2264b0 removed the fixtures bundle needed for that from the kernel init; it's now back. This app now works just fine with the usual defaults both in `dev` and `prod` envs, but users who want to try running it on Docker or on a PaaS like Heroku can now trivially adjust relevant settings via environment variables. /CC @stof @fabpot Commits ------- 21fcb92 Use dynamic env params for critical settings and restore Heroku deploys
2 parents 1bf9dec + 21fcb92 commit 84de97a

8 files changed

+24
-18
lines changed

app.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
"repository": "https://github.com/symfony/symfony-demo",
1010
"logo": "https://symfony.com/images/v5/pictos/demoapp.svg?v=4",
1111
"success_url": "/",
12+
"scripts": {
13+
"postdeploy": "php bin/console doctrine:schema:create && php bin/console doctrine:fixtures:load -n"
14+
},
1215
"env": {
1316
"SYMFONY_ENV": "prod",
17+
"SYMFONY_LOG": "php://stderr",
1418
"SYMFONY_SECRET": {
1519
"description": "Extra entropy for %kernel.secret%; used for CSRF tokens, cookies and signed URLs.",
1620
"generator": "secret"
1721
}
1822
},
23+
"addons": [
24+
"heroku-postgresql"
25+
],
1926
"image": "heroku/php"
2027
}

app/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function registerBundles()
2121
new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
2222
new CodeExplorerBundle\CodeExplorerBundle(),
2323
new AppBundle\AppBundle(),
24+
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(), // used for initial population of non-SQLite databases in production envs
2425
// uncomment the following line if your application sends emails
2526
// new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
2627
];
@@ -34,7 +35,6 @@ public function registerBundles()
3435
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
3536
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
3637
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
37-
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
3838
}
3939

4040
return $bundles;

app/config/config.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ framework:
3131
# http://symfony.com/doc/current/book/http_cache.html#edge-side-includes
3232
esi: { enabled: true }
3333
translator: { fallback: "%locale%" }
34-
secret: "%secret%"
34+
secret: "%env(SYMFONY_SECRET)%"
3535
router:
3636
resource: "%kernel.root_dir%/config/routing.yml"
3737
strict_requirements: ~
@@ -61,10 +61,10 @@ twig:
6161
# Doctrine Configuration (used to access databases and manipulate their information)
6262
doctrine:
6363
dbal:
64-
# if you don't want to use SQLite, comment the two following lines
65-
driver: "pdo_sqlite"
66-
path: "%kernel.root_dir%/data/blog.sqlite"
67-
# uncomment the following lines to use a database different than SQLite
64+
# if you don't want to use SQLite, change the URL in parameters.yml or set the DATABASE_URL environment variable
65+
url: "%env(DATABASE_URL)%"
66+
67+
# instead of using a URL, you may also uncomment the following lines to configure your database
6868
# driver: pdo_mysql
6969
# host: "%database_host%"
7070
# port: "%database_port%"

app/config/config_dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ monolog:
1515
handlers:
1616
main:
1717
type: stream
18-
path: "%kernel.logs_dir%/%kernel.environment%.log"
18+
path: "%env(SYMFONY_LOG)%"
1919
level: info
2020
console:
2121
type: console

app/config/config_prod.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ monolog:
1919
handler: nested
2020
nested:
2121
type: stream
22-
path: "%kernel.logs_dir%/%kernel.environment%.log"
22+
path: "%env(SYMFONY_LOG)%"
2323
level: debug
2424
console:
2525
type: console

app/config/parameters.yml.dist

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ parameters:
66
# this demo application uses an embedded SQLite database to simplify setup.
77
# in a real Symfony application you probably will use a MySQL or PostgreSQL database
88
# the path must be relative or else it will not work on Windows
9-
database_url: 'sqlite:///%kernel.root_dir%/data/blog.sqlite'
9+
env(DATABASE_URL): 'sqlite:///%kernel.root_dir%/data/blog.sqlite'
1010

1111
# Uncomment this line to use a MySQL database instead of SQLite:
1212
#
13-
# database_url: 'mysql://root:[email protected]:3306/symfony_demo'
13+
# env(DATABASE_URL): 'mysql://root:[email protected]:3306/symfony_demo'
1414
#
1515
# Furthermore, you must remove or comment out the "doctrine" section from config_dev.yml regarding SQLite!
1616
#
@@ -35,4 +35,7 @@ parameters:
3535
# used internally by Symfony in several places (CSRF tokens, URI signing,
3636
# 'Remember Me' functionality, etc.)
3737
# see: http://symfony.com/doc/current/reference/configuration/framework.html#secret
38-
secret: 'secret_value_for_symfony_demo_application'
38+
env(SYMFONY_SECRET): 'secret_value_for_symfony_demo_application'
39+
40+
# Destination for log files; can also be "php://stderr" etc
41+
env(SYMFONY_LOG): %kernel.logs_dir%/%kernel.environment%.log

composer.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@
6464
"symfony-tests-dir": "tests",
6565
"symfony-assets-install": "relative",
6666
"incenteev-parameters": {
67-
"file": "app/config/parameters.yml",
68-
"env-map": {
69-
"database_url": "DATABASE_URL",
70-
"secret": "SYMFONY_SECRET"
71-
}
67+
"file": "app/config/parameters.yml"
7268
}
7369
}
7470
}

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)