Skip to content

Commit

Permalink
Generate APP_SECRET in .env.local when missing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Sep 30, 2024
1 parent 22d4acf commit bef9131
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=2ca64f8d83b9e89f5f19d672841d6bb8
APP_SECRET=%generate(secret)%
#TRUSTED_PROXIES=127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
#TRUSTED_HOSTS='^(localhost|example\.com)$'
###< symfony/framework-bundle ###
Expand Down
16 changes: 16 additions & 0 deletions .env.setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$env = file_get_contents('.env');

if (!preg_match('{^APP_SECRET=("?)%generate\(secret\)%\1([\r\n]++)}m', $env, $m)) {
return;
}

$eol = $m[2];
$local = is_file('.env.local') ? file_get_contents('.env.local') : '';

if (preg_match('{^APP_SECRET=}m', $local)) {
return;
}

file_put_contents('.env.local', 'APP_SECRET="'.bin2hex(random_bytes(16)).'"'.$eol.$local);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
},
"scripts": {
"auto-scripts": {
".env.setup.php": "php-script",
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd",
"importmap:install": "symfony-cmd",
Expand Down

0 comments on commit bef9131

Please sign in to comment.